开发者

Apply XSLT Template for an Encoded XML Value

开发者 https://www.devze.com 2023-03-01 04:59 出处:网络
I have a XML document which I need to transform to HTML. XML Content is as开发者_JS百科 follows:

I have a XML document which I need to transform to HTML. XML Content is as开发者_JS百科 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消