Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath child::* vs child::node()

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?

like image 826
Cuauh Medina Avatar asked Sep 08 '17 17:09

Cuauh Medina


1 Answers

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 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) whereas
  • the node() 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:
    • 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.

like image 157
kjhughes Avatar answered Oct 11 '22 15:10

kjhughes