
his is the final part of the four part series on Xquery. In this final section, you'll see an explanation of conditional and quantified expressions as well as a quick tutorial on writing your own functions in XQuery.
Writing Conditional Expressions
As you would expect, XQuery provides an
if-then-else clause that lets you branch execution based on a condition. The syntax of the
if-then-else is:
if ( test-expression ) then result-expression1
else result-expression2
For example, the following query checks whether customer 1001 ordered any item with a price over 200.
# Query listing -- XQuery41.ixq in samples.zip
let $price :=
document("data/PO.xml")//po[customer/custno=
'1001']/lineitems/lineitem/item/price[. > 200 ]
return
if ( count($price) ) then
<result>
customer ordered an expensive item!
</result>
else
<result>
No expensive item was ordered.
</result>
The variable
$price represents a sequence of price items whose value is greater than 200. The
if-then-else expression displays one of two different messages depending on the
result (
true or false) of evaluating the test expression
count($price).