Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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>

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).


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap