XPath child::* vs child::node()
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...
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 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
.
Node Tests per Axis
Therefore, the difference between child::*
and child::node()
is that
- the
*
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) whereas - the
node()
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:- root: No, the root is the child of no other node, by definition.
- element: Yes
- text: Yes
- attribute: No, attributes have their own axis.
- namespace: No, namespaces have their own axis.
- processing instruction: Yes
- comment: Yes
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.
What's the difference between child::* and child::node().
To quote from here:
*
matches any elementnode()
matches any node other than an attribute node and the root node
So child::*
matches only child elements and
child::node()
matches any child nodes except attributes and the root node. These can be five of the seven node types: elements, comments, text and namespace nodes and processing instructions (see here).