Unary plus and minus
The unary minus operator changes the sign of a number. For example
-1 is minus
-0e0 is the double value negative zero.
Unary plus has very little effect: the value of
+1 is the same as the value of
1. It does, however, provide a quick way of forcing untyped values to be
numeric, for example you can write
<xsl:sort select="+@price"/> to force
a numeric sort, if you find
<xsl:sort select="number(@price)"/> too
verbose for your tastes.
Multiplication and division
* multiplies two numbers. If the operands are of different types,
one of them is promoted to the type of the other (for example, an integer is promoted to a
decimal, a decimal to a double). The result is the same type as the operands after
div divides two numbers. Dividing two integers produces a double;
in other cases the result is the same type as the operands, after promotion. In the case of
decimal division, the precision is the sum of the precisions of the two operands, plus
idiv performs integer division. For example, the result of
10 idiv 3 is
mod operator returns the modulus (or remainder) after division. See the
XPath 2.0 specification for details of the way that negative numbers are handled.
div may also be used to multiply or divide a
duration by a number. For example,
fn:dayTimeDuration('PT12H') * 4 returns the
duration two days.
Addition and subtraction
- perform addition and subtraction of
numbers, in the usual way. If the operands are of different types, one of them is promoted,
and the result is the same type as the operands after promotion. For example, adding two
integers produces an integer; adding an integer to a double produces a double.
Note that the
- operator may need to be preceded by a space to prevent it
being parsed as part of the preceding name.
XPath 2.0 also allows these operators to be used for adding durations to durations or to dates and times.