General comparisons in XQuery
General comparisons compare two sequences of any length to determine if a comparison is
true for at least one item in both sequences. The general comparison operators include
=
, !=
, <
, <=
,
>
, and >=
.
The following table describes these operators, listed in order of operator precedence from
highest to lowest.
Operator | Purpose |
---|---|
= |
Returns true if any value in the first sequence is equal to any value in the second sequence. |
!= |
Returns true if any value in the first sequence is not equal to any value in the second sequence. |
< |
Returns true if any value in the first sequence is less than any value in the second sequence. |
<= |
Returns true if any value in the first sequence is less than or equal to any value in the second sequence. |
> |
Returns true if any value in the first sequence is greater than any value in the second sequence. |
>= |
Returns true if any value in the first sequence is greater than or equal to any value in the second sequence. |
Restriction: The operand of a general comparison cannot contain an FLWOR
expression.
The result of a general comparison expression is a boolean value or an error.
XQuery uses the following process to
evaluate a general comparison expression.
- Atomizes each operand into a sequence of atomic values.
- Compares each of the values in the first sequence to each of the values in the second sequence.
For each comparison:
- If one of the atomic values is an instance of
xs:untypedAtomic
and the other is an instance of a numeric type (xs:integer, xs:decimal, or xs:double), the untyped value is cast to the typexs:double
. - If one of the atomic values is an instance of
xs:untypedAtomic
and the other is an instance ofxs:untypedAtomic
orxs:string
, thexs:untypedAtomic
values are cast to the typexs:string
. - If one of the atomic values is an instance of
xs:untypedAtomic
and the other is not an instance ofxs:string
,xs:untypedAtomic
, or any numeric type, thexs:untypedAtomic
value is cast to the dynamic type of the other value.
- If one of the atomic values is an instance of
- If at least one of the values in the first sequence and at least one of the values in the second sequence meet the conditions of the comparison, the general comparison is true.
Syntax
Examples
The following query uses a general comparison expression to find the descriptions of products that cost less than 20 units.
SELECT XMLQUERY ('declare namespace pos="http://posample.org";
/pos:product/description[price < 20]'
PASSING DESCRIPTION)
FROM T1;