user2628187
user2628187

Reputation: 307

Convert encoding with XSLT

I have the below in XML and i'm trying to transform this in to some output file say a HTML

Is it possible to convert the & to & in the output file?

<Property>
  <name>Document Title</name>
  <value>Me &amp; you</value>   
</Property>

Upvotes: 0

Views: 225

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

As you have tagged the question as XSLT 2.0, if you really want an unescaped ampersand & in the HTML output then you can use a character map:

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

    <xsl:output method="html" use-character-maps="m1"/>

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

    <xsl:template match="/">
        <html>
            <body>
                <p>This is a test of the encoding of the <code>&amp;</code> character.</p>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Output is

<html>
   <body>
      <p>This is a test of the encoding of the <code>&</code> character.
      </p>
   </body>
</html>

But in HTML as in XML the ampersand is usually escaped as &amp; for a reason and if your XSLT processor escapes it in HTML output then it is usually implementing HTML syntax rules.

Upvotes: 1

Related Questions