RE: [xsl] Replace function and apostrophes

Subject: RE: [xsl] Replace function and apostrophes
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 17 Feb 2005 14:49:10 -0000
&apos; is translated to ' by the XML parser before the XPath processor sees
it. So the XPath string literal is ''' which is clearly wrong. In fact,
there's no point using &apos here: you only need it when the attribute
delimiters are apostrophes.

XPath 2.0 allows the delimiter of a string literal to be doubled withing the
string, so you can write

"replace(., '''', '\\''')" 

Alternatively, use

select='replace(., "&apos;", "\\&apos;")'

or define the strings as variables:

<xsl:variable name="x" as="xs:string">'</xsl:variable>
<xsl:variable name="x" as="xs:string">\\'</xsl:variable>


Michael Kay

> -----Original Message-----
> From: Andrew Welch [mailto:ajwelch@xxxxxxxxxxxxxxx] 
> Sent: 17 February 2005 14:35
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Replace function and apostrophes
> I'm trying to replace all occurances of apostrophes with \' 
> as part of a
> variable's select statement:
> <xsl:variable name="foo" select="replace(., '&apos;', '\\&apos;'"/>
> This fails with "invalid character in expression '\'" - any 
> ideas on how
> to do this?
> Thanks
> andrew 

Current Thread