Navigating Input Documents Using Paths - Steps and changing context (Page 2 of 4 )
The context item changes with each step. A step returns a sequence of zero, one, or more nodes that serve as the context items for evaluating the next step. For example, in:
the doc("catalog.xml") step returns one document node that serves as the context item when evaluating the catalog step. The catalog step is evaluated using the document node as the current context node, returning a sequence of one catalog element child of the document node. This catalog element then serves as the context node for evaluation of the product step, which returns the sequence of product children of catalog.
The final step, number, is evaluated in turn for each product child in this sequence. During this process, the processor keeps track of three things:
The context node itself--for example, the product element that is currently being processed
The context sequence, which is the sequence of items currently being processed--for example, all the product elements
The position of the context node within the context sequence, which can be used to retrieve nodes based on their position
As we have seen in previous examples, steps in a path can simply be primary expressions like function calls (doc("catalog.xml")) or variable references ($catalog). Any expression that returns nodes can be on the lefthand side of the slash operator.
Another kind of step is the axis step, which allows you to navigate around the XML node hierarchy. There are two kinds of axis steps:
Forward step This step selects descendents or nodes appearing after the context node (or the context node itself).
Reverse step This step selects ancestors or nodes appearing before the context node (or the context node itself).
In the examples so far, catalog, product, and @dept are all axis steps (that happen to be forward steps). The syntax of an axis step is shown in Figure 4-1.
Figure 4-1.Syntax of a step in a path expression
Each forward or reverse step has an axis, which defines the direction and relationship of the selected nodes. For example, the child:: axis (a forward axis) can be used to indicate that only child nodes should be selected, while the parent:: axis (a reverse axis) can be used to indicate that only the parent node should be selected. The 12 axes are listed in Table 4-2.
Table 4-2. Axes
The context node itself.
Children of the context node. Attributes are not considered children of an element. This is the default axis if none is specified.
All descendants of the context node (children, children of children, etc.). Attributes are not considered descendants.
The context node and its descendants.
Attributes of the context node (if any).
Table 4-2. Axes (continued)
All nodes that follow the context node in the document, minus the context nodes descendants.
All siblings of the context node that follow it. Attributes of the same element are not considered siblings.
The parent of the context node (if any). This is either the element or the document node that contains it. The parent of an attribute is its element, even though it is not considered a child of that element.
All ancestors of the context node (parent, parent of the parent, etc.).
The context node and all its ancestors.
All nodes that precede the context node in the document, minus the context nodes ancestors.
All the siblings of the context node that precede it. Attributes of the same element are not considered siblings.
An additional forward axis, namespace, is supported (but deprecated) by XPath 2.0 but not supported at all by XQuery 1.0. It allows you to access the in-scope namespaces of a node.
Implementations are not required to support the following axes: following, following-sibling, ancestor, ancestor-or-self, preceding, and preceding-sibling.