|
>Home>Learn XQuery> XQuery Tips and Tricks>Using Node Before Operator
Print
What's the proper way to use the node sequence (<< and >>) operators?
This is my XQuery:
let $referenceBook := /books/book[2]
let $book := /books/book
where $book/title << $referenceBook
return $book
This looks ok to me, but when I run it, I get the following error. Can you tell me why?
[DataDirect][XQuery][err:XPTY0004]A sequence of more than one item is not allowed as the first operand of '<<' (<title/>, <title/>, ...)
The "node before" (<<) and "node after" (>>) operators can only compare the document position of a single node to the position of another node. In your case, the let $book := /books/book instruction is assigning all the <book> elements in the input document to the $book variable, which means that $book/title corresponds to a sequence of many elements.
Perhaps what you mean to do is this:
let $referenceBook := /books/book[2]
for $book in /books/book
where $book/title << $referenceBook
return $book
Next Question!
Can I insruct XQuery to use numbers and not scientific notation in query results?
|