XML technologies / XPath / XPath 2.0 – Language reference / Path expressions: location steps / The axis step

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 4.)

Starting at the context node, an axis step initially selects all nodes on its named axis. There may be any number of nodes, including none, ordered in axis direction, thus in or against document order depending on the axis.

From this initial set, only nodes passing one of the XPath node tests just described (node tests) are retained. The underlying axes have also already been explained.

Every possible axis step has a complete form. Some frequently used steps also have abbreviated syntax.

Axis step: unabbreviated syntax

Complete, or more accurately unabbreviated, syntax (unabbreviated syntax) uses the full axis name followed by a double colon within an axis step. Schematically:

achsenname::knotentest

This syntax works for all thirteen axes. A frequently used subset also has abbreviations; see abbreviated syntax.

Axis names with double colons are also followed by a node test identifying the node kind to select on that axis.

Examples:

a) Simple path expressions

A simple path expression consists of one step comprising an axis name and a node test.

child::* 
selects all element nodes on the context node’s child axis, regardless of name.

child::mein_element
selects all element nodes named mein_element that are children of the context node.

child::text() 
selects all text nodes that are children of the context node.

child::node() 
selects all children of the context node regardless of kind or name: element, text, comment and processing-instruction nodes.

attribute::mein_attribut 
selects the context node’s attribute node named mein_attribut, at most one node.

parent::* 
selects the context node’s parent, an element or document node. If the context node is an attribute, it returns the element to which that attribute belongs.

descendant::mein_element
selects all element nodes named mein_element on the context node’s descendant axis, hence its descendants.

ancestor::mein_element
selects all element nodes mein_element that are ancestors of the context node.

ancestor-or-self::mein_element
selects all element nodes mein_element that are ancestors of the context node. If the context node itself is an element node mein_element, it is selected too.

descendant-or-self::mein_element
selects all element nodes mein_element that are descendants, children or grandchildren, of the context node. If the context node itself is an element node mein_ele­ment, it is selected too.

self::mein_element
selects the context node if it is an element node mein_element. Otherwise nothing is selected.

b) Compound path expressions

Compound path expressions must specify an axis for each step, which may therefore change from step to step.

child::kapitel/descendant::abschnitt
selects all element nodes abschnitt that are descendants of an element kapitel that is itself a child of the context node.

child::*/child::abschnitt
selects all element nodes abschnitt that are grandchildren of the context node. The intermediate element’s name is irrelevant, using a wildcard, but exactly one intermediate step is required.

/descendant::abschnitt 
selects all elements abschnitt in the same document tree as the context node. If the context node itself is an element abschnitt, it is selected too.

/descendant::abschnitt/child::ueberschrift
selects all element nodes ueberschrift that are children of an element abschnitt in the same document tree as the context node.

Axis steps: abbreviated syntax

Besides the universally usable full syntax, some important axes have abbreviations replacing the axis name. The following axes can be abbreviated, or have related abbreviations representing a complete axis step:

  • self axis
    Placeholder “.”, a dot, represents the context item. Within path expressions it corresponds to axis step self::node(), but elsewhere can denote either a node, the context node, or an atomic context item. Thus, “.” is actually not an abbreviation for the axis itself.

  • child axis
    The child axis name can simply be omitted from axis steps because child is the default axis. Instead of child::mein_element, write mein_element; instead of child::node(), write node(). Abbreviated syntax for the child axis thus consists only of the node test.

  • parent axis
    Placeholder “..”, two dots, represents axis step parent::node(). This expression is therefore not an actual abbreviation of the axis name. Expression ../titel thus abbreviates parent::node()/child::titel and selects all sibling nodes, children of the same parent, named titel, plus the context node itself if it is an element titel.

  • attribute axis
    The attribute axis name can be replaced within an axis step by “@”. Instead of attribute::mein_attribut, simply write @mein_attribut; instead of attribute::*, write @*. Abbreviated syntax for the attribute axis thus consists of @ and a node test.

  • descendant-or-self axis
    Placeholder “//”, two slashes, represents axis step descendant-or-self::node(). Within a compound path expression, abbreviation // effectively expands to /descendant-or-self::node()/. At the start of the path expression, it makes the path absolute; see the formal discussion of / and // below. Thus the double slash is not directly an abbreviation of the descendant-or-self axis itself.

Examples:

mein_element 
selects all children mein_element of the context node.

*
selects all element children of the context node, regardless of name.

text() 
selects all children of the context node that are text nodes.

@mein_attribut
selects the attribute named mein_attribut belonging to the context node.

kapitel[fn:last()] 
selects the last element kapitel in document order that is a child of the context node.

kapitel//abschnitt 
selects all element nodes named abschnitt descending from an element kapitel that is a child of the context node.

/
selects the document node at the bottom of the hierarchy of the tree containing the context node; see the formal discussion of / and // below.

//abschnitt 
selects all element nodes abschnitt within the document, descendants of the document node. If the context node is an element abschnitt, it is selected too.

.//abschnitt 
selects all element nodes abschnitt that are descendants (descendants) of the context node. The dot representing the context node is indispensable here. The unabbreviated expression would be self::node()/descendant-or-self::node()/abschnitt.

buch/(kapitel | anhang)/abschnitt 
selects all element nodes abschnitt that are children of an element kapitel or anhang that is a child of an element buch that is itself a child of the context node.

Formal discussion of / and //

A slash / at the start of a path expression is an abbreviation, as is a double slash //. At the start of a path expression, the latter also starts at the document node and includes that node (descendant-or-self). From there, it reaches arbitrarily far down the descendant axis into the document tree, necessarily including the current node.

The single slash:

An initial or standalone slash / is not actually the path operator used inside path expressions, but an abbreviation for the document node, formally the step:

fn:root(self::node()) treat as document-node()

Explanation:
Function fn:root() finds a tree’s root node. (The root found by fn:root() is not always a document node, hence the following subexpression.) The context node passed as its argument through expression self::node() belongs to this tree. The root found is additionally required to be treated as a document node, treat as document-node(), and the path expression starts there. The context node’s position in the document therefore does not affect where the expression is anchored.

Note – No type conversion through treat as:
Expression treat as is only a static assertion (assertion). If the node found is subsequently not a document node, a runtime error occurs (err:XPDY0050). No conversion is performed by treat as.

The double slash:

Like the expression just explained, abbreviation // represents the full step sequence:

fn:root(self::node()) treat as document-node()/descendant-or-self::node()

Explanation:
The double slash // stands for axis descendant-or-self and every node node() at its end: descendant-or-self::node(). At the start of a path expression, it also denotes an absolute path, so the full step for a single slash is prepended. Put simply, // in //step_1 assembles an initial sequence of all nodes in the document tree as context for evaluating step_1. Every node in the tree is therefore checked in turn against this step.

   

back to top