Value comparisons

Value comparisons compare two atomic values. The value comparison operators include eq, ne, lt, le, gt, and ge.

The following table describes these operators.
Table 1. Value comparison operators in XQuery
Operator Purpose
eq Returns true if the first value is equal to the second value.
ne Returns true if the first value is not equal to the second value.
lt Returns true if the first value is less than the second value.
le Returns true if the first value is less than or equal to the second value.
gt Returns true if the first value is greater than the second value.
ge Returns true if the first value is greater than or equal to the second value.
Restriction: The operand of a value comparison cannot contain an FLWOR expression.

Two values can be compared if they have the same type or if the type of one operand is a subtype of the other operand's type. Two operands of numeric types (types xs:integer, xs:decimal, xs:double, and types derived from these) can be compared.

Special values: For xs:double values, positive zero and negative zero compare equal. INF equals INF, and -INF equals -INF. NaN does not equal itself. Positive infinity is greater than all other non-NaN values; negative infinity is less than all other non-NaN values. NaN ne NaN is true, and any other comparison involving a NaN value is false.
The result of a value comparison can be a boolean value, an empty sequence, or an error. When a value comparison is evaluated, each operand is atomized (converted into an atomic value), and the following rules are applied:
  • If either atomized operand is an empty sequence, the result of the value comparison is an empty sequence.
  • If either atomized operand is a sequence that contains more than one value, an error is returned.
  • If either atomized operand is an untyped atomic value (xs:untypedAtomic), that value is cast to xs:string.

    Casting values of type xs:untypedAtomic to xs:string allows value comparisons to be transitive. In contrast, general comparisons follow a different rule for casting untyped data and are therefore not transitive.

  • If the types of the operands, after evaluation, are a valid combination for the operator, the operator is applied to the atomized operands, and the result of the comparison is either true or false. If the types of the operands are not a valid combination for the comparison operator, an error is returned.

The following types can be compared with value comparison operators. The term numeric refers to the types xs:integer, xs:decimal, xs:double, and any type derived from one of these types. During comparisons that involve numeric values, subtype substitution and numeric type promotion are used to convert the operands into the first type in the ordered list (xs:integer, xs:decimal, xs:double) into which all operands can be converted.

  • Numeric
  • xs:boolean
  • xs:string
  • xs:date
  • xs:time
  • xs:dateTime
  • xs:yearMonthDuration
  • xs:dayTimeDuration
  • xs:duration (eq and ne only)

Examples

  • The following comparison atomizes the nodes that are returned by the expression $book/author. The comparison is true only if the result of atomization is the value "Kennedy" as an instance of xs:string or xs:untypedAtomic. If the result of atomization is a sequence that contains more than one value, an error is returned
    $book1/author eq "Kennedy"
  • The following path expression contains a predicate that selects products whose weight is greater than 100. For any product that does not have a weight subelement, the value of the predicate is the empty sequence, and the product is not selected:
    //product[xs:decimal(weight) gt 100]
  • The following comparisons are true because, in each case, the two constructed nodes have the same value after atomization, even though they have different identities or names:
    <a>5</a> eq <a>5</a> 
    <a>5</a> eq <b>5</b>