Home > XQuery Resources > XQuery Tutorial> Introduction
This document contains an example each of some of the common types of XQuery expressions. The examples in this section use the following documents: 1. Literal
{-- String literal --}
"Hello World !"

{-- Integer literal --}
"-123"
 
2. Function Call
{-- Returns the document node representing the
    document named "R-items.xml" in collection
    named "data". The document() function is one
    of the XQuery 1.0 built-in functions --}

document("data/R-items.xml")
 
3. Path Expression
{-- Returns all the "item_tuple" elements in the
    document "data/R-items.xml" that had a reserve
    price over 50 dollars --}

document("data/R-items.xml")/items/item_tuple[reserve_price > 50]
 
4. FLWOR Expression
{-- For each "item_tuple" element return the
    description and reserve_price if the
    reserve_price is below 50 dollars, and return
    them in alphabetically ascending order of
    description --}

for $item in
  document("data/R-items.xml")/items/item_tuple
where $item/reserve_price > 50
order by $item/description
return <item>{$item/description, $item/reserve_price}</item>
 
5. Conditional Expression
{-- Tell me if the average of the reserve_price
    values is less than 200 dollars. This
    expression evaluates to a xs:string typed
    value --}

if(avg(document("data/R-items.xml")//reserve_price) < 200)
then "Average reserve_price is less than 200 dollars"
else "Average reserve_price is not less than 200 dollars"
 
6. Quantified Expressions
{-- Quantified expressions return a boolean value
    based on applying a test to a sequence of
    tuples. There are two types of quantifiers:
      -> Existential Quantifiers (some)
      -> Universal Quantifiers (every)
    Here are examples of these two types of
    quantifiers --}
6a. Existential Quantifier
{-- Tell me if there exists at least one
    reserve_price that is greater than 1000
    dollars. Return value if true if atleast one
    reserve_price has value greater than 1000 --}

some $price in document("data/R-items.xml")//reserve_price
    satisfies $price > 1000
6b. Universal Quantifier
{-- Tell me if all reserve_price values are less
    than 100000 --}

every $price in
  document("data/R-items.xml")//reserve_price
    satisfies $price < 100000
 
7. User defined functions
{-- define a function that takes a bid_tuple
    element and tells me whether the bidder is a
    valid user (has an entry in the R-users.xml
    document. The format of the "bid_tuple"
    element is as indicated in the R-bids.xml
    document --}

define function verifyUser($bid_tuple as element) as xs:boolean {
    some $user in
      document("data/R-users.xml")//userid satisfies
        $user =$bid_tuple/userid
}

 


Company | Products | Solutions | News & Events | Developers | Contact Us | Site Map