Data model
Every value in XPath 2.0 is a ''sequence'' of ''items''. The items may be ''nodes'' or ''atomic values''. An individual node or atomic value is considered to be a sequence of length one. Sequences may not be nested. Nodes are of seven kinds, corresponding to different constructs in the syntax of XML: elements, attributes, text nodes, comments, processing instructions, namespace nodes, and document nodes. (The document node replaces the ''root node'' ofType system
The type system of XPath 2.0 is noteworthy for the fact that it mixes@price * 1.2
), then the node is automatically ''atomized'' to extract the atomic value. If the input document has been validated against a schema, then the node will typically have a type annotation, and this determines the type of the resulting atomic value (in this example, the price
attribute might have the type decimal
). If no schema is in use, the node will be untyped, and the type of the resulting atomic value will be untypedAtomic
. Typed atomic values are checked to ensure that they have an appropriate type for the context where they are used: for example, it is not possible to multiply a date by a number. Untyped atomic values, by contrast, follow a weak typing discipline: they are automatically converted to a type appropriate to the operation where they are used: for example with an arithmetic operation an untyped atomic value is converted to the type double
.
Path expressions
The ''location paths'' of/
" operator, for example a/b/c
(which is short for child::a/child::b/child::c
). More formally, however, "/
" is simply a binary operator that applies the expression on its right-hand side to each item in turn selected by the expression on the left hand side. So in this example, the expression a
selects all the element children of the context node that are named <a>
; the expression child::b
is then applied to each of these nodes, selecting all the <b>
children of the <a>
elements; and the expression child::c
is then applied to each node in this sequence, which selects all the <c>
children of these <b>
elements.
The "/
" operator is generalized in XPath 2.0 to allow any kind of expression to be used as an operand: in XPath 1.0, the right-hand side was always an axis step. For example, a function call can be used on the right-hand side. The typing rules for the operator require that the result of the first operand is a sequence of nodes. The right hand operand can return either nodes or atomic values (but not a mixture). If the result consists of nodes, then duplicates are eliminated and the nodes are returned in ''document order'', an ordering defined in terms of the relative positions of the nodes in the original XML tree.
In many cases the operands of "/
" will be axis steps: these are largely unchanged from XPath 1.0, and are described in the article on Other operators
Other operators available in XPath 2.0 include the following: Conditional expressions may be written using the syntax . XPath 2.0 also offers afor
expression, which is a small subset of the FLWOR expression from XQuery. The expression evaluates the expression Y
for each value in the result of expression X
in turn, referring to that value using the variable reference $x
.
Function library
The function library in XPath 2.0 is greatly extended from the function library in XPath 1.0. (Bold items are available in XPath 1.0) The functions available include the following:Backwards compatibility
Because of the changes in the data model and type system, not all expressions have exactly the same effect in XPath 2.0 as in 1.0. The main difference is that XPath 1.0 was more relaxed about type conversion, for example comparing two strings ("4" > "4.0"
) was quite possible but would do a numeric comparison; in XPath 2.0 this is defined to compare the two values as strings using a context-defined collating sequence.
To ease transition, XPath 2.0 defines a mode of execution in which the semantics are modified to be as close as possible to XPath 1.0 behavior. When using XSLT 2.0, this mode is activated by setting version="1.0"
as an attribute on the xsl:stylesheet
element. This still doesn't offer 100% compatibility, but any remaining differences are only likely to be encountered in unusual cases.
Support
Support for XPath 2.0 is still limited.References
External links