Path expressions

Path expressions locate nodes within an XML tree. Path expressions in XQuery are based on the syntax of XPath 2.0.

A path expression consists of a series of one or more steps that are separated by a slash character (/) or two slash characters (//). The path can begin with a slash character (/), two slash characters(//), or a step. Two slash characters (//) in a path expression are expanded as /descendant-or-self::node()/ , which leaves a sequence of steps separated by a slash character (/). A step generates a sequence of items. The steps in a path expression are evaluated from left to right. The sequence of items that a step generates are used as context nodes for the step that follows. For example, in the expression description/name, the first step generates a sequence of nodes that includes all description elements. The final step evaluates the name element once for each description item in the sequence. Each time a name element is evaluated, it is evaluated with a different focus, until all name elements have been evaluated. The sequences that result from each evaluation of the step are combined, and duplicate nodes are eliminated based on node identity.

A slash character (/) at the beginning of a path expression means that the path is to begin at the root node of the tree that contains the context node. That root node must be a document node.

Restriction: In Db2, XQuery path expressions cannot contain the comma operator.

Recommendation: Because the slash character can be used as both an operator and an operand, use parentheses to clarify the meaning of the slash character when it is used as the first character of an operator. For example, to specify an empty path expression as the left operand of a multiplication operation use (/)*5 instead of /*5. The later expression causes an error. Because path expressions have the higher precedence, Db2 interprets this expression as a path expression with a wildcard for a name test (/*) that is followed by the token 5.

Two slash characters (//) at the beginning of a path expression establishes an initial node sequence that contains the root of the tree in which the context node is found and all nodes descended from this root. This node sequence is used as the input to subsequent steps in the path expression. That root node must be a document node.

The value of the path expression is the combined sequence of items that results from the final step in the path. This value is a sequence of nodes or an atomic value. A path expression that returns a mixture of nodes and atomic values results in an error.

A step consists of an axis step or a filter expression.

Syntax

path-expression
Read syntax diagramSkip visual syntax diagram /// step/step//step
step
Read syntax diagramSkip visual syntax diagramfilter-expressionaxis-step

Example

Use a path expression to determine which stocks have at least one bid for which the price is greater than the price of some offer on that stock.

//stock[bid/xs:double(price) > offer/price]/@stock_id