Example: Publishing data with XQuery
This example shows how you can construct XML values suitable for publishing not only with SQL/XML publishing functions but also with XQuery expressions.
The following XQuery expression uses the delete updating
expression to create a simple customer list. The expression removes
the customer ID, address, assistant information, and non-work phone
numbers from the customer information and moves the country attribute
from the address node element to the customerinfo node
element.
xquery
<phonelist>
{for $d in db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo
return
transform
copy $mycust := $d
modify (
do delete ( $mycust/@Cid ,
$mycust/addr ,
$mycust/assistant ,
$mycust/phone[@type!="work"] ),
do insert attribute country { $mycust/addr/@country } into $mycust )
return $mycust }
</phonelist>
Note that although address element
is deleted, the address information is accessible within the modify clause
and the country attribute from the address element
is used by the insert expression.
The query returns the following results:
<phonelist>
<customerinfo country="Canada">
<name>Kathy Smith</name>
<phone type="work">416-555-1358</phone>
</customerinfo>
<customerinfo country="Canada">
<name>Kathy Smith</name>
<phone type="work">905-555-7258</phone>
</customerinfo>
<customerinfo country="Canada">
<name>Jim Noodle</name>
<phone type="work">905-555-7258</phone>
</customerinfo>
<customerinfo country="Canada">
<name>Robert Shoemaker</name>
<phone type="work">905-555-7258</phone>
</customerinfo>
<customerinfo country="Canada">
<name>Matt Foreman</name>
<phone type="work">905-555-4789</phone>
</customerinfo>
<customerinfo country="Canada">
<name>Larry Menard</name>
<phone type="work">905-555-9146</phone>
</customerinfo>
</phonelist>