A Closer Look at Context

The processor can set the context node outside the query. Alternatively, the context node can be set by an outer expression. In XQuery, the only operators that change the context node are the slash and the square brackets used in predicates.[*] For example:

doc("catalog.xml")/catalog/product/(if (desc) then desc else name)

In this case, the if expression uses the paths desc and name. Because it is entirely contained in one step of another (outer) path expression, it is evaluated with the context node being the product element. Therefore, desc and name are tested as children of product.

In some cases, no context node is defined. This might occur if the processor does not set the context node outside the scope of the query, as described earlier in "Setting the Context Node Outside the Query" and there is no outer expression that sets the context. In addition, the context node is never defined inside the body of a function. In these cases, using a relative path such as desc raises an error.

Working with the Context Node

It is sometimes useful to be able to reference the context node, either in a step or in a predicate. A prior example retrieved product elements whose number child is less than 500 using the expression:

doc("catalog.xml")/catalog/product[number < 500]

Suppose, instead, you want to retrieve the number child itself. You can do this using the expression:

doc("catalog.xml")/catalog/product/number[. < 500]

The period (.) is used to represent the context node itself in predicates and in paths. You can also use the period as a parameter to functions, as in:

doc("catalog.xml")/catalog/product/name[starts-with(., "T")]

which passes the context item to the starts-with function. Some functions, when they are not passed any arguments, automatically use the context node. For example:

doc("catalog.xml")/catalog/product/desc[string-length( ) > 20]

uses the string-length function to test the length of the desc value. It was not necessary to pass the "." to the string-length function. This is because the defined behavior of this particular function is such that if no argument is passed to the function, it defaults to the context node.

Accessing the Root

When the context node is part of a complete XML document, the root is a document node (not the outermost element). However, XQuery also allows nodes to participate in tree fragments, which can be rooted at any kind of node.

There are several ways of accessing the root of the current context node. When a path expression starts with one forward slash, as in:

/catalog/product

the path is evaluated relative to the root of the tree containing the current context node. For example, if the current context node is a number element in the catalog.xml document, the path /catalog/product retrieves all product children of catalog in catalog.xml.

When a path expression starts with two forward slashes, as in:

//product/number

it is referring to any product element in the tree containing the current context node. Starting an expression with / or // is allowed only if the current context node is part of a complete XML document (with a document node at its root). / can also be used as an expression in its own right, to refer to the root of the tree containing the context node (provided this is a document node).

The root function also returns the root of the tree containing a node. It can be used in conjunction with path expressions to find siblings and other elements that are in the same document. For example, root($myNode)//product retrieves all product elements that are in the same document (or document fragment) as $myNode. When using the root function, it's not necessary for the tree to be rooted at a document node.



[*] This is in contrast to XSLT, where several kinds of expressions change the context node, including the xsl:for-each element and template matching.

Get XQuery now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.