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
if ( test-expression ) then result-expression1
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 :=
'1001']/lineitems/lineitem/item/price[. > 200 ]
if ( count($price) ) then
customer ordered an expensive item!
No expensive item was ordered.
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)