Learn XQuery |
Home:Learn XQuery: XQuery Tips and Tricks:Comparison Operators How do I choose the right comparison operator?I have this XQuery, which works as I would expect: let $doc := … in that it returns the “book1” book authored by “author1”; but if I changed the XQuery to this… let $doc := … then I get a type error, like the following: A sequence of more than one item is not allowed as the first operand of 'eq'” The problem is with the use of the comparison operator “eq”; XQuery has a variety of comparison operators: value, general, node and order. It is easy to miss the difference between value (eq, ne, lt, le, gt, ge) and general (=, !=, <, <=, >, >=) comparison operators.
There are other more subtle differences about value and general comparison operators, especially when dealing with untyped data; The XPath 2.0 specifications describe those differences in full details. From a performance point of view, it is usually good practice to use value comparison operators when you know that you are comparing exactly two items. Going back to the example above, if what the query is trying to find are all the books for which “author1” is listed as an author, then you can just change it to use a general comparison operator: let $doc := Next Question! |
Submit Your DataDirect XQuery Tip or TrickTell us your XQuery Tip or Trick – if it gets published on our site, you’ll receive a |





