Problem: We have character entities come to our systems in various formats (Ex: &
and &
) and we need to convert them to a standard XML character entities if needed (& < > ' "
) and then maintain them as entities through a couple of separate tranformations.
Given XML of:
<rootelm>
<testdata>&apos; &gt; &lt; &quot;</testdata>
</rootelm>
and a stylesheet of (based on xsl:character-map to replace special characters):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- COPY EVERYTHING -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="@* | node()">
<xsl:sort select="local-name()"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:variable name="quote">
<xsl:text>&quot;</xsl:text>
</xsl:variable>
<xsl:variable name="quote2">
<xsl:value-of select="string('"')"/>
</xsl:variable>
<xsl:template match="text()[contains(.,'&lt;') or contains(.,'&gt;') or contains(.,'&quot;') or contains(.,'&apos;')]">
<xsl:value-of select='replace(
replace(
replace(
replace(., "&lt;", "<"),
"&gt;",
">"
),
"&apos;",
"'"
),
$quote,
$quote2
)
' />
</xsl:template>
</xsl:stylesheet>
How can I keep the apostrophe's and quotes as entities (source system expects/needs it)?
Current Output:
<rootelm>
<testdata>' > < "</testdata>
</rootelm>
Use Character Maps :