advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the code for this article
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 4.5/5 | Rate this item | 2 users have rated this item.
 

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).
  Next Page: Writing Quantified Expressions


Page 1: IntroductionPage 3: Using XQuery's Built-in Functions
Page 2: Writing Quantified ExpressionsPage 4: User-Defined Functions
Please rate this item (5=best)
 1  2  3  4  5
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About