for $x in doc("books.xml")/bookstore/book
return if ($x/@category="CHILDREN")
then <child>{data($x/title)}</child>
else <adult>{data($x/title)}</adult>
请注意 "If-Then-Else" 的语法:if 表达式后的圆括号是必需的。else 也是必需的,不过只写 "else ()" 也可以。
上面的例子的结果:
<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>Learning XML</adult>
<adult>XQuery Kick Start</adult>