[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Problem with ampersands


Subject: RE: [xsl] Problem with ampersands
From: "Joe Fawcett" <joefawcett@xxxxxxxxxxx>
Date: Tue, 26 Sep 2006 08:26:53 +0100

Jeff

I understand the unreasonableness of many requirements but I don't understand what's happening here. Surely if you transform an XML document to another XML document then any ampersands in the output will still be escaped on serialisation?

Joe


From: Jeff Sese <jsese@xxxxxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: Xsl-List <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: [xsl] Problem with ampersands
Date: Tue, 26 Sep 2006 10:03:30 +0800

Hi,

I have this requirement from a client to retain all the entity references in their xml files after transformation so what i did was collect all the entities in the DTD and then created a character-map for single-character entities. For multi-character ones, i created a used a function to search and replace all occurrences in a text node. My problem is when i use the function and return the value, xslt escapes the ampersand symbol as &amp; and i don't know how i can unescape it without unescaping the real ampersand character.

Below is a snippet of my stylesheet and the output that i'm getting.

XSL:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:f="f;data">
<xsl:character-map name="brillMaps">
<!-- character mapping for single-character entities-->
</xsl:character-map>
<!--variable that holds the values of the multi-character entities-->
<xsl:variable name="entityTable" as="element()+">
<entity>
<search>c&#818;h&#818;k&#818;</search>
<replace>&chkeiubareiubareiubar;</replace>
</entity>
<!-- other multi-character entities--> </xsl:variable>
<xsl:function name="f:replace-all">
<xsl:param name="input" as="xs:string"/>
<xsl:param name="words-to-replace" as="xs:string*"/>
<xsl:param name="replacement" as="xs:string*"/>
<xsl:sequence select="if (exists($words-to-replace)) then f:replace-all(replace($input, $words-to-replace[1], $replacement[1]),remove($words-to-replace,1),remove($replacement,1)) else $input"/>
</xsl:function>
<xsl:output method="xml" encoding="UTF-8" use-character-maps="brillMaps"/>
<xsl:template match="/">
<xsl:variable name="str" select="'&amp; test test the quick brown fox textc&#818;h&#818;k&#818;text test test the quick brown fox'"/>
<xsl:variable name="searchStr" as="xs:string*" select="$entityTable/search[matches($str,.)]"/>
<test>
<xsl:value-of select="f:replace-all($str,$entityTable/search[matches($str,.)],$entityTable/search[matches($str,.)]/following-sibling::replace)"/>
<xsl:value-of select="$entityTable[1]/replace" disable-output-escaping="yes"/>
</test>
</xsl:template>
</xsl:stylesheet>


java net.sf.saxon.Transform CharMap.xsl CharMap.xsl > out.xml

out.xml --

<test>&amp; test test the quick brown fox text&amp;chkeiubareiubareiubar;text test test the quick brown fox&chkeiubareiubareiubar;</test>
--
*Jeff*


Current Thread
Keywords