Other operators
This page describes a few useful operators available in XPath and XQuery.
For more information on comparisons and the comparison operators, head over to the comparing sequences page.
Logical Expressions
A logical expression (XPath, XQuery) is either an and-expression or an or-expression. A logical expression will always return a boolean true or false, if it does not happen to raise an error.
A logical expression will first try to find the effective boolean value of its operands. Finding the effective boolean value may raise an error. This makes determining the outcome of a logical expression a bit less straight forward.
The description of the and-expression and or-expressions on this page omit the notes on XPath 1.0 compatibility mode. To see how these expressions behave when XPath 1.0 compatibility mode is enabled, refer to the W3C XPath 3.1 specification.
And-expression
The value of an and-expression is determined by the effective boolean values (EBV's) of its operands. The following table shows what value the and-expression returns for all possible EBV's.
And-expression |
EBV2 = |
EBV2 = |
EBV2 = |
EBV1 = |
|
|
|
EBV1 = |
|
|
|
EBV1 = |
|
|
|
The order in which the operands are evaluated is implementation-dependent. This means that the and-expression will either return false or it will raise an error. This situation exists when one of its operands will return false and the other operand will raise an error while finding their effective boolean values.
When the operand that will raise an error is evaluated first, the and-expression will also raise an error. When the operand that will return false is valuated first, the and-expression returns false.
And-expression returning true
XQuery
1 eq 1 and 2 eq 2
And-expression returning false
XQuery
1 eq 1 and 2 eq 3
And-expression returning either false or an error
XQuery
1 eq 2 and 3 idiv 0 = 1
Or-expression
The value of an or-expression is determined by the effecive boolean values (EBVs) of its operands. The following table shows what value the or-expression returns for all possible EBV's.
Or-expression |
EBV2 = |
EBV2 = |
EBV2 = |
EBV1 = |
|
|
|
EBV1 = |
|
|
|
EBV1 = |
|
|
|
The order in which the operands are evaluated is implementation-dependent. This means that the or-expression will either return true or it will raise an error. This situation exists when one of its operands will return true and the other operand will raise an error while finding their effective boolean values.
When the operand that will raise an error is evaluated first, the or-expression will also raise an error. When the operand that will return true is valuated first, the or-expression returns false.
Or-expression returning true
XQuery
1 eq 1 or 2 eq 3
Or-expression returning false
XQuery
1 eq 2 or 2 eq 3
Or-expression returning true or an error
XQuery
1 eq 1 or 3 idiv 0 = 1
String concatenation expression
Arithmetic expressions
XPath and XQuery support arithmetic expressions (XPath, XQuery). These expressions may be operators for addition, multiplication, division, and modulus.
An arithmetic expression will first atomize both their operands. An arithmetic expression will return an empty sequence if one or both of its operands turn out to be an empty sequence after atomization. If an atomized operand turns out to be a sequence with a length greater than one, the type error XPTY0004 will be raised.
If an atomized operand is an instance of xs:untypedAtomic, it is cast to xs:double. If this cast fails, the dynamic error FORG0001 will be raised.
The description of the arithmetic expressions on this page omit the notes on XPath 1.0 compatibility mode. To see how these expressions behave when XPath 1.0 compatibility mode is enabled, refer to the W3C XPath 3.1 specification.
After atomization, and when both operands are singleton sequences, the operands need to be a valid combination of data types for the used arithmetic operator. The combination of atomic types accepted by the different operators, and their respecitve result types are listed in Appending B.2 Operator Mapping of both the XPath and XQuery specs.
Addition operator
The addition operator adds two numeric or two date and time related operands and returns their sum. The operator itself is the +
character.
Simple addition operator example
XQuery
1 + 1
Subtraction operator
The subtraction operator subtracts two numeric or two date and time related operands. The operator itself is the -
character. This operators will only work when it is preceded by whitespace. If it is not, the dash is considered as a part of a (node) name.
Simple subtraction operator example
XQuery
1 - 1
Multiplication operator
The multiplication operator multiplies its two operands. The operator itself is the *
character.
Simple multiplication operator example
XQuery
2 * 5
Division operators
The division operator divides the first operand by the second operand. There are two variants of the division operator. One operator returns the decimal value of the division and the operator itself is the word div
. The other operator returns the integer value of the divison and the operator itself is the word idiv
.
Simple division operator example
XQuery
5 div 2
Simple integer division operator example
XQuery
5 idiv 2
Modulo operator
The modulo operator calculates the remainder of dividing the first operand by its second operand. The operator itself is the word mod
.
Simple modulo operator example
XQuery
5 mod 2