Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Changes in XPath 2.0
The main change affecting arithmetic operators is the increased range of data types they can handle. In XPath 1.0, all numbers were handled as double-precision floating point, and the operators were not overloaded to handle any other data types.
In XPath 1.0, the arguments supplied to the function were automatically converted to numbers, using the rules for the
number()
function (which is described in Chapter 13). This means, for example, that the result of
1 + true()
would be
2
(
true()
converts to
1
), and the result of
“apple”+“pear”
would be
NaN
(any non-numeric string converts to the special not-a-number value
NaN
, and adding two
NaN
s gives
NaN
). These conversions are still carried out in XPath 2.0 if you run with backward compatibility enabled, which is what happens in an XSLT stylesheet that specifies
version = “1.0”
. The main advantage of this behavior is that you never get a runtime error (only
NaN
results), and if this is important to you, you can achieve the same effect in XPath 2.0 by using the
number()
function explicitly, even without backward compatibility enabled.
The
idiv
operator is new in XPath 2.0. It does integer division and is particularly useful when calculating how many rows and columns you need for a table; for example, if you have
$N
items to arrange in three columns, then the number of rows needed is
($N + 2) idiv 3
. Although the result is always an integer, the operands don't have to be integers: for example, the result of
3.6 idiv 1.5
is
2
. Another handy use of this operator is to convert any number
$x
to an integer by writing
$x idiv 1
.