Wednesday, 24 September 2014

Encode XML elements with XSLT



I have a requirement to create the following structure using XSLT:



<body>
&lt;p&gt;&lt;span&gt;This is a test&lt;/span&gt;&lt;/p&gt;
</body>


Currently my "solution" (which I hope can be used) is this:



<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="//body">
<body>
<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
<xsl:apply-templates select="@*|node()" />
<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
</body>

</xsl:template>


I have tried the following which did not work:



<xsl:template match="foo">
<xsl:text>&lt;p&gt;</xsl:text>
<xsl:apply-templates select="@*|node()" />
<xsl:text>&lt;/p&gt;</xsl:text>
</xsl:template>


Any suggestion appreciated.


No comments:

Post a Comment