XML
  Home arrow XML arrow Page 2 - Path, Predicates, and XQuery
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Path, Predicates, and XQuery
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-03-06

    Table of Contents:
  • Path, Predicates, and XQuery
  • More Complex Predicates
  • Dynamic Paths
  • Accessing a Single Document
  • Setting the Context Node Outside the Query
  • Working with the Context Node

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More XML Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "XQuery," published by O'Reilly. We hope...
     

    Buy this book now. 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.

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek