item means child::item so it returns children of my context node (that is //olist/). So, in the end it returns item nodes, children of all children of my starting context node or one of its descendent that have olist in their path.
Right?
That's almost correct - not only must the item
elements in the result set "olist
in their path" (which I understand as: must be a descendant of olist
) - they must be a direct child element of olist
.
Then let us clear the confusion about the "root node":
When Matt Jones uses "root node" in his explanation of //
, he does not mean the outermost element of his XML example. It is only by conincidence that the outermost element there is also called root
.
The "root node" or "document node" is different from the outermost element of an XML document. The outermost element is physically present in your document, and all XML elements and attributes are inside it.
The document node is not visible in the document, it's an abstract concept that represents an entire XML document. All nodes in an XML document are descendants of the document node.
On the other hand, not all nodes are descendants of the outermost element: for instance, there could be processing instructions or comments outside the outermost element. But those processing instructions and comments are contained by the document node.
Now, //
means either the context node or any of its descendants, as you know already. In this case, the document node will be the context of your path expression (if that's not what you want, prefix it with .
: .//
).
So the expression //item
means /descendant-or-self::node()/child::item
which means
select any element node with the name item
that is a child of a node anywhere in the XML structure
The parent of such an item
element will either be the document node (in this case the item
element will be the outermost element) or any element node.
In XPath 1.0, the abstract container for XML documents is called "root node", from XPath 2.0 onwards it is called "document node".
XPath 1.0 Specification, Abbreviated Syntax
"Document element and root node are often confused, but they are not the same thing"