Applying an XSLT Template for an XML Coded Value

I have an XML document that I need to convert to HTML. The XML content is as follows:

<root> <enc>Sample Text : &lt;d&gt;Hello&lt;/d&gt; &lt;e&gt;World&lt;/e&gt;</enc> <dec> Sample Text : <d>Hello</d> <e>World</e> </dec> </root> 

I need to apply a template for the value in the "enc" element, as I did for the "dec" element in the following xslt.

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" > <xsl:template match="root"> <html> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="dec"> <xsl:apply-templates/> </xsl:template> <xsl:template match="enc"> <xsl:value-of select="." disable-output-escaping="no" /> <br/> </xsl:template> <xsl:template match="d"> <b> <xsl:value-of select="."/> </b> </xsl:template> <xsl:template match="e"> <i> <xsl:value-of select="."/> </i> </xsl:template> </xsl:stylesheet> 

Actual output for the above XSLT:

Sample text: <d>Hello</d> <e>World</e>
Sample text: Hello World

Required Result:

Sample text: Hello World
Sample text: Hello World

Please help me convert the encoded xml value only with XSLT.

Thanks in advance.

+4
source share
1 answer

Since internal XML has been escaped, it is present as a single node text containing angle brackets, and not as a tree of nodes. Before you can process it using XSLT, you need to turn it into a tree of nodes. The process of converting XML-as-angle-brackets to XML-as-a-tree is called parsing, so you need to process this internal XML through an XML parser. There is no standard function for this in XSLT, but you can usually use it using processor-specific extensions: for example, saxon:parse() , if you are in Saxon.

+2
source

All Articles