XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0596006349
1st edition, , O'Reilly Media, Inc.
Chapter 1: Introduction to XQuery
<catalog> <product dept="WMN"> <number>557</number> <name language="en">Fleece Pullover</name> <colorChoices>navy black</colorChoices> </product> <product dept="ACC"> <number>563</number> <name language="en">Floppy Sun Hat</name> </product> <product dept="ACC"> <number>443</number> <name language="en">Deluxe Travel Bag</name> </product> <product dept="MEN"> <number>784</number> <name language="en">Cotton Dress Shirt</name> <colorChoices>white gray</colorChoices> <desc>Our <i>favorite</i> shirt!</desc> </product> </catalog>
<prices> <priceList effDate="2006-11-15"> <prod num="557"> <price currency="USD">29.99</price> <discount type="CLR">10.00</discount> </prod> <prod num="563"> <price currency="USD">69.99</price> </prod> <prod num="443"> <price currency="USD">39.99</price> <discount type="CLR">3.99</discount> </prod> </priceList> </prices>
<order num="00299432" date="2006-09-15" cust="0221A"> <item dept="WMN" num="557" quantity="1" color="navy"/> <item dept="ACC" num="563" quantity="1"/> <item dept="ACC" num="443" quantity="2"/> <item dept="MEN" num="784" quantity="1" color="white"/> <item dept="MEN" num="784" quantity="1" color="gray"/> <item dept="WMN" num="557" quantity="1" color="black"/> </order>
<product dept="WMN"> <number>557</number> <name language="en">Fleece Pullover</name> <colorChoices>navy black</colorChoices> </product> <product dept="ACC"> <number>563</number> <name language="en">Floppy Sun Hat</name> </product> <product dept="ACC"> <number>443</number> <name language="en">Deluxe Travel Bag</name> </product> <product dept="MEN"> <number>784</number> <name language="en">Cotton Dress Shirt</name> <colorChoices>white gray</colorChoices> <desc>Our <i>favorite</i> shirt!</desc> </product>
for $prod in doc("catalog.xml")/catalog/product where $prod/@dept = "ACC" order by $prod/name return $prod/name
for $product in doc("catalog.xml")/catalog/product let $name := $product/name where $product/@dept = "ACC" order by $name return $name
<ul>{ for $product in doc("catalog.xml")/catalog/product where $product/@dept='ACC' order by $product/name return $product/name }</ul>
<ul>{ for $product in doc("catalog.xml")/catalog/product where $product/@dept='ACC' order by $product/name return <li>{$product/name}</li> }</ul>
<ul>{ for $product in doc("catalog.xml")/catalog/product where $product/@dept='ACC' order by $product/name return <li>{data($product/name)}</li> }</ul>
<ul type="square">{ for $product in doc("catalog.xml")/catalog/product where $product/@dept='ACC' order by $product/name return <li class="{$product/@dept}">{data($product/name)}</li> }</ul>
for $item in doc("order.xml")//item let $name := doc("catalog.xml")//product[number = $item/@num]/name return <item num="{$item/@num}" name="{$name}" quan="{$item/@quantity}"/>
for $d in distinct-values(doc("order.xml")//item/@dept) let $items := doc("order.xml")//item[@dept = $d] order by $d return <department name="{$d}" totQuantity="{sum($items/@quantity)}"/>