•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the XML, XSLT and XPATH section within the Software Development category of DaniWeb, a massive community of 455,973 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,828 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our XML, XSLT and XPATH advertiser: Programming Forums
Views: 4617 | Replies: 32
![]() |
•
•
Join Date: Oct 2007
Posts: 76
Reputation:
Rep Power: 2
Solved Threads: 0
DB2 treats XQuery as a first-class language, allowing users to write Xquery expressions directly rather than requiring that users embed or wrap XQueries in SQL statements. DB2's query engine processes XQueries natively, meaning that it parses, evaluates, and optimizes XQueries without ever translating them into SQL behind the scenes. You can check http://www.ibm.com/developerworks/db...-0603saracco2/ to know how to use several popular SQL/XML expressions to query DB2 XML data XQuery.
•
•
Join Date: Oct 2007
Posts: 76
Reputation:
Rep Power: 2
Solved Threads: 0
XQuery provides several different kinds of expressions that may be combined in any way you like. Each expression returns a list of values that can be used as input to other expressions. The result of the outermost expression is the result of the query.
There are manly two important kinds of XQuery expressions:
i) FLWOR expressions
ii) Path expressions.
A FLWOR expression is much like a SELECT-FROM-WHERE expression in SQL -- it is used to iterate through a list of items and to optionally return something that is computed from each item. A Path expression, on the other hand, navigates through a hierarchy of XML elements and returns the elements that are found at the end of the path.
Like a SELECT-FROM-WHERE expression in SQL, an XQuery FLWOR expression may contain several clauses that begin with certain keywords. The following keywords are used to begin clauses in a FLWOR expression:
· for: Iterates through an input sequence, binding a variable to each input item in turn
· let: Declares a variable and assigns it a value, which may be a list containing multiple items
· where: Specifies criteria for filtering query results
· order by: Specifies the sort order of the result
· return: Defines the result to be returned
For example FLWOR expression to retrieve client fax data:
xquery for $y in db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client/fax return $y
A Path expression in XQuery consists of a series of "steps," separated by slash characters. In its simplest form, each step navigates downward in an XML hierarchy to find the children of the elements returned by the previous step. Each step in a path expression may also contain a predicate that filters the elements that are returned by that step, retaining only elements that satisfy some condition.
For example Path expression to retrieve client fax data:
xquery
db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client/fax
There are manly two important kinds of XQuery expressions:
i) FLWOR expressions
ii) Path expressions.
A FLWOR expression is much like a SELECT-FROM-WHERE expression in SQL -- it is used to iterate through a list of items and to optionally return something that is computed from each item. A Path expression, on the other hand, navigates through a hierarchy of XML elements and returns the elements that are found at the end of the path.
Like a SELECT-FROM-WHERE expression in SQL, an XQuery FLWOR expression may contain several clauses that begin with certain keywords. The following keywords are used to begin clauses in a FLWOR expression:
· for: Iterates through an input sequence, binding a variable to each input item in turn
· let: Declares a variable and assigns it a value, which may be a list containing multiple items
· where: Specifies criteria for filtering query results
· order by: Specifies the sort order of the result
· return: Defines the result to be returned
For example FLWOR expression to retrieve client fax data:
xquery for $y in db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client/fax return $y
A Path expression in XQuery consists of a series of "steps," separated by slash characters. In its simplest form, each step navigates downward in an XML hierarchy to find the children of the elements returned by the previous step. Each step in a path expression may also contain a predicate that filters the elements that are returned by that step, retaining only elements that satisfy some condition.
For example Path expression to retrieve client fax data:
xquery
db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client/fax
•
•
Join Date: Oct 2007
Posts: 76
Reputation:
Rep Power: 2
Solved Threads: 0
The XQuery where clause enables you to filter results based on the value of the zip element in your XML documents. Consider we stored below mentioned xml document in CONTACTINFO column of CLIENTS table.
<client>
<Address>
<street>5401 Julio Ave.</street>
<city>San Jose</city>
<state>CA</state>
<zip>95116</zip>
</Address>
<Phone>
<work>4084630000</work>
<home>4081111111</home>
<cell>4082222222</cell>
</phone>
<fax>4087778888</fax>
<email>test@yahoo.com</email>
</client>
Below example explains how to obtain only the addresses of the customers who live in zip code 95116 using FLWOR expression.
xquery
for $y in db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client/Address
where $y/zip="95116"
return $y
The “for” clause binds the variable “$y” to each address in turn. The “where” clause contains a small path expression that navigates from each address to its nested zip element. The “where” clause is true (and the address is retained) only if the value of this zip element is equal to 95116.
The same result could be obtained by adding a predicate to the path expression,
xquery db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client/Address[zip="95116"]
You can get the e-mail information for customers who live in a specific zip code in New York City (10011) or anywhere in the city of San Jose using following queries:
Using FLWOR expression:
xquery
for $y in db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client
where $y/Address/zip="10011" or $y/Address/city="San Jose"
return $y/email
Using path expression:
xquery
db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client[Address/zip="10011"
or Address/city="San Jose"]/email;
For more information you can follow the link:
http://www.ibm.com/developerworks/db...m-0604saracco/
<client>
<Address>
<street>5401 Julio Ave.</street>
<city>San Jose</city>
<state>CA</state>
<zip>95116</zip>
</Address>
<Phone>
<work>4084630000</work>
<home>4081111111</home>
<cell>4082222222</cell>
</phone>
<fax>4087778888</fax>
<email>test@yahoo.com</email>
</client>
Below example explains how to obtain only the addresses of the customers who live in zip code 95116 using FLWOR expression.
xquery
for $y in db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client/Address
where $y/zip="95116"
return $y
The “for” clause binds the variable “$y” to each address in turn. The “where” clause contains a small path expression that navigates from each address to its nested zip element. The “where” clause is true (and the address is retained) only if the value of this zip element is equal to 95116.
The same result could be obtained by adding a predicate to the path expression,
xquery db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client/Address[zip="95116"]
You can get the e-mail information for customers who live in a specific zip code in New York City (10011) or anywhere in the city of San Jose using following queries:
Using FLWOR expression:
xquery
for $y in db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client
where $y/Address/zip="10011" or $y/Address/city="San Jose"
return $y/email
Using path expression:
xquery
db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client[Address/zip="10011"
or Address/city="San Jose"]/email;
For more information you can follow the link:
http://www.ibm.com/developerworks/db...m-0604saracco/
![]() |
•
•
•
•
•
•
•
•
DaniWeb XML, XSLT and XPATH Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- XML Management (RSS, Web Services and SOAP)
- Database Design : DB2 + PHP + Windows? (Database Design)
- Unable to uninstall DB2 Express-c (Win XP Home) (Windows NT / 2000 / XP / 2003)
- Complete guideline book for the DB2 (Shell Scripting)
- shell script manual for db2 (Shell Scripting)
- extracting db2 table records to csv (Shell Scripting)
- Simple question about using JDBC to access DB2 (Java)
- Immediate project for IBM DB2 UDB database administrator (Software Development Job Offers)
Other Threads in the XML, XSLT and XPATH Forum
- Previous Thread: Xpath
- Next Thread: SQL/XML Query


Linear Mode