Subject: RE: Problem using translate to escape quotes
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Wed, 4 Dec 2002 16:33:29 -0000
|
The translate() function replaces one character with one character -
read the spec. For solutions for a multi-character replace, see the FAQ
under keyword "replace".
Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx
> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of
> Kevin Collins
> Sent: 04 December 2002 15:43
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Problem using translate to escape quotes
>
>
> I have a string containing single quotes held in a variable.
> I want to escape each quote by inserting a backslash before
> it, but I can't get it to work. The translate function
> appears to be inserting the backslashes without the quotes.
>
> Here's a test version of the stylesheet:
> --------------------------------------------
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
> <xsl:output method="html"/>
>
> <xsl:variable name="mystr">this is Kevin's 'string'</xsl:variable>
>
> <xsl:variable name="backSlashQuote">\'</xsl:variable>
> <xsl:variable name="backSlash">\</xsl:variable>
> <xsl:variable name="singleQuote">'</xsl:variable>
> <xsl:variable name="backSlashQuote2"><xsl:value-of
> select="concat( $backSlash, $singleQuote )"/></xsl:variable>
>
> <xsl:variable name="mystr_escaped">
> <xsl:value-of select="translate( $mystr, $singleQuote,
> $backSlashQuote )"/> </xsl:variable>
>
> <xsl:template match="/">
> <html>
> <body>
> <xsl:value-of select="$mystr"/>
> <br/>
> <xsl:value-of select="$mystr_escaped"/>
> </body>
> </html>
> </xsl:template>
>
> </xsl:stylesheet>
> --------------------------------------------
>
> Test source document (any one will do):
> --------------------------------------------
> <?xml version="1.0" encoding="iso-8859-1"?>
> <mynode></mynode>
> --------------------------------------------
>
> I get the same result regardless of whether I use
> $backSlashQuote or $backSlashQuote2 in the translate
> function. Here's the output I'm
> getting:
> --------------------------------------------
>
> <html>
> <body>this is Kevin's 'string'<br>this is Kevin\s
> \string\</body> </html>
>
> --------------------------------------------
>
> I'm using MSXML 4.0.
>
> Thanks for any help with this!
> Kevin Collins
>
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|