Home arrow XML arrow Page 3 - Using Predicates with XQuery

Using Predicates with XQuery

In this second part of a three-part series on extracting information from XML documents, you'll learn how to use predicates in path expressions. 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: 5 stars5 stars5 stars5 stars5 stars / 6
February 28, 2008
  1. · Using Predicates with XQuery
  2. · Comparisons in Predicates
  3. · Using Positions in Predicates
  4. · The position and last functions

print this article

Using Predicates with XQuery - Using Positions in Predicates
(Page 3 of 4 )

Another use of predicates is to specify the numeric position of an item within the sequence of items currently being processed. These are sometimes called, predictably, positional predicates. For example, if you want the fourth product in the catalog, you can specify:


Any predicate expression that evaluates to an integer will be considered a positional predicate. If you specify a number that is greater than the number of items in the context sequence, it does not raise an error; it simply does not return any nodes. For example:


returns the empty sequence.

Understanding positional predicates

With positional predicates, it is important to understand that the position is the position within the current sequence of items being processed, not the position of an element relative to its parent's children. Consider the expression:


This expression refers to the first name child of each product; the step name[1] is evaluated once for every product element. It does not necessarily mean that the name element is the first child of product.

It also does not return the first name element that appears in the document as a whole. If you wanted just the first name element in the document, you could use the expression:


because the parentheses change the order of evaluation. First, all the name elements are returned; then, the first one of those is selected. Alternatively, you could use:


because the sequence of descendants is evaluated first, then the predicate is applied. However, this is different from the abbreviated expression:


which, like the first example, returns the first name child of each of the products. Thats because it's an abbreviation for:


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-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials