Apply XSLT Template for an Encoded XML Value

2019-05-07 02:37发布

I have a XML document which I need to transform to HTML. 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 "enc" element like I have done for the "dec" element in 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 is:

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

The desired output is:

Sample Text : Hello World
Sample Text : Hello World

Please help me to transform the encoded xml value with the help of XSLT only.

Thanks in advance.

标签: xml xslt
1条回答
Fickle 薄情
2楼-- · 2019-05-07 02:49

Because the inner XML has been escaped, it is present as a single text node containing angle brackets, rather than 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 what you need to do is to process this inner XML through an XML parser. There's no standard function in XSLT to do this, but it can generally be done using processor specific extensions: for example saxon:parse() if you're in Saxon.

查看更多
登录 后发表回答