Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Examples of Rooted Paths
Expression | Description |
/price-list | Selects the document element within the current document, provided its name is Current document here and in the other examples means the tree containing the context node, assuming that the tree is rooted at a document node). |
/* | Selects the document element within the current document, whatever its name. |
/child::node() | Selects all nodes that are immediate children of the document root, that is, the document element plus any comments or processing instructions that come before or after the document element. (However, note that the version = “1.0”?> at the start of a document is not a processing instruction; in fact, it is not a node at all and is not accessible using XPath). |
/*/xsl:* | Selects all element nodes with names in the namespace associated with the xsl: namespace prefix that are immediate children of the document element. (If applied to an XSLT stylesheet, this would select all the top-level XSLT declarations). |
//figure | This path expression selects all the elements in the current document. |
The
//
Abbreviation
Colloquially,
//
in a path expression means “find all descendants”. More formally, whether it appears at the start of a path expression or as a binary operator, it is equivalent to writing
/descendant-or-self::node()/
.
I refer to
//
as a pseudo-operator because its semantics are described by means of a textual expansion into a different expression; this differs from a proper operator whose effect is described in terms of evaluating the operands and combining the results.
The expression
//A
is often used to select all
elements in the document.
How does this work? The expression
//
is equivalent to the rooted path
/descendant-or-self::node()/child::A
, which in turn (as we saw in the previous section) expands to
(/)/descendant-or-self::node()/child::A
. This selects all
elements whose parent is either the document node or a descendant of the document node, looking as always within the tree that contains the context node. Since every element has a parent that meets these criteria, it selects all
elements. Similarly,
//@B
means
/descendant-or-self::node()/attribute::B
, which selects all
B
attributes in the current document.