Chapter 2. Technologies i n XML 45
Prepare a table of contents for the document "cookbook.xml", c ontaining 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 Table2 -1 we have a very simple database.
Table 2-1 Example schema for relational database and Xquery comparison
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
query-blocks into FLWR-expressions. We illustrate this mapping by the following
query:
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")
Table name Relational data XML representation
SSNO
SNAME
<s>
<s_tuple>
<sno>
<sname>
PPNO
DESCRIP
<p>
<p_tuple>
<pno>
<descrip>
SP SNO
PNO
PRICE
<sp>
<sp_tuple>
<sno>
<pno>
<price>