Skip to content

How to Use XPath Number Operators and Functions

How to perform mathematical calculations in XPath with arithmetic operators (+, -, *, div, mod) and numeric functions (round(), ceiling(), floor(), sum()) for filtering XML data and computing values in XSLT.

While XPath is primarily a language for navigating and selecting nodes, it also includes a powerful set of operators and functions for performing mathematical calculations. These are essential for creating complex filters in predicates [...] or for computing values in technologies like XSLT.

Let's explain the standard arithmetic operators and numeric functions in XPath and provide practical examples of how to use them to query an XML document.

XPath Arithmetic Operators

XPath provides five basic arithmetic operators for use in expressions.

Operator Description Example
+ Addition 6 + 4 (returns 10)
- Subtraction 6 - 4 (returns 2)
* Multiplication 6 * 4 (returns 24)
div Division 8 div 4 (returns 2)
mod Modulo (remainder) 8 mod 3 (returns 2)

XPath Numeric Functions

XPath includes several built-in functions for rounding and aggregation.

Function Description Example
round() Rounds a number to the nearest integer. round(4.6) (returns 5)
ceiling() Rounds a number up to the nearest integer. ceiling(4.1) (returns 5)
floor() Rounds a number down to the nearest integer. floor(4.9) (returns 4)
sum() Calculates the sum of a node-set. sum(//price)

Practical Examples

All examples will refer to the following XML document:

<bookstore>
  <book>
    <title>Everyday Italian</title>
    <price>30.00</price>
    <stock>15</stock>
  </book>
  <book>
    <title>Harry Potter</title>
    <price>29.99</price>
    <stock>10</stock>
  </book>
  <book>
    <title>Learning XML</title>
    <price>39.95</price>
    <stock>7</stock>
  </book>
</bookstore>

Using Operators in Predicates

Arithmetic operators are most commonly used inside predicates [...] to filter nodes based on a calculation.

Example: Select all books where the price multiplied by the stock is greater than 400.

//book[price * stock > 400]

Output: this selects the <book> for "Everyday Italian", because 30.00 * 15 = 450, which is greater than 400.

<book>
    <title>Everyday Italian</title>
    <price>30.00</price>
    <stock>15</stock>
</book>
<book>
    <title>Everyday Italian</title>
    <price>30.00</price>
    <stock>15</stock>
</book>

Example: Select all books with an odd number of items in stock.

//book[stock mod 2 != 0]

Output: this selects the <book> elements for "Learning XML" (stock 7) and "Everyday Italian" (stock 15).

<book>
    <title>Learning XML</title>
    <price>39.95</price>
    <stock>7</stock>
</book>

Using Functions in Predicates

Rounding functions are useful for filtering based on integer values.

Example: Select all books where the rounded price is exactly 30.

//book[round(price) = 30]

Output: this selects the <book> for "Harry Potter", because round(29.99) is 30.

<book>
    <title>Harry Potter</title>
    <price>29.99</price>
    <stock>10</stock>
</book>

Example: Select all books where the price, when rounded down, is 29.

//book[floor(price) = 29]

Output: this also selects the <book> for "Harry Potter", because floor(29.99) is 29.

<book>
    <title>Harry Potter</title>
    <price>29.99</price>
    <stock>10</stock>
</book>

Using Functions to Return a Value

Functions like sum() are often used not to select nodes, but to return a single, calculated value. This is especially common in XSLT.

Example: Calculate the total value of all books in stock.

sum(//book/price * //book/stock)

Output: this expression would be evaluated by an XPath processor to return the number 1029.85 (which is (30.00 * 15) + (29.99 * 10) + (39.95 * 7)).

Example: Select all books with a price higher than the average price.

//book[price > sum(//price) div count(//book)]

Output: this selects the <book> for "Learning XML", as its price (39.95) is higher than the average price of all books.

Conclusion

XPath's number operators and functions transform it from a simple node-selection language into a powerful query language capable of performing calculations.

  • Use arithmetic operators (+, -, *, div, mod) inside predicates ([...]) to filter nodes based on calculations.
  • Use rounding functions (round(), ceiling(), floor()) to simplify comparisons with decimal values.
  • Use sum() to perform aggregations on node-sets, either to return a final value or for advanced filtering within a predicate.
Navigation