XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 1491915103
2nd edition, , O'Reilly Media, Inc.
Chapter 1: Introduction to XQuery
Please note that the book contains many inline examples and informal tables that are not provided here.
<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="2015-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="2015-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
<name language="en">Deluxe Travel Bag</name> <name language="en">Floppy Sun Hat</name>
for $prod in doc("catalog.xml")/catalog/product let $name := $prod/name where $prod/@dept = "ACC" order by $name return $name
<ul>{ for $prod in doc("catalog.xml")/catalog/product where $prod/@dept='ACC' order by $prod/name return $prod/name }</ul>
<ul> <name language="en">Deluxe Travel Bag</name> <name language="en">Floppy Sun Hat</name> </ul>
<ul>{ for $prod in doc("catalog.xml")/catalog/product where $prod/@dept='ACC' order by $prod/name return <li>{$prod/name}</li> }</ul>
<ul> <li><name language="en">Deluxe Travel Bag</name></li> <li><name language="en">Floppy Sun Hat</name></li> </ul>
<ul>{ for $prod in doc("catalog.xml")/catalog/product where $prod/@dept='ACC' order by $prod/name return <li>{data($prod/name)}</li> }</ul>
<ul> <li>Deluxe Travel Bag</li> <li>Floppy Sun Hat</li> </ul>
<ul type="square">{ for $prod in doc("catalog.xml")/catalog/product where $prod/@dept='ACC' order by $prod/name return <li class="{$prod/@dept}">{data($prod/name)}</li> }</ul>
<ul type="square"> <li class="ACC">Deluxe Travel Bag</li> <li class="ACC">Floppy Sun Hat</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}"/>
<item num="557" name="Fleece Pullover" quan="1"/> <item num="563" name="Floppy Sun Hat" quan="1"/> <item num="443" name="Deluxe Travel Bag" quan="2"/> <item num="784" name="Cotton Dress Shirt" quan="1"/> <item num="784" name="Cotton Dress Shirt" quan="1"/> <item num="557" name="Fleece Pullover" quan="1"/>
xquery version "3.0"; for $i in doc("order.xml")//item let $d := $i/@dept group by $d order by $d return <department name="{$d}" totQuantity="{sum($i/@quantity)}"/>
<department name="ACC" totQuantity="3"/> <department name="MEN" totQuantity="2"/> <department name="WMN" totQuantity="2"/>