O'Reilly logo

XQuery by Priscilla Walmsley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Comparison Expressions

Comparison expressions are used to compare values. There are three kinds of comparison expressions: general, value, and node.

General Comparisons

General comparisons are used for comparing atomic values or nodes that contain atomic values. Table 3-2 shows some examples of general comparisons. They use the operators = (equal to), != (not equal to), < (less than), <= (less than or equal to), > (greater than), and >= (greater than or equal to). Unlike in XSLT, you don't need to escape the < operator as &lt;; in fact, it won't be recognized if you do.

Table 3-2. General comparisons

Example

Value

doc("catalog.xml")/catalog/product[2]/name = 'Floppy Sun Hat'

true

doc("catalog.xml")/catalog/product[4]/number < 500

false

1 > 2

false

( ) = (1, 2)

false

(2, 5) > (1, 3)

true

(1, "a") = (2, "b")

Type error

If either operand is the empty sequence, the expression evaluates to false.

General comparisons on multi-item sequences

General comparisons can operate on sequences of more than one item, as well as empty sequences. If one or both of the operands is a sequence of more than one item, the expression evaluates to true if the corresponding value comparison is true for any combination of two items from the two sequences. For example, the expression (2, 5) < (1, 3) returns true if one or more of the following conditions is true:

  • 2 is less than 1

  • 2 is less than 3

  • 5 is less than 1

  • 5 is less than 3

This example returns true because 2 is less than ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required