I'm working with an XSLT transformation, and I found an interesting question that I couldn't answer:
What's the difference between child::*
and child::node()
?
I want to create a condition in which I delimit the quantity of children elements to 1, in this case:
<xsl:if test="parent[count(child::*) eq 1])">
vs
<xsl:if test="parent[count(child::node()) eq 1])">
What would be the difference?
To understand the difference between child::*
and child::node()
in XPath, understand not only the difference between the *
and node()
node tests, but also the concept of the principal node type of an axis...
Rule: If an axis can contain elements, then its principal node type is element
; otherwise, it's the node type that the axis can contain. (For example, the principal node type of attribute
axis is attribute
because it can only contain attributes.)
The child
axis can contain elements, so the principal node type of the child axis is element
.
Therefore, the difference between child::*
and child::node()
is that
*
node test on the child
axis succeeds for all child elements of the
context node, because the *
node test succeeds for all nodes of the principal node type (element
, here) whereasnode()
node test succeeds for all child nodes of the
context node, because the node()
node test succeeds for all nodes types. However, note that not all nodes types can be on the child
axis. Here are the seven types of nodes and whether they can appear on the child axis:
Therefore, child::*
matches all element children of the context node, and child::node()
matches all all element, text, and processing instruction children of the context node.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With