XML technologies / XPath / XPath 2.0 – Language reference / Comparing XPath 1.0 and XPath 2.0

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 4.)

XPath 2.0 is much larger than XPath 1.0, both in its built-in function count and in the possibilities for writing expressions.

Similarities and differences

XPath 2.0 permits decisions and loops as control structures within expressions, greatly simplifying some problems or making them possible for the first time compared with XSLT 1.0/XPath 1.0. Unlike XPath 1.0, however, XPath 2.0 is no longer an independent language but a subset of XQuery 1.0.

Aside – What is XQuery?
XQuery 1.0 arose from efforts to develop an XML-compatible general query language for XML documents with functionality comparable to SQL. Every valid XPath 2.0 expression is therefore also valid in XQuery 1.0 and produces the same result. The reverse is not true, since not all XQuery features are available in XPath or, consequently, in XSLT.

Similarities between XPath 1.0 and XPath 2.0

  • XPath is purely functional. An XPath expression can therefore combine other XPath expressions arbitrarily. For example, it can contain a function call whose argument is another XPath expression.

  • XPath is case-sensitive. All names, including keywords, operators and function names, must use the exact specified spelling, usually lowercase. Namespaces and QNames are supported for all names.

Differences between XPath 1.0 and XPath 2.0

Unordered sets versus ordered sequences:

  • XPath 1.0 is based on sets: an expression creates a collection (set) of nodes. Such sets are unordered but have no duplicates; a node cannot occur more than once.

  • XPath 2.0 is based on sequences: an expression creates a succession of items, nodes or atomic values. Such a sequence is ordered and may contain duplicates; a node can occur several times.

Data types – Loose versus strong typing:

  • XPath 1.0 is loosely typed (loose typed): values are converted automatically if they do not match a required input type, for example a function’s. There are only four types (string, num­ber, boolean, node-set).

  • XPath 2.0 is strongly typed (strong typed): expression operands and function arguments must match their required types, otherwise a type error occurs. All data types from XML Schema are supported, giving approximately 45 distinguishable types in XPath 2.0.

XML Schema support:

  • XPath 1.0 does not support schema types supplied by an external XML schema. Only partial information from a DTD, such as definitions of ID attributes, can be used.

  • XPath 2.0 can work directly with schema types if a schema is accessible, for example by import. Users can also define their own data types or types derived from base types (derived types) in XML Schema for use by XPath 2.0.

XPath 2.0 backward compatibility with XPath 1.0

XPath 2.0 has two compatibility modes for XPath 1.0. The mode is recorded in the static evaluation context’s XPath Compatibility Mode component.

This mode applies globally, unchanged throughout a stylesheet execution. It can have value true for evaluation backward-compatible with XPath 1.0, or false for evaluation under XPath 2.0 rules.

Three possible kinds of incompatibility must be distinguished: 

  1. Incompatibility with backward compatibility disabled, XPath 1.0 compatibility mode set to false.

  2. Incompatibility without a source-document schema and with backward compatibility enabled, XPath 1.0 compatibility mode set to true.

  3. Incompatibility with a source-document schema, in either compatibility mode, true or false.

Incompatibilities with compatibility mode false

With compatibility mode set to false, backward compatibility, meaning identical results, is not explicitly required and need not be guaranteed. Many XPath expressions nevertheless produce identical results in XPath 2.0 and 1.0. Exceptions need attention, but simple methods usually restore identical results.

Note: In the descriptions below, node-set in XPath 1.0 and node sequence in XPath 2.0 can be treated as analogous.

Function or operator expects a single-node argument

Problem: If a function or operator expecting a single-node node-set receives multiple nodes, XPath 1.0 discards all except the first and evaluates only that node. XPath 2.0 instead reports a type error for the same expression.

Solution: Applying an additional numeric predicate $sequenz[1]  to the node sequence makes XPath 2.0 evaluate only the first node too, restoring identical results.

Empty node-set passed to an arithmetic or string function

Problem: Loosely typed XPath 1.0 automatically converts an empty node-set into an empty string or numeric value NaN, depending on the function’s needs. XPath 2.0 does not implicitly convert an empty sequence in this way.

Solution: Functions fn:string() or fn:number() force explicit conversion in XPath 2.0. In XPath 1.0 this is redundant but harmless.

Incompatibilities with compatibility mode true

Even in compatibility mode true, XPath 2.0 evaluates expressions differently from XPath 1.0. The following assumes that the expression is also valid in XPath 1.0.

Whitespace required between operator and operand

XPath 1.0 does not always require a space between a number and a following operator when the operation is unambiguous. XPath 2.0 requires one. For example, expression 10div 2 is permitted in XPath 1.0, but in XPath 2.0 it must be written 10 div 2.

Converting strings to numbers

XPath 2.0 changes the rules for converting strings to numbers compared with XPath 1.0.

For special values such as INF and -INF, XPath 2.0 follows XML Schema. Signed digit strings with + and strings in scientific exponential notation can now also be converted into actual numbers.

XPath 1.0 permits only a leading minus sign and forbids letters inside the string, including e or E. Such strings always evaluate to NaN  in XPath 1.0.

Chained comparisons forbidden in XPath 2.0

XPath 1.0 permits chained comparisons such as A < B < C or A = B = C. XPath 2.0 no longer permits these and treats them as syntax errors. Such expressions can, however, be rewritten, including internally by the processor, as (A < B) < C.

No namespace axis in XPath 2.0

The namespace axis should no longer be used in XPath 2.0 (deprecated). Implementations may continue supporting it for backward compatibility with XPath 1.0, but are not required to do so.

XPath 2.0 provides functions that make the namespace axis essentially unnecessary: fn:name­space-from-QName(), fn:namespace-uri-for-prefix() and fn:in-scope-prefixes().

XQuery 1.0 does not support the namespace axis either.

Incompatibilities when using a schema

XPath 2.0 can use data-type information from a schema, unlike XPath 1.0. Whether a schema is present can therefore affect an expression’s result in XPath 2.0, whereas it makes no difference in XPath 1.0. With a schema present, the same expression may thus produce different results in XPath 1.0 and 2.0.

For example, an attribute value defined in a schema as list type xs:string* can be tested in a predicate for one of its component values. Attribute value farbe="rot gelb blau" tested with predicate [@farbe="gelb"] yields false in XPath 1.0, but true in XPath 2.0 with the corresponding schema. Without the schema it also returns false.

Another example is date values, which in XPath 2.0 have type xs:date when a schema is available, but can only be treated as strings in XPath 1.0:

For element <person geburtstag="1950-07-01" todestag="2000-06-03"/>, test @geburtstag < @todestag  therefore yields false in XPath 1.0, because both operands must be converted to numbers and yield NaN. XPath 2.0, by contrast, recognizes both values as dates xs:date and performs the comparison correctly.

   

back to top