Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] escaping ampersands in replace function

From: Abel Braaksma <abel.online@--------->
To:
Date: 9/4/2007 5:01:00 PM
Mario Madunic wrote:
Sorry to say



replace($l_TempString,'&','and')

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?



Cheers,
-- 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:

replace($l_TempString,'&','and')



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

select="replace($l_TempString,'&amp;','and')"



This is assuming that your input string contains a " & " 
 if it contains " &amp; "

then you want the XPath




replace($l_TempString,'&amp;','and')




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



select="replace($l_TempString,'&amp;amp;','and')"



as the & has to be written as &amp;



David



________________________________________________________________________


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent