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::*
andchild::node()
in XPath, understand not only the difference between the*
andnode()
node tests, but also the concept of the principal node type of an axis...Principal Node Type
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 ofattribute
axis isattribute
because it can only contain attributes.)The
child
axis can contain elements, so the principal node type of the child axis iselement
.Node Tests per Axis
Therefore, the difference between
child::*
andchild::node()
is that*
node test on thechild
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 thenode()
node test succeeds for all nodes types. However, note that not all nodes types can be on thechild
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, andchild::node()
matches all all element, text, and processing instruction children of the context node.