XML technologies / XPath / XPath 2.0 – Language reference / XPath expressions: operators and keywords / Comparison expressions

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 4.)

Comparison expressions compare the values of two subexpressions in a compound expression. The result is Boolean: true if the comparison holds, false if it fails.

XPath 2.0 distinguishes four kinds of comparison with different operator sets: value comparisons, general comparisons, node comparisons and document-order comparisons.

The operator symbols used are briefly listed here:

Comparing individual values

Operators: eq, ne, lt, le, gt, ge

These operators specifically compare individual atomic values. Separate symbols exist for comparing complex values or sequences; see below. (Note: Although, for example, eq and = use the same predefined operator function through overloading, a value comparison with eq can yield a different result from a general comparison with =.)

For a value comparison, both operands are first atomized, as if function fn:data() had been applied. If they then have type xdt:untypedA­tomic, they are converted into strings of type xs:string; otherwise the value remains unchanged. Failed atomization produces an error.

The actual comparison then takes place, yielding Boolean value true or false, depending on whether the condition holds.

  • eq: 3 eq 3 yields true

  • ne: 3 ne 3 yields false

Use of operator eq requires caution: the following comparison yields true only if expression $buch has a single child element autor, not several, whose string value is “James Joyce”:

$buch/autor eq "James Joyce"

If there is only one autor element but its value differs, the comparison yields false. However, as soon as there are several child elements autor, the comparison ends with an error.

Caution – No implicit type conversion in XPath 2.0:
XPath 1.0 commonly performed implicit type conversion during comparisons. XPath 2.0 does not.
Thus, comparison 3 eq "3" produces an error and must be written with explicit conversion of an operand as 3 eq fn:number("3") or fn:string(3) eq "3".

Looking back at XPath 1.0

XPath 1.0 has no operators directly comparing individual values. Instead it uses general comparison operators, which can, however, compare only numeric values.

General comparisons

Operators: =, !=, <, <=, >, >=

General comparisons accept arbitrary operands. Sequence comparison operates on the basis of quantifier some, regardless of the sequences’ lengths. Unless an error occurs directly, a general comparison therefore always returns true or false, regardless of operand properties.

Non-atomic operands are first atomized, as by fn:data(), and the resulting sequences are compared to see whether any pair of items satisfies the comparison.

These sequences of atomic values are compared using the value-comparison operators eq, ne, lt, le, gt, or ge, depending on whether the original general-comparison operator was =, !=, <, <=, > or >=.

The general comparison holds if the corresponding value comparison of atomized values holds.

The following general comparison therefore holds if expression $buch has any number of child elements autor, one of which has string value “James Joyce”. This corresponds to existential quantification with some; see below:

$buch/autor = "James Joyce" 

Note – Escaping in XPath expressions
Within XML documents, including XSLT stylesheets, XPath expressions must follow special-character rules. Comparison operator < must therefore be written as &lt;, also for <=. Operator > is usually escaped too, although this is not essential.

Looking back at XPath 1.0

In XPath 1.0, comparing two values performs implicit type conversion. A general comparison operator then performs the comparison. Relational comparisons, such as greater or less, require numbers or values convertible to numbers. String comparison other than equality is impossible in XPath 1.0.

Node comparisons

Operators: is, isnot

Node comparison tests the identity or nonidentity of two nodes, rather than equality of their values. General comparisons can test the latter, but cannot unambiguously determine whether two nodes are the very same node.

Each operand of a node comparison must be a single node, otherwise a type error is reported. An empty sequence for either or both operands causes no type error, but the comparison then returns an empty sequence rather than a Boolean value. In this context, however, the empty sequence can ultimately be treated as false.

If both operands are nodes, a comparison using operator is is true if both nodes are identical, that is, the same node. By contrast, comparison with isnot is true if the nodes are not identical.

The following comparison is true if the two expressions identify the same node:

//buch[isbn="3-518-11100-0"] is //buch[titel="Ulysses"]

These operators are formally based on operator function op:node-equal(), which users cannot call directly. Comparison a is b therefore corresponds to op:node-identical(a, b), and comparison a isnot b to not(op:node-identical(a, b)).

Looking back at XPath 1.0

XPath 1.0 provides no direct node-identity comparison, but a workaround using function count() is possible. To compare node $a with node $b, the following test yields true if both nodes are identical: count($a | $b) = 1

Comparing document positions

Operators: <<, >>

Operators << and >> compare two nodes’ relative positions in document order. Both operands must be nodes. If either operand expression does not yield a single node, a type error occurs. An empty sequence for either or both operands causes no type error; the comparison then also returns an empty sequence.

Comparing two nodes with operator << yields true if the node represented by the left operand precedes the other node in document order, otherwise false.

Similarly, comparing two nodes with >> yields true if and only if the left operand follows the right operand’s node in document order.

Operator symbols << and >> operate through internal operator functions that cannot themselves be called directly. Expression a << b corresponds to function call op:node-before(a, b) and expression a >> b to function call op:node-after(a, b).

Looking back at XPath 1.0

XPath 1.0 has no operators comparing node positions, but workarounds using the preceding and following axes are possible.

   

back to top