Comparing sequences

The comparison expression (XPath, XQuery) is used to compare two given sequences. There are three different types of comparison expressions. These are the Value Comparisons, the General Comparisons, and the Node Comparisons.

Each of these comparisons has its own specific use. There are some important differences between the comparisons. Using the wrong comparison may cause unexpected results.

Value comparison

Value comparisons (XPath, XQuery) are used to compare single values. The value comparison operators are eq (equal), ne (not equal), lt (lower than), le (lower than or equal), gt (greater than), and ge (greater than or equal).

How it works

A value comparison consists of a few steps. First, both operands will be atomized. The result of this is called the atomized operand. If one of the atomized operands is an empty sequence, the comparison will return an empty sequence.

Value comparison resulting in an empty sequence

XQuery

"abc" eq ()

Open example in playground

The next step is to check the length of both atomized operands. Remember that the value comparison only accepts single values. If one of the operands is not a singleton sequence, the value comparison will raise a type error (XPTY0004).

Value comparison resulting in a type error XPTY0004

XQuery

"abc" eq ("a", "b", "c")

Open example in playground

Each atomized operand now for sure contains exactly one value. Atomized operands of the type xs:untypedAtomic will be cast to xs:string. If the atomized operands are instances of different primitive types, then:

  • Both operands are cast to xs:string if each operand is of the types xs:string or xs:anyURI;

  • Both operands are cast to xs:float if each operand is of the types xs:float or xs:decimal;

  • Both operands are cast to xs:double if each operand is of the types xs:double, xs:float, or xs:decimal;

  • Otherwise a type error is raised (XPTY0004)

Finally, the values are compared.

Examples

Value comparison returning false

XQuery

2 eq 4

Open example in playground

Value comparison returning true

XQuery

"abc" eq "abc"

Open example in playground

General comparison

General comparisons (XPath, XQuery) are used to compare sequences that may contain multiple values. The general comparison operators are = (equal), != (not equal), < (lower than), <= (lower than or equal), > (greater than), >= (greater than or equal).

How it works

This explanation is relevant only when XPath 1.0 compatibility mode is set to false. This is the case for all uses of XPath and XQuery within Fonto Editor.

First, both operands are atomized. This results in two sequences of atomized values. The general comparison will return true if there is a pair of atomic values that have the so-called required magnitude relationship. Otherwise the result is false or an error.

If both atomic values which are being compared are an instance of xs:untypedAtomic, they are cast to the type xs:string. If exactly one of both atomic values is an instance of xs:untypedAtomic, it is cast to a type depending on the other value's dynamic type, according to the following rules:

  1. If the other value is a numeric type or is derived from a numeric type, the value is cast to xs:double;

  2. If the other value is xs:dayTimeDuration or is derived from xs:dayTimeDuration, the value is cast to xs:dayTimeDuration;

  3. If the other value is xs:yearMonthDuration or is derived from xs:yearMonthDuration, the value is cast to xs:yearMonthDuration;

  4. In all other cases, the value is cast to the primitive type of the other value.

If a cast operation is not successful, the comparison will raise a dynamic error (FORG0001).

After performing the conversions, the atomic values are compared using one of the value comparison operators. Which value comparison operator (eq, ne, lt, le, gt, or ge) is used depends on which comparison operator (=, !=, <, <=, >, or >=) was used. The values have the required magnitude relationship if and only if the result of this value comparison is true.

An implementation may return as soon as it finds a pair of values with the required magnitude relationship or when it encounters an error. This means that the result of a general comparison is not deterministic in the presence of errors.

Examples

General comparison returning true

XQuery

(1, 2, 3, 4, 5) = 3

Open example in playground

General comparison returning false

XQuery

(1, 2, 3) = (4, 5)

Open example in playground

Node comparison

Node comparisons (XPath, XQuery) are used to compare two given nodes. The node comparison operators are is (equal), << (precedes), and >> (follows). Node comparisons can compare two nodes based on their identity or by their document order.

How it works

Each operand must be either a single node or an empty sequence; otherwise a type error (XPTY0004) is raised. If either operand is an empty sequence, the result of the comparison is an empty sequence.

A comparison with the is operator is true when both operands are the same node, otherwise it is false. A comparison with the << operator returns true when the left operand node precedes the right operand node in document order; otherwise it returns false. A comparison with the >> operator returns true when the left operand node follows the right operand node in document order; otherwise it returns false.

Examples

Node comparison returning true

XQuery

//element1 is //*[@id = "1"]

Open example in playground

Node comparison returning false

XQuery

//element1 is //element2

Open example in playground

Node comparison precedes

XQuery

//element1 << //element2

Open example in playground

Node comparison follows

XQuery

//element2 >> //element1

Open example in playground