XML technologies / XPath / XPath 2.0: language reference / XPath expressions: operators and keywords / Arithmetic expressions

(Excerpt from "XSLT 2.0 & XPath 2.0" by Frank Bongers, chapter 4.)

Operators: +, –, * div, idiv, mod

XPath provides all common binary and unary arithmetic operators. An arithmetic expression combines two XPath expressions, which may themselves be compound, using one of the operators listed above.

An arithmetic expression produces a numeric value (numeric, that is, one of XPath’s numeric types).

Depending on the operators, expressions are classified as multiplicative (operators: *, div, idiv and mod) or additive (operators: +, -) expressions.

Arithmetic operators follow precedence rules: two multiplicative expressions can be combined into an additive expression without additional parentheses, since multiplication takes precedence over addition:

ausdruck * ausdruck + ausdruck div ausdruck

Parentheses allow lower-precedence subexpressions to be evaluated first:

ausdruck * (ausdruck + ausdruck) div ausdruck

Additive expressions

The two additive operators, + and –, perform addition and subtraction as usual. If the operands have different numeric types, one operand undergoes appropriate type promotion. The result has the type shared by both operands after promotion.

Thus, when an integer of type xs:integer is added to a floating-point number of type xs:double, the integer is promoted to double. The result is therefore also of type xs:double.

For subtraction operator “-”, note that the same hyphen character can also occur in an identifier. To ensure a hyphen (hyphen) is interpreted as an operator, precede it with a space, which cannot be part of an identifier. A space after the operator is not required. The same applies to unary minus:

$summe-preis – $summe-rabatt

Caution — Omitting spaces can cause problems
An expression such as “a-b” would be interpreted as an identifier without separating spaces, whereas “a – b” unambiguously represents an arithmetic operation because spaces cannot belong to identifiers.

Looking back at XPath 1.0

XPath 1.0 has the same two additive operators as XPath 2.0, except that no type promotion is needed because XPath 1.0 uses the general numeric type number.

Multiplicative expressions

Multiplicative operators include the familiar multiplication and division operators, as well as integer division and modulo. These operators also promote operand types according to context.

Operator * multiplies two numbers. If their types differ, they are promoted to the higher-ranking type: a value of type xs:integer becomes xs:decimal, and a value of type xs:decimal becomes xs:double. The result has the type shared by both operands after promotion.

XPath uses only operator div for division. Operator “/”, used for division in other languages, is used only within path expressions or to denote the document node.

The result type depends on the operand types but need not match them even when both operands have the same type. Dividing two integers, for example, produces a value of type xs:double. In other cases, operands of different types may, after promotion, share their type with the result.

div: -3 div 2 produces -1.5

XPath 2.0 introduces operator idiv for integer division. Its operands default to type xs:integer. The result is always an integer, rounded toward zero if necessary: positive results are rounded according to function fn:floor() and negative results according to function fn:ceiling().

idiv: -3 idiv 2 produces -1

XPath also supports the modulo operation (remainder), using operator mod, formally backed by operator function op:numeric-mod().

mod: 17 mod 3 produces 2

Looking back at XPath 1.0

XPath 1.0 has no integer-division operator. Its other multiplicative operators correspond to those of XPath 2.0, except that no type promotion is needed because XPath 1.0 uses the general numeric type number.

   

back to top