sanjay
sanjay

Reputation: 1020

Keep """ entity in XSLT transformation

I have the following XML:

<doc>
    <chap>
        &gt;The bowler &gt;delivers&lt; the ball &lt;
        &gt;to the batsman who attempts to &lt;
        &gt;hit the ball &quot; with his  &quot;  bat away from &lt;
        &gt;the fielders so he can run to the &lt;
        &gt;other end of the pitch and score a run.&lt;
    </chap>
</doc>

I have to write some XSL to do some tasks to this input XML. But when I copy all nodes using...

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

...in the resulting file, instead of &quot;. In the resulting file those are converted to ". I don't want that to happen.

<doc>
    <chap>
        &gt;The bowler &gt;delivers&lt; the ball &lt;
        &gt;to the batsman who attempts to &lt;
        &gt;hit the ball " with his  "  bat away from &lt;
        &gt;the fielders so he can run to the &lt;
        &gt;other end of the pitch and score a run.&lt;
    </chap>
</doc>

Is there any method to keep &quot; as it is in the resulting XML?

Upvotes: 2

Views: 1131

Answers (1)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

You can use xsl:use-character-maps as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

    <xsl:output use-character-maps="CharMap"/>

    <xsl:character-map name="CharMap">
        <xsl:output-character character="&quot;" string="&amp;quot;"/>
    </xsl:character-map>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 4

Related Questions