One of the first things I ran into with XQuery was how to compare a single string value against a list of matching values.
In any language there is usually some trick involving arrays, lists and looping.
So imagine my surprise when I learned that, in XQuery, you can do this:
let $input := "christmas"
let $holidays := ("christmas", "hanukkah", "chanukah", "kwanza", "new year")
return
if ($input = $holidays) then
<sentiment>We've got holiday!</sentiment>
else
<sentiment>looks like coal</sentiment>
XQuery is made up of expressions, and the value of every expression is a sequence. A sequence is one or more items . . . and an item is one of the XQuery types (strings, numerics, elements, attributes etc). See this very complete section of the XQuery language for the complete lowdown.
So, since everything is a sequence (even the sequence of 1 that holds "christmas") the '=' operator, called the general comparison, evaluates the left hand side of the equation against *every* value in the sequence on the right.
Compare a value against a list is built into XQuery!
You can still do equality with the 'eq' or value comparison but this is reserved for a single value like:
let $input := "christmas"
return
if ($input eq "christmas") then
<sentiment>We've got stockings!</sentiment>
else
<sentiment>looks like coal, try again</sentiment>
Useful, but in a multicultural content filled world, I think '=' is the better match.
Happy Holidays!
Matt
Comments