XPath Number Operators
XPath number operators are used to perform mathematical operations like addition, subtraction, multiplication, and division on numerical values in XPath expressions.
Syntax
The following table lists the available number operators in XPath.
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
div | Division |
mod | Modulo |
Example
Consider the following XML document:
<bookstore>
<book>
<title>Harry Potter and the Philosopher's Stone</title>
<author>J.K. Rowling</author>
<price>10.99</price>
</book>
<book>
<title>The Hobbit</title>
<author>J.R.R. Tolkien</author>
<price>7.99</price>
</book>
<book>
<title>The Da Vinci Code</title>
<author>Dan Brown</author>
<price>12.99</price>
</book>
</bookstore>
To select the price of the first book and add $2 to it, the following XPath expression can be used:
/bookstore/book[1]/price + 2
This will select the price of the first book (10.99) and add 2 to it, resulting in the output 12.99.
Output
The output of the above XPath expression will be a number value, which is the result of the mathematical operation performed.
Explanation
Number operators in XPath are used to perform mathematical operations on numerical values. The +
operator is used to add two numbers, the -
operator is used to subtract one number from another, the *
operator is used to multiply two numbers, the div
operator is used to divide one number by another, and the mod
operator is used to find the remainder after division of one number by another.
When using number operators in an XPath expression, the operands must be numeric values. If an operand is not a numeric value, it will be automatically converted to a number before the operation is performed.
Use
XPath number operators are commonly used in XPath expressions to perform mathematical operations on numeric values. They are useful when selecting or manipulating numeric data in XML documents.
Important Points
- The
+
operator is used to add two numbers. - The
-
operator is used to subtract one number from another. - The
*
operator is used to multiply two numbers. - The
div
operator is used to divide one number by another. - The
mod
operator is used to find the remainder after division of one number by another. - When using number operators in an XPath expression, the operands must be numeric values.
Summary
XPath number operators are used to perform mathematical operations on numerical values in XPath expressions. They include the +
operator for addition, the -
operator for subtraction, the *
operator for multiplication, the div
operator for division, and the mod
operator for finding the remainder after division. These operators are useful when selecting or manipulating numeric data in XML documents.