Arithmetic expressions

Unary plus and minus

The unary minus operator changes the sign of a number. For example -1 is minus one, and -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

The operator * 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 promotion.

The operator 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 six.

The operator idiv performs integer division. For example, the result of 10 idiv 3 is 3.

The 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.

The operators * and 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

The operators + and - 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.