[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: entity output

Subject: RE: entity output
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Thu, 12 Jun 2003 16:17:53 -0400
javascript escape entities
[Craig Kattner]

>  I was trying to solve the problem at the wrong 
> place. What I need to do is escape the quote inside some 
> javascript. 

You do not have to escape it unless you need to do it for javascript
purposes (e.g, you  need to change from double to single quote).  

If you need to have a javascript escape, using a backslash, you should
probably feed each string to a named template that can add a backslash.
But that has nothing to do with using an & q u o t ;, which javascript
will not understand.

Here is a template I have used to javascript-escape a single-quote.  The
template works its way through the entire string that it is passed as a
parameter.  In xslt 1.0  you have to do this  recursively.  You need to
remember that if you use, say, substring-after, and you get no match,
nothing is returned (you might think you would get the whole string, but
you do not).  Handling those cases adds some complexity.  

[The slightly odd formatting, with the closing angle brackets at the
start of new lines, is the simplest way I have found to make __sure__
that no unwanted whitespace sneaks into expressions like these.  Experts
may say you can avoid stray whitespace easily without resorting to
tricks like this, but I have been bit too many times trying to format
javascript properly.  This method may be overkill, but it works (there
is __no__ extra whitespace between elements in the template so there is
no way whitespace nodes can accidentally get added).  Yet it is
formatted almost the same as I would normally format the code].

<xsl:template name='fixapos'
><xsl:param name='data'
/><xsl:variable name='rest' select='substring-after($data,"&apos;")'
/><xsl:variable name='first'><xsl:choose><xsl:when
test='contains($data,"&apos;")'
><xsl:value-of select='substring-before($data,"&apos;")'
/></xsl:when><xsl:otherwise><xsl:value-of select='$data'
/></xsl:otherwise></xsl:choose></xsl:variable><xsl:value-of
select='$first'
/><xsl:if test='$rest'>\&apos;<xsl:call-template name='fixapos'
><xsl:with-param name='data'
     select='$rest'/></xsl:call-template></xsl:if></xsl:template>

Cheers,

Tom P

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread
  • Re: entity output, (continued)
    • Craig Kattner - Thu, 12 Jun 2003 15:30:13 -0400 (EDT)
    • Craig Kattner - Thu, 12 Jun 2003 15:37:23 -0400 (EDT)
    • Passin, Tom - Thu, 12 Jun 2003 16:18:34 -0400 (EDT) <=
      • Mukul - Thu, 12 Jun 2003 22:02:12 -0400 (EDT)
        • Jason - Fri, 13 Jun 2003 01:58:03 -0400 (EDT)
        • Mukul - Fri, 13 Jun 2003 23:47:20 -0400 (EDT)
        • Mukul - Sat, 14 Jun 2003 02:42:11 -0400 (EDT)

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.