Home arrow XML arrow Page 6 - Path, Predicates, and XQuery

Path, Predicates, and XQuery

In this conclusion to a three-part series on path expressions, you'll learn about complex predicates, dynamic paths, and more. This article is excerpted from chapter four of the book XQuery, written by Priscilla Walmsley (O'Reilly, 2007; ISBN: 0596006349). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 8
March 06, 2008
  1. · Path, Predicates, and XQuery
  2. · More Complex Predicates
  3. · Dynamic Paths
  4. · Accessing a Single Document
  5. · Setting the Context Node Outside the Query
  6. · Working with the Context Node

print this article

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:


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:


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.

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials