Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
855 views
in Technique[技术] by (71.8m points)

xml - What does "@*|node()" in a XSLT apply-template select mean?

I read some XSLT examples and found that code:

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

What does that mean?

question from:https://stackoverflow.com/questions/11167501/what-does-node-in-a-xslt-apply-template-select-mean

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The XPath expression @* | node() selects the union of attribute nodes (@*) and all other types of XML nodes (node()).

It is a shorthand for attribute::* | child::node().

In XSLT, XPath is relative to the context node and the default selection axis is the child axis, so the expression

  • selects all attributes and immediate children of the context node (when used as a select="..." expression, for example in <xsl:apply-templates>)
  • matches all attribute- and other nodes regardless of context (when used as a match="" expression in <xsl:template>) - note that there is a difference between selecting nodes and matching them: the context node only matters for selection.

Imagine the following node is the context node:

<xml attr="value">[
  ]<child />[
  ]<!-- comment -->[
  ]<child>
    <descendant />
  </child>[
]</xml>

the expression node() will not only select both <child> nodes, but also four whitespace-only text nodes (signified by [ and ] for the sake of visibility) and the comment. The <descendant> is not selected.

A special characteristic of XML is that attribute nodes are not children of the elements they belong to (although the parent of an attribute is the element it belongs to).

This asymmetric relationship makes it necessary to select them separately, hence the @*.

It matches any attribute node belonging to the context node, so the attr="value" will be selected as well.

The | is the XPath union operator. It creates a singe node set from two separate node-sets.

<xsl:apply-templates> then finds the appropriate <xsl:template> for every selected node and runs it for that node. This is the template matching part I mentioned above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...