RE: [xsl] entity output

Subject: RE: [xsl] entity output
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Thu, 12 Jun 2003 16:17:53 -0400
[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