Using Predicates with XQuery - Comparisons in Predicates (Page 2 of 4 )
The examples in the previous section use general comparison operators like = and <. You can also use the corresponding value comparison operators, such as eq and lt, but you should be aware of the difference. Value comparison operators only allow a single value, while general comparison operators allow sequences of zero, one, or more values. Therefore, the path expression:
is acceptable, because each priceList element can have only one effDate attribute. However, if you wanted to find all the priceList elements that contain the product 557, you might try the expression:
doc("prices.xml")//priceList[prod/@num eq 557]
This will raise an error because the expression prod/@num returns more than one value per priceList. By contrast:
doc("prices.xml")//priceList[prod/@num = 557]
returns a priceList if it has at least one prod child whose num attribute is equal to 557. It might have other prod children whose numbers are not equal to 557.
In both cases, if a particular priceList does not have any prod children with num attributes, it does not return that priceList, but it does not raise an error.
Another difference is that value comparison operators treat all untyped data like strings. If we fixed the previous problem with eq by returning prod nodes instead, as in:
doc("prices.xml")//priceList/prod[@num eq 557]
it would still raise an error if no schema were present, because it treats the num attribute like a string, which can't be compared to a number. The = operator, on the other hand, will cast the value of the num attribute to xs:integer and then compare it to 557, as you would expect.
For these reasons, general comparison operators are easier to use than value comparison operators in predicates when children are untyped or repeating. The down side of general comparison operators is that they also make it less likely that the processor will catch any mistakes you make. In addition, they may be more expensive to evaluate because it's harder for the processor to make use of indexes.