Re: [xsl] escaping ampersands in replace function

Subject: Re: [xsl] escaping ampersands in replace function
From: Abel Braaksma <>
Date: Tue, 04 Sep 2007 18:59:22 +0200
Mario Madunic wrote:
Sorry to say

get an error message stating the & must be followed by an entity reference

precisely, that is exactly what David was explaining you (see below for his post).

replace($l_TempString,'&amp;','and') and replace($l_TempString,'&amp;amp;','and')
the amp is not replaced.

Just trying it out probably won't get you further. I am under the impression that your string does not contain what you think it contains. To find that out, change your output mode to "text" and remove all other noise and just value-of the string. That should be '&', but likely it is something else, if I understand your problem correctly. Can you make an exact copy to the list of the string you'll get if you follow this scenario?

I'm using the latest version of Saxon8 (from sourceforge).

An example of a string I'm trying to replace: A &amp; B to A and B. So th

Something else must be wrong. Maybe the string really does contain something different. If you do a replace ($string, '&amp;', 'and') then the following will be true:

 if $string has '&' >>> 'and'
 if $string has '&amp;' >>> 'andamp;'
 if $string has '&amp&amp;' >>> 'andampandamp;'

(all this means that when the string is inside an input XML it will look like '&amp;' at the very least).

Either way: if you used the simplest suggestion from David, you must have seen a difference. Perhaps you can share a bit more of the context information or provide a full minimal working sample so we can help you better?

-- Abel Braaksma

Quoting David Carlisle <davidc@xxxxxxxxx>:

I'm using XSLT2's replace function against a string but am having problems
escaping the ampersand.
ampersaand isn't a special character to xpath regular expressions, so
you don't need to escape it  so you want:


then you need to put that XPath expression in a slect attribute,
whenever you put a string containg & in an XMl file you need to write it
as &amp; so the xslt looks like


This is assuming that your input string contains a " & " if it contains " &amp; "
then you want the XPath


which would be written in an XML file such as an XSLT stylesheet as


as the & has to be written as &amp;



Current Thread