Prepare a table of contents for the document "cookbook.xml", containing nested sections and their titles:
LET $b := document("cookbook.xml")
RETURN <toc>
filter($b, $b//section $b//section/title $b//section/title/text() ) </toc>
Querying relational data
The best way to demonstrate XQuery is showing how it can be used to query a relational database. In Table
Table 2-1 Example schema for relational database and Xquery comparison
Table name | Relational data | XML representation |
|
|
|
S | SNO | <s> |
| SNAME | <s_tuple> |
|
| <sno> |
|
| <sname> |
|
|
|
P | PNO | <p> |
| DESCRIP | <p_tuple> |
|
| <pno> |
|
| <descrip> |
|
|
|
SP | SNO | <sp> |
| PNO | <sp_tuple> |
| PRICE | <sno> |
|
| <pno> |
|
| <price> |
|
|
|
SQL is the standard relational database language. In many cases, SQL queries can be converted to XQuery syntax in a straightforward way by mapping SQL
Find the part numbers of gears in numeric order:
SQL version:
SELECT pno
FROM p
WHERE descrip LIKE 'Gear'
ORDER BY pno;
XQuery version:
FOR $p IN document("p.xml")//p_tuple
WHERE contains($p/descrip, "Gear")