advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   TIP BANK
Browse DevX
Download the code for this article
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
 

Introduction to XQuery (Part 4 of 4)

Learn to use XQuery conditional and quantified expressions to filter and select data, and to modify query results by including custom functions.  


 
 
 
 
advertisement
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>

It's quick, easy and you get access to all the articles on DevX.
This registration/login is to allow you to read articles on devx.com.
Already a member?



advertisement