本文介绍了为编码的 XML 值应用 XSLT 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 文档,需要将其转换为 HTML.XML内容如下:

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>

我需要为enc"元素中的值应用一个模板,就像我在下面的 xslt 中为dec"元素所做的那样.

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>

上述 XSLT 的实际输出为:

Actual output for the above XSLT is:

示例文本:<d>Hello</d><e>世界</e>
示例文本:你好 世界

所需的输出是:

示例文本:你好 世界
示例文本:你好 世界

请帮助我仅在 XSLT 的帮助下转换编码的 xml 值.

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

提前致谢.

推荐答案

因为内部 XML 已经过转义,所以它以包含尖括号的单个文本节点的形式出现,而不是以节点树的形式出现.在您可以使用 XSLT 处理它之前,您需要将它变成一个节点树.将 XML-as-angle-brackets 转换为 XML-as-a-tree 的过程称为解析,因此您需要做的是通过 XML 解析器处理这个内部 XML.XSLT 中没有标准函数来执行此操作,但通常可以使用特定于处理器的扩展来完成:例如 saxon:parse() 如果你在撒克逊.

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.

这篇关于为编码的 XML 值应用 XSLT 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:22