Rob
Rob

Reputation: 1255

How do I escape special characters in XSLT

I have the following XML being output from my XSLT

Current output

<stem>
    <text>
        <p style="white-space: pre-wrap">
            <span style="font-size:11;">How much would it cost to buy an Apple &amp; Pear if it's a Saturday?</span>
        </p>
    </text>
</stem>

Desired output

<stem>
    <text>
        <p style="white-space: pre-wrap">
            <span style="font-size:11;">How much would it cost to buy an Apple &amp; Pear if it&apos;s a Saturday?</span>
        </p>
    </text>
</stem>

I tried disable-output-escaping="no" but it had no effect:

I'm using

<xsl:value-of select="." disable-output-escaping="no"/>

Thanks

Upvotes: 1

Views: 9655

Answers (1)

Thorsten
Thorsten

Reputation: 61

If you are using XSLT 2 (as your use of Saxon suggests) there is a feature called character maps to fine-tune how your output is serialized. To serialize all apostrophes as &apos; use:

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

<xsl:output method="xml" use-character-maps="escape-apos"/>

Upvotes: 4

Related Questions