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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

Path, Predicates, and XQuery - More Complex Predicates
(Page 2 of 6 )

So far, the examples of predicates have been simple path expressions, comparison expressions, and numbers. In fact, any expression is allowed in a predicate, making it a very flexible construct. For example, predicates can contain function calls, as in:

  doc("catalog.xml")/catalog/product[contains(@dept, "A")]

which returns all product children whose dept attribute contains the letter A. They can contain conditional expressions, as in:

  doc("catalog.xml")/catalog/product[if ($descFilter)
                     then desc else true()]

which filters product elements based on their desc child only if the variable $descFilter is true. They can also contain expressions that combine sequences, as in:

  doc("catalog.xml")/catalog/product[* except number]

which returns all product children that have at least one child other than number. General comparisons with multiple values can be used, as in:

  doc("catalog.xml")/catalog/product[@dept = ("ACC", "WMN", "MEN")]

which returns products whose dept attribute value is any of those three values. This is similar to a SQL "in" clause.

To retrieve every third product child of catalog, you could use the expression:

  doc("catalog.xml")/catalog/product[position() mod 3 = 0]

because it selects all the products whose position is divisible by 3.

Predicates can even contain path expressions that themselves have predicates. For example:

  doc("catalog.xml")/catalog/product[*[3][self::colorChoices]]

can be used to find all product elements whose third child element is colorChoices. The *[3][self::colorChoices] is part of a separate path expression that is itself within a predicate. *[3] selects the third child element of product, and [self:: colorChoices] is a way of testing the name of the current context element.

Predicates are not limited to use with path expressions. They can be used with any sequence. For example:

  (1 to 100)[. mod 5 = 0]

can be used to return the integers from 1 to 100 that are divisible by 5. Another example is:

  (@price, 0.0)[1]

which selects the price attribute if it exists, or the decimal value 0.0 otherwise.


blog comments powered by Disqus
XML ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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