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

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

The unary minus operator is defined to change the sign of the operand. This is subtly different from subtracting the operand from zero, because it means that
-0e0
represents negative zero rather than positive zero. There's very little difference between the two: about the only way of telling them apart is by a test such as
1 div $x > 0
, which returns true if
$x
is positive zero (the division gives positive infinity), but false if
$x
is negative zero. For practical purposes, the distinction between the two values is rarely important, and it is lost once you write the results away to an XML document. Its only significance is that it preserves a useful difference in the intermediate results of complex calculations.

Floating-point arithmetic can always give you rounding errors, because there are values that can be written accurately in decimal notation that can't be expressed accurately in binary. So, for example, the result of the expression
1.0E-3 * 1.0E-4
might not be displayed as
1.0E-7
as you would expect, but as
1.0000000000000001E-7
. You can round it to the number of decimal places required using the
round-half-to-even()
function, or in XSLT, by using the
format-number()
function. Both are described in Chapter 13.

Examples of Numeric Arithmetic

Expression
Description
$X + 1
The result of adding 1 to the value of the variable
$X
.
last()-1
One less than the position of the last node in the context list.
@margin*2
Twice the value of the
margin
attribute of the context node. This will work only if the margin attribute either has a numeric type, or is untyped and has a value that can be interpreted as a number.
ceiling(count (item) div 3)
One-third of the number of child

elements of the context node, rounded upwards. (Useful if you are arranging the items in three columns).
$seq[position() <=last() idiv 2]
Selects the first half of the items in the sequence
$seq
, rounded down. For example, if there are 11 items in the sequence, it selects the first five.
item[position() mod 2 = 0]
Selects the even-numbered child

elements of the context node. (Again, this can be useful if you are arranging items in a table).
count($list) mod 5 + 1
The number of items in the sequence
$list
modulo 5, plus one. The result will be a number in the range 1 to 5.
-@credit
The negated numeric value of the
credit
attribute of the context element node. If the context node has no
credit
attribute, or if its value is not numeric, the result of the expression is
()
(the empty sequence), unless backward-compatibility mode is set, in which case it is
NaN
(not a number).
+@credit
The plus sign here is not a no-op; it forces conversion of the attribute node to a number. It also tells the compiler that the result will be a number, which can be useful when doing static typing and optimization.
1---1
A not very useful but perfectly legal way of writing the value zero. The first minus sign is a binary subtraction operator; the next two are unary minus signs.

Other books

Stress Relief by Evangeline Anderson
Perfect Submission by Roxy Sloane
The Malady of Death by Marguerite Duras
Addicted After All by Krista Ritchie, Becca Ritchie
Secrets of a First Daughter by Cassidy Calloway
The Siren of Paris by David Leroy
The Ball Hogs by Rich Wallace