Expressions in XPath include the sorts of structures, operators, and data types found in most computer languages, albeit with a few differences. This section gives an overview of these distinguishing features. Consult the XPath Recommendation [XPath] for the full details. Note that location paths can appear in expressions, and expressions can appear in location paths as predicates (see Section 6.3.4).
The evaluation of XPath expressions occurs in a context. This context consists of a node, position, and set of variable bindings. Section 6.3 discussed nodes and positions as they apply to location paths. XPath does not support looping or assignment constructs, although you can achieve a limited repetition effect by using an axis in a location step. While the context node and position can be different at different places in evaluating an XPath expression, particularly inside a location path, the context variable bindings do not change during the evaluation of an expression.
Location paths always produce a node-set, which can be empty.
6.4.1 Function Calls
The syntax for a function call has the typical format: the function name followed by zero or more comma-separated arguments (parameters) in parentheses. For example:
When the type of a particular parameter is fixed as "string," "number," or "boolean," and a different type of parameter is supplied, the type is converted as if by calling the "string( )," "number( )," or "boolean( )" function, respectively. An error occurs if a parameter of any other type is supplied when the program expects a "node-set" type parameter.
Section 6.5 provides a full list of the core library functions. Particular applications of XPath may supplement this library. Node tests (listed in Section 6.3.3) that look like function calls are considered to be special instances of the XPath syntax, not separate functions.
This section lists all of the operators available in XPath expressions except for the operators that are part of the syntax of location paths, such as the slash and double colons. If the values presented to an operator are of the wrong type, they are converted, or else generate an error. Note that some operators, such as the left angle bracket ("<"), must be encoded as a character reference or the like ("<") if they occur in an XML attribute value or element content.
( ) Operators have precedence and association direction as described in [XPath] but can be overridden or clarified by explicit grouping via parentheses.
| The vertical bar character produces the union of the node-sets appearing before and after it.
Boolean operators appear syntactically between two operands.
or The logical "or" of the Boolean values appearing before and after it. When the left operand is true, the right operand is not evaluated.
and The logical "and" of the Boolean values appearing before and after it. When the left operand is false, the right operand is not evaluated.
For the comparison operators shown below, if both operands are node-sets, then the comparison is true if either of the following conditions applies:
= Tests for equality between its operands. If neither operand is a node-set, both are converted to the same type as follows:
!= Tests for inequality between its operands. Use the same rules as for "=" if neither operand is a node-set.
For the following comparison operators, if neither is a node-set, then both are converted to numbers and compared according to [IEEE 754].
< True if the left operand is less than the right operand.
> True if the left operand is greater than the right operand.
<= True if the left operand is less than or equal to the right operand.
>= True if the left operand is greater than or equal to the right operand.
Double-precision 64-bit floating-point numbers use the [IEEE 754] system of binary floating-point arithmetic. It includes positive and negative infinity, positive and negative zero, and "Not-a-Number" (NaN). The numeric operators, if necessary, convert operands as if by calling the "number( )" function.
div The left operand divided by the right operand. (A slash ("/") cannot be used for division because it separates location path steps.)
mod The remainder of the left operand divided by the right operand with truncation, not rounding.
+ The sum of the left and right operands. The unary plus is not supported.
- The left operand minus the right operand. It may be used as a unary operator with only a right operand that produces the same result as if the left operand had been zero. Because XML permits you to use this character inside names, it may be necessary to insert white space before a minus sign: "x-y" is a single token and "x- y" is two tokens separated by a space, whereas "x -y" and "x - y" are x minus y.