16.10.2015 Views

Getting Started with DB2 Express-C

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

278 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>DB2</strong> <strong>Express</strong>-C<br />

Example 5<br />

The example illustrated in Listing 15.12 demonstrates again the XMLQUERY function<br />

which takes an XQuery FLWOR expression; however, note that this time we are returning<br />

not only XML, but also HTML.<br />

SELECT xmlquery('for $e in $c/Client/email[1]/text()<br />

return {$e}'<br />

passing contact as "c")<br />

FROM clients<br />

WHERE status = 'Gold'<br />

Listing 15.12 - An example returning XML and HTML<br />

The return clause of XQuery enables you to transform XML output as needed. Using the<br />

text()function in the first line indicates that only the text representation of the first email<br />

address of qualifying customers is of interest. The second line specifies that this<br />

information is to be surrounded by HTML paragraph tags.<br />

Example 6<br />

The following example uses the XMLELEMENT function to create a series of item<br />

elements, each of which contain sub-elements for the ID, brand name, and stock keeping<br />

unit (SKU) values obtained from corresponding columns in the items table. Basically, you<br />

can use the XMLELEMENT function when you want to convert relational data to XML data.<br />

This is illustrated in Listing 15.13.<br />

SELECT<br />

xmlelement (name "item", itemname),<br />

xmlelement (name "id", id),<br />

xmlelement (name "brand", brandname),<br />

xmlelement (name "sku", sku)<br />

FROM items<br />

WHERE srp < 100<br />

Listing 15.13 - An example using XMLELEMENT<br />

The query in Listing 15.13 would return the output as shown in Listing 15.14<br />

<br />

4272<br />

Classy<br />

981140<br />

<br />

…<br />

<br />

1193<br />

Natural

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!