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 2-1 we have a very simple database.

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 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")

Chapter 2. Technologies in XML 45

Page 61
Image 61
IBM Version 5 manual Querying relational data, Example schema for relational database and Xquery comparison