Home arrow XML arrow Page 4 - 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 - The position and last functions
(Page 4 of 4 )

The position and last functions are also useful when writing predicates based on position. The position function returns the position of the context item within the context sequence (the current sequence of items being processed). The function takes no arguments and returns an integer representing the position (starting with 1, not 0) of the context item. For example:

  doc("catalog.xml")/catalog/product[position() < 3]

returns the first two product children of catalog. You could also select the first two children of each product, with any name, using:

  doc("catalog.xml")/catalog/product/*[position() < 3]

by using the wildcard *. Note that the predicate [position() = 3] is equivalent to the predicate [3], so the position function is not very useful in this case.

When using positional predicates, you should be aware that the to keyword does not work as you might expect when used in predicates. If you want the first three products, it may be tempting to use the syntax:

  doc("catalog.xml")/catalog/product[1 to 3]

However, this will raise an error* because the predicate evaluates to multiple numbers instead of a single one. You can, however, use the syntax:

  doc("catalog.xml")/catalog/product[position() = (1 to 3)]

You can also use the subsequence function to limit the results based on position, as in:

  doc("catalog.xml")/catalog/subsequence(product, 1, 3)

The last function returns the number of nodes in the current sequence. It takes no arguments and returns an integer representing the number of items. The last function is useful for testing whether an item is the last one in the sequence. For example, catalog/product[last()] returns the last product child of catalog.

Table 4-7 shows some examples of predicates that use the position of the item. The descriptions assume that there is only one catalog element, which is the case in the catalog.xml example.

Table 4-7. Position in predicates (examples start with doc("catalog.xml")/catalog/)


The second product child of catalog

product[position() = 2]

The second product child of catalog

product[position() > 1]

All product children of catalog after the first one


The second to last product child of catalog


The last product child of catalog


The second child of catalog , regardless of name


The second child of the third product child of catalog

In XQuery, it's very unusual to use the position or last functions anywhere except within a predicate. It's not an error, however, as long as the context item is defined. For example, a/last() returns the same number as count(a).

Please check back next week for the conclusion to this article.

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