Given an element with a value of:
<xml_element>Distrib = SU & Prem &lt;&gt; 0</xml_element>
I need to turn &lt;
or &gt;
into <
or >
because a downstream app requires it in this format throughout the entire XML document. I would need this for quotes and apostrophes too. I am tryinging a character-map in XSLT 2.0.
<xsl:character-map name="specialchar">
<xsl:output-character character="'" string="&apos;" />
<xsl:output-character character=""" string="&quot;" />
<xsl:output-character character=">" string="&gt;" />
</xsl:character-map>
The <xsl:character-map>
instruction can be used to serialize a single character to any string. However this problem requires more than one character (an ampersand followed by another character to be replaced.
<xsl:character-map>
cannot be used to solve such problems.
Here is a solution to this problem, using the XPath 2.0 replace()
function:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select=
'replace(
replace(
replace(., "&lt;", "<"),
"&gt;",
">"
),
"&apos;",
"'"
)
'/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
<xml_element>Distrib = SU & &apos;Prem &lt;&gt; 0</xml_element>
the wanted result is produced:
<xml_element>Distrib = SU & 'Prem <> 0</xml_element>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With