XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (358 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
13.18Mb size Format: txt, pdf, ePub

Remember that if you are embedding your XPath expressions in an XML document—for example, an XSLT stylesheet or an XML Schema—then the
<
character must be escaped as
<
. Many people also like to escape
>
as
>
, though this is not strictly necessary.

Changes in XPath 2.0

Despite the special rules for handling backward-compatibility mode, the general comparison operators are probably the area where incompatibilities between XPath 1.0 and XPath 2.0 are most likely to be encountered. This is mainly because of the generalization of the data model to handle sequences, and also because of the increased range of data types. XPath 1.0 only supported four data types (string, number, boolean, and node-set). Given two operands, and allowing for symmetry, there were therefore 10 possible combinations of operand types, and each of these was described separately. Because there was little consistency to the XPath 1.0 rules, generalizing them to handle a much richer set of data types proved difficult.

The incompatibility that you are most likely to hit is when comparing strings, or untyped nodes. In XPath 1.0, an equality comparison (
=
) between two nodes (all nodes were untyped in those days) treated both values as strings, while an ordering comparison (
<
) treated them as numbers. This led to oddities such as the fact that
“2”=“2.0”
was false, while
“2”<=“2.0”
and
“2”>=“2.0”
were both true. This has been swept away in XPath 2.0; if you compare two strings, or untyped values, using any of these operators, then they are compared as strings, using the default collation defined in the XPath evaluation context. So if you have an expression such as
@discount < @max-discount
, and the element in question is

, then XPath 1.0 would return
true
, while XPath 2.0 returns
false
. The solution is to make sure that if you want a numeric comparison, you force it by converting the values explicitly to numbers, for example, by using the
number()
function (which is described in Chapter 13).

Other books

Kissed by a Cowboy by Lacy Williams
When He Dares by Emma Gold
Confessions by Jaume Cabré
The Sundial by Shirley Jackson
All That Glitters by Holly Smale
Code Breakers: Alpha by Colin F. Barnes
Total Immersion by Alice Gaines
Anatomy of a Murder by Robert Traver