Path, Predicates, and XQuery - Working with the Context Node
(Page 6 of 6 )
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.
a The detailed syntax of <element-attribute-test> is shown in Figure 13-4.
* Although several implementations erroneously support this construct.
* This is in contrast to XSLT, where several kinds of expressions change the context node, including the xsl:for-each element and template matching.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the book XQuery, written by Priscilla Walmsley (O'Reilly, 2007; ISBN: 0596006349). Check it out today at your favorite bookstore. Buy this book now.
|
|