代码之家  ›  专栏  ›  技术社区  ›  Ikke

xslt应用模板选择所有剩余的文本节点

  •  0
  • Ikke  · 技术社区  · 17 年前

    <?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,这是剩余的textnodes。我读过关于内置模板的内容,如果找不到匹配的模板,就会应用这些模板,但是在这种情况下,它应该找到一个模板吗?

    发生了什么事?

    编辑:

    在本例中,我希望什么也看不到,因为模板是空的。但我得到了23个。

    2 回复  |  直到 17 年前
        1
  •  1
  •   0x6adb015    17 年前

    当你这样做的时候 <xsl:template match="d"> ,告诉处理器忽略下的所有节点 <d> .

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

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

        2
  •  0
  •   Lucero    17 年前

    从根开始:

    <xsl:template match="/a">
    

    并指定模式(这样默认模板不会被调用,因为它会被调用) 查找的模板 e f g )或者定义您自己的*模板,该模板在样式表末尾不起任何作用:

    <xsl:template match="*"/>