首页 文章

xslt中的xpath产生与直接评估xpath不同的结果

提问于
浏览
3

我有这个简单的xml文档

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://example.org/attributes">
    <record>
        <codice_fiscale>IT07654930130</codice_fiscale>
        <indirizzo tipo="casa">Viale Carlo Espinasse 5, Como</indirizzo>
    </record>
    <n:record xmlns:n="http://test.com/records">
        <n:codice_fiscale>IT87654770157</n:codice_fiscale>
        <n:indirizzo tipo="ufficio">Via Biancospini 20, Messina</n:indirizzo>
    </n:record>
    <record>
        <codice_fiscale>IT471142131</codice_fiscale>
        <indirizzo tipo="ufficio">Via Chiasserini 88B, Firenze</indirizzo>
        <test>
            <nummeroo>01-000-000</nummeroo>
            <nummeroo>02-000-000</nummeroo>
        </test>
        <test>
            <nummeroo>03-000-000</nummeroo>
            <nummeroo>04-000-000</nummeroo>
        </test>
        <stuff>other stuff</stuff>
    </record>
    <things>
        <nummero>08-000-000</nummero>
        <nummero>09-000-000</nummero>
    </things>
</root>

以下xslt 2.0样式表应输出所有元素的名称,这些元素至少有一个叶节点作为子节点 .

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="no" />
    <xsl:strip-space elements="*"/>
    <xsl:template match="//*[child::*[not(*)]]">
        <xsl:value-of select="concat(name(), '&#xa;')" />
    </xsl:template>
</xsl:stylesheet>

我使用 omit-xml-declaration 并删除alle空格来获取简单的文本文档 . 与 '&#xa;' 连接给了我一个换行符 .

应用于上述xml文档,xslt产生以下结果

record
n:record
record
things

在样式表中使用模板的xpath 2.0表达式,即

//*[child::*[not(*)]]

与name()一起使用 - 将元素名称作为字符串获取,即

//*[child::*[not(*)]]/name()

我得到以下结果(使用不同的编辑器/ xpath 2.0评估程序)

record
n:record
record
test
test
things

使用xslt 2.0样式表并使用编辑器直接评估xpath 2.0表达式会产生不同的结果!

我希望结果完全一样 . 样式表和xpath //*[child::*[not(*)]]/name() 的直接评估基本相同,应该提供相同的文本输出 .

xpath表达式和样式表非常简单,但我无法弄清楚为什么两者的评估产生不同的结果 .

有谁知道为什么这两个评估产生不同的结果?

1 回答

  • 3

    匹配模式与select表达式不同 .

    样式表会发生什么:

    • 首先,内置模板规则应用于 / 根节点 .

    • 然后,使用内置递归,相同的内置模板将应用于 root 根元素 .

    • 接下来,您的模板将启动并处理作为根元素子节点的节点 - 此处处理将停止,因为您的模板不包含 xsl:apply-templates 指令 .

    请注意,匹配模式中将忽略前导 // .

相关问题