16.2 New XPath 2.0 Features

Following are just a handful of some of the new features added to the XPath 2.0 specification:


Improved terminology

XPath has tightened up its terminology, and a glossary will be available at the end of the specification in later drafts. For example, the result of an expression is now considered a sequence of zero or more items, and an item is either a node or an atomic value, such as an integer, as defined by XML Schema datatypes (see http://www.w3.org/TR/xmlschema-2/). This is much more than a terminology change. You can now have sequences of integers or strings (there are many more datatypes) as well as sequences of nodes.


New functions

XPath 2.0 has over 100 functions, compared with 27 in XPath 1.0 (I'm counting functions with the same name but different signatures or argument lists as one function). They are too numerous to list in this book, but you can peruse them in the new functions and operators specification (see http://www.w3.org/TR/xpath-functions/).


Strongly typed

XPath 2.0 has grown into a strongly typed language. It recognizes datatypes from XML Schema and also its own datatypes, such as xdt:anyAtomicType. See Section 2.4 of XPath 2.0.


New kind tests

New kind tests are now offered that test kinds of nodes, such as document-node( ), element( ), and attribute( ); for example, document-node( ) matches the document node (root node in XSLT 1.0). You can also test with empty( ) and item( ). The occurrence indicators ? (zero or one), * (zero or more), and + (one or more) are also in the mix; for example, item( )* matches zero or more atomic values or nodes. See 2.4 in XPath 2.0.


Sequences and ranges

Sequence expressions allow you to specify a sequence of items that can be atomic values or nodes; for example, (100, 101, 102) will return a sequence of the atomic values 100, 101, and 102, in that order. Range expressions let you represent a range of items; for example, (100 to 110) is a range from 100 to 110. See Section 3.3 of XPath 2.0. You can also combine sequences of nodes with the union, intersect, and except operators. See Section 3.3.2 of XPath 2.0.


Comparison

XPath 2.0 adds new comparison operators, such as eq, ne, lt, le, gt, and ge, but you can still use =, !=, < as &lt;, <= as &lt;=, >, and >=. The node comparison operators is and isnot have also been added as well. Also new are << and >>, which test the order of nodes. The new operators are stricter about the type conversions they allow, and they should be faster and safer as a result. Strong typing means your errors are more likely to be reported at compile time rather than simply give you the wrong output. See Section 3.5 of XPath 2.0.


For and conditional expressions

For expressions make it possible to process a range of values in one step. For example, sum(for $i in //item return $i/price * $i/quantity) computes the sum of the value of price times quantity over all items. See Section 3.7 in XPath 2.0. Also, you can now use a construct such as if (value[1] gt value[2]) then value[1] else value[2] in expressions. See Section 3.8 in XPath 2.0.


Quantified expressions

XPath 2.0 has new keywords such as some, every, and satisfies, which allow you to test for partial or complete compliance with a given item; for example, if (every $i in //item satisfies $i < 1000) then.... See Section 3.9 of XPath 2.0.


Working with types

You can now test whether an item is an instance of a type; you can cast as a type (change the type) and check whether an item is castable (its type can change); for example, if ($x castable as xs:date) tests whether the string in $x is a valid date; you can also treat as a type (meaning temporarily treat a type as another type).

This is by no means a complete review of all the changes and additions to XSLT 2.0 or XPath 2.0 it's just a quick discussion of a good number of them. These are working drafts; it is possible that they will change somewhat before they become recommendations. Fortunately, you can start playing with some of the new features today by using Saxon 7.7 (or later), which is an experimental implementation of XSLT 2.0 and XPath 2.0. The remaining sections of this chapter will try out some of these features, the first of which is result-document element.



Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net