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 andexpression or an orexpression. 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 andexpression and orexpressions 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.
Andexpression
The value of an andexpression is determined by the effective boolean values (EBV's) of its operands. The following table shows what value the andexpression returns for all possible EBV's.
Andexpression 
EBV2 = 
EBV2 = 
EBV2 = 
EBV1 = 



EBV1 = 



EBV1 = 



The order in which the operands are evaluated is implementationdependent. This means that the andexpression 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 andexpression will also raise an error. When the operand that will return false is evaluated first, the andexpression returns false.
Andexpression returning true
XQuery
1 eq 1 and 2 eq 2
Andexpression returning false
XQuery
1 eq 1 and 2 eq 3
Andexpression returning either false or an error
XQuery
1 eq 2 and 3 idiv 0 = 1
Orexpression
The value of an orexpression is determined by the effecive boolean values (EBVs) of its operands. The following table shows what value the orexpression returns for all possible EBV's.
Orexpression 
EBV2 = 
EBV2 = 
EBV2 = 
EBV1 = 



EBV1 = 



EBV1 = 



The order in which the operands are evaluated is implementationdependent. This means that the orexpression 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 orexpression will also raise an error. When the operand that will return true is evaluated first, the orexpression returns false.
Orexpression returning true
XQuery
1 eq 1 or 2 eq 3
Orexpression returning false
XQuery
1 eq 2 or 2 eq 3
Orexpression 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 division 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