What is the default select of XSLT apply-templates

2019-01-26 05:33发布

The identity template looks like this:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>

Does <xsl:apply-templates select="@*|node()" /> select more than <xsl:apply-templates />, or could the identity template have been like this?

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

What exactly is selected when I do the following?

<xsl:apply-templates />

标签: xslt xslt-2.0
3条回答
趁早两清
2楼-- · 2019-01-26 05:38

Does <xsl:apply-templates select="@*|node()" /> select more than <xsl:apply-templates />, or could the identity template have been like this?

<xsl:apply-templates/> 

is equivalent to:

<xsl:apply-templates select="node()"/>

and this is a shorter former of:

<xsl:apply-templates select="child::node()"/>

and this is a equivalent to:

<xsl:apply-templates select="* | text() | comment() | processing-instruction()"/>

As we see from the last instruction, the xsl:apply-templates instruction you are asking about, doesn't select any attributes, therefore it cannot be used as a shorthand for:

<xsl:apply-templates select="@*|node()"/>
查看更多
做个烂人
3楼-- · 2019-01-26 05:43

The default select for <xsl:apply-templates/> is just "node()", it doesn't include attributes.

查看更多
可以哭但决不认输i
4楼-- · 2019-01-26 05:59

The default selection of apply-templates is node(), which is shorthand for child::node(). This XPath expressions is evaluated as follows:

  • First, all nodes from the "child" axis are taken. This are all the direct children of the current element, i.e. other elements, text, and comments, but not the attributes.
  • Then this node set is filtered with the node test "node()". In this case, no elements are filtered because that test matches everything.

So with <xsl:apply-templates />, templates for the child elements are applied but not for the attributes. In case of the copy template this would mean that the attributes are not copied.

查看更多
登录 后发表回答