Get started with XPath 2.0

Home arrow XML Tutorials arrow XPath arrow Get started with XPath 2.0
Get started with XPath 2.0 Print E-mail
Contributed by Howell   
Monday, 05 June 2006
 A major update to a familiar standard

Although still a Candidate Recommendation, XPath 2.0 is moving towards formal approval. The first update to the XPath recommendation since 1999 is eagerly anticipated by the market and, indeed, several tools have already begun to implement the latest drafts. The changes are so fundamental, I expect that in time the world will come to see XPath 1.0 as a draft for XPath 2.0.

The XPath 2.0 recommendation serves as the basis for XSLT 2.0 and XQuery 1.0. Both languages use XPath as their core querying engine and augment it with statements to format the results (see Resources).

The many changes between XPath 1.0 and XPath 2.0 include:

  • A new data model, based on sequences instead of node sets
  • The ability to bind variables; previously variables were bound in the host language (XSLT)
  • Full support for XML Schema datatypes
  • Many new functions, including regular expression, data/time, and string manipulations
    Comments; although not a major feature, they are handy when you debug queries: just comment out portions of the path for testing
Sequences in XPath 2.0

XPath 2.0 processes anything as a sequence. A sequence is an ordered heterogeneous collection of items. The items can be either nodes from an XML document or atomic values. Atomic values can be any type defined in the XML Schema recommendation, including complex types. To declare a sequence in an XPath, just separate the items with commas and enclose the whole sequence in parentheses:

(2, 'declencheur', 5.10)

In practice, almost every valid XPath 1.0 request remains valid in XPath 2.0. In other words, XPath 2.0 retains the familiar XPath 1.0 syntax: a path still is made of location steps separated by a forward slash (/), such as:

/po:PurchaseOrder/po:ProductList/po:Name.

However, the location steps in XPath 2.0 identify items in a sequence (again, those items might be XML nodes) instead of nodes in a tree (the XPath 1.0 data model).

Every concept in XPath 2.0 has been reworked around sequences. For example, functions that expected node sets in XPath 1.0 now work with sequences.

Given that XML documents are hierarchical, the XPath 1.0 model (a tree structure) is sensible. But it is also limiting because XPath cannot generate trees and, therefore, it is impossible to pass the result from a request to another request for further processing. Complex requests, à la SQL, are impossible to write.

Using sequences

As noted earlier, the XPath 1.0 syntax remains in use, but XPath 2.0 also introduces several new statements specifically to work with sequences. I will first review the for expression which, as the name implies, loops over the items in a sequence.

A typical for expression looks like Listing 1:

 

Listing 1. XPath 2.0 sample

for $line in /po:PurchaseOrder/po:OrderLines/po:Line
   return $line/po:Price * $line/po:Quantity


The preceding XPath would be executed against a purchase order like Listing 2. It computes the totals of each order line and returns the following sequence:

(29.99, 89.98, 80, 3.1)

 

 


Last Updated ( Monday, 03 July 2006 )

  home              contact us

 

©2006-2009 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground