xslt apply-templates选择所有剩余的文本节点

时间:2009-05-13 18:52:00

标签: xslt

我有这个简化的xml:

<?xml version="1.0" encoding="UTF-8"?>

<a>
    <b>
        <c>
            <d>1</d>
            <e>2</e>
        </c>
    </b>
    <f>
        <g>3</g>
    </f>
</a>

这是我尝试申请的xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="a">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="b">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="c">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="d">

    </xsl:template>

</xsl:stylesheet>

当我应用此工作表时,我得到输出2 3,这是剩余的文本节点。我已经读过如果找不到匹配的模板就会应用的内置模板,但在这种情况下,它应该找到一个模板吗?

发生了什么事?

修改

在这种情况下,我希望什么都看不到,因为模板是空的。但我得到了2 3。

2 个答案:

答案 0 :(得分:1)

执行<xsl:template match="d">时,您告诉处理者忽略<d>下的所有节点。

使用默认规则处理所有其他节点,包括text()一个,即打印文本。

这就是为什么你看到23而不是1。

答案 1 :(得分:0)

从根开始:

<xsl:template match="/a">

并指定一种模式(这样就不会调用默认模板,因为它找到ef和{{1}的模板})或定义自己的*模板,它在样式表的末尾不执行任何操作:

g