Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Introduction to XQuery (Part 2 of 4) : Page 3

The XQuery data model recognizes seven types of nodes. Learn how to take advantage of the data model in XQuery expressions




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

XQuery is a functional language just like other programming languages, so it is not strange to see almost all the operators that you typically encounter in other languages. But in addition to those, there are some new operators that are XQuery specific, such as the except and intersect operators.

Arithmetic operators. The arithmetic operators in XQuery are same as the arithmetic operators in other languages. The argument and the return types are basic numeric types. The following example shows how you might use arithmetic operators in XQuery:

# see XQuery26.ixq in samples.zip # Calculate the unit price, tax and total amount for item number '0021': let $price := document("data/PO.xml") //item[itemno='0021']/price let $quantity := document("data/PO.xml") //po[@id='0001']/lineitems/lineitem/quantity let $taxrate := document("data/PO.xml") //item[itemno='0021']/taxrate return <result> <unitprice>{$price div $quantity} </unitprice> <tax> {$price * $taxrate} </tax> <total> {$price + $price * $taxrate}</total> </result>

Comparison operators. Comparison operations take two operands of the same type and return a Boolean value. XQuery supports comparing two operands of different types if one of the operands can be converted to the same type as the other. The following query computes an approval level for each item in a purchase order based on the item's price.

# see XQuery27.ixq in samples.zip # Find out the approval level for all items: for $item in document("data/PO.xml")//item return <result> {$item/itemno} <ApprovalLevel> {if ($item/price >= 500) then 3 else if ($item/price >= 200) then 2 else if ($item/price <= 50) then 0 else 1} </ApprovalLevel> </result>

Boolean operators. XQuery supports the basic boolean operations: and, or, and not. Here's a query that returns an approval level for items based on the item's price range. Note how the query uses the Boolean and operator to test the price ranges.

# see XQuery28.ixq in samples.zip # Find out the approval level for all items using Boolean operators: for $item in document("data/PO.xml")//item return <result> {$item/itemno} <ApprovalLevel> {if ($item/price >= 500 and $item/price < 10000) then 3 else if ($item/price >= 200 and $item/price < 500) then 2 else if ($item/price <= 200 and $item/price > 50) then 1 else 0} </ApprovalLevel> </result>

Sequence operators. XQuery defines a number of operators that apply specifically to sequences: item-at, concatenate, except, union, and intersect. The following example shows how to use the except operator.

# see XQuery29.ixq in samples.zip # Find all items except the 'Finishing good' type item let $allitem := document("data/items.xml")//item/itemno let $orderitem:= document("data/items.xml") //item[ItemType='Finishing good']/itemno return <result> {$allitem except $orderitem} </result>

Node Operators. There are a few node-specific operators defined in XQuery. They are node-after (>>), node-before (<<), and node-equal (==, !==). The following query demonstrates how to use the >> operator.

# see XQuery210.ixq in samples.zip # Find all the items that occur after item '0031' in document order: let $theitem := document("data/items.xml") //item[itemno='0031']/itemno for $allitem in document("data/items.xml")//item/itemno where $allitem >> $item return <result> {$allitem} </result>

Other Operators. XQuery defines a number of operators on QName, Date, Time, anyURI, base64Binary and hexBinary that are not covered here in this article.

Comment and Contribute






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



Thanks for your registration, follow us on our social networks to keep up-to-date