Login | Register   
LinkedIn
Google+
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) : Page 2

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


advertisement
Writing Quantified Expressions
XQuery quantified expressions support existential (meaning at least one value exists that satisfies the given condition) and universal (all values satisfy the given condition) quantification. The value of a quantified expression is either true or false. There are several types:

Existential Quantification Expressions

The syntax of the existential quantification expression is:

some $v in seq-expression satisfies test-expression

A quantified expression returns a Boolean true or false by iteratively evaluating the items in seq-expression and returns true as soon as it finds one that meets (satisfies) the criteria in test-expression. If no item meets the criteria, the quantified expression returns false.

For example, the following query checks to see if customer 1001 has ordered any items where price is greater than 200.

# see XQuery42.ixq in samples.zip let $price := document("data/PO.xml")//po[customer/custno= '1001']/lineitems/lineitem/item/price return if (some $v in $price satisfies ( $v > 200 ) ) then <result> customer ordered an expensive item! </result> else <result> No expensive item is ordered. </result>

The existential quantification expression some $v in $price satisfies ($v > 200)) appears as the test condition in the if-then-else expression in the return clause. If the quantification expression returns true, the query result is:


<result> customer ordered an expensive item! </result>

Universal Quantification Expressions

The syntax of the universal quantification expression is:

every $v in seq-expression satisfies test-expression

The following query checks if the price of every item customer 1001 orders is over 200.

# Query listing - XQuery43.ixq in samples.zip let $price := document("data/PO.xml") //po[customer/custno='1001'] /lineitems/lineitem/item/price return if (every $v in $price satisfies ( $v > 200 ) ) then <result> customer always orders expensive items! </result> else <result> Customer does not always order expensive items </result>

Unless customer 1001 always orders items priced over 200, the result is

<result> Customer does not always order expensive items </result>



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap