XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 1491915103
2nd edition, , O'Reilly Media, Inc.
Chapter 5: Adding Elements and Attributes to Results
Please note that the book contains many inline examples and informal tables that are not provided here.
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC'] return $prod/name
<name language="en">Floppy Sun Hat</name> <name language="en">Deluxe Travel Bag</name>
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC'] return $prod
<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>
<html> <h1>Product Catalog</h1> <ul>{ for $prod in doc("catalog.xml")/catalog/product return <li>number: {data($prod/number)}, name: {data($prod/name)}</li> }</ul> </html>
<html> <h1>Product Catalog</h1> <ul> <li>number: 557, name: Fleece Pullover</li> <li>number: 563, name: Floppy Sun Hat</li> <li>number: 443, name: Deluxe Travel Bag</li> <li>number: 784, name: Cotton Dress Shirt</li> </ul> </html>
<html> <h1>Product Catalog</h1> <p>A <i>huge</i> list of {count(doc("catalog.xml")//product)} products.</p> </html>
<html> <h1>Product Catalog</h1> <p>A <i>huge</i> list of 4 products.</p> </html>
for $prod in doc("catalog.xml")/catalog/product return <li>number: {$prod/number}</li>
<li>number: <number>557</number></li> <li>number: <number>563</number></li> <li>number: <number>443</number></li> <li>number: <number>784</number></li>
for $prod in doc("catalog.xml")/catalog/product return <li>{$prod/@dept}number: {$prod/number}</li>
<li dept="WMN">number: <number>557</number></li> <li dept="ACC">number: <number>563</number></li> <li dept="ACC">number: <number>443</number></li> <li dept="MEN">number: <number>784</number></li>
for $prod in doc("catalog.xml")/catalog/product return <li>{$prod/@dept, "string", 5+3, $prod/number}</li>
<li dept="WMN">string 8<number>557</number></li> <li dept="ACC">string 8<number>563</number></li> <li dept="ACC">string 8<number>443</number></li> <li dept="MEN">string 8<number>784</number></li>
<html> <h1 class="itemHdr">Product Catalog</h1> <ul>{ for $prod in doc("catalog.xml")/catalog/product return <li dep="{$prod/@dept}">number: {data($prod/number) }, name: {data($prod/name)}</li> }</ul> </html>
<html> <h1 class="itemHdr">Product Catalog</h1> <ul> <li dep="WMN">number: 557, name: Fleece Pullover</li> <li dep="ACC">number: 563, name: Floppy Sun Hat</li> <li dep="ACC">number: 443, name: Deluxe Travel Bag</li> <li dep="MEN">number: 784, name: Cotton Dress Shirt</li> </ul> </html>
<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"> <xhtml:h1 class="itemHdr">Product Catalog</xhtml:h1> <xhtml:ul>{ for $prod in doc("catalog.xml")/catalog/product return <xhtml:li class="{$prod/@dept}">number: { data($prod/number)}</xhtml:li> }</xhtml:ul> </xhtml:html>
<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"> <xhtml:h1 class="itemHdr">Product Catalog</xhtml:h1> <xhtml:ul> <xhtml:li class="WMN">number: 557</xhtml:li> <xhtml:li class="ACC">number: 563</xhtml:li> <xhtml:li class="ACC">number: 443</xhtml:li> <xhtml:li class="MEN">number: 784</xhtml:li> </xhtml:ul> </xhtml:html>
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC'] return <product id="P{$prod/number}"> {$prod/(@*, *)} </product>
<product dept="ACC" id="P563"> <number>563</number> <name language="en">Floppy Sun Hat</name> </product> <product dept="ACC" id="P443"> <number>443</number> <name language="en">Deluxe Travel Bag</name> </product>
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC'] return <product> {$prod/(@*, * except number)} </product>
<product dept="ACC"> <name language="en">Floppy Sun Hat</name> </product> <product dept="ACC"> <name language="en">Deluxe Travel Bag</name> </product>
<ul> { <li> <b> number:</b> { "557" } </li> } </ul>
element html { element h1 { "Product Catalog" }, element ul { for $prod in doc("catalog.xml")/catalog/product return element li {"number:", data($prod/number), ", name:", data($prod/name)} } }
<html> <h1>Product Catalog</h1> <ul> <li>number: 557 , name: Fleece Pullover</li> <li>number: 563 , name: Floppy Sun Hat</li> <li>number: 443 , name: Deluxe Travel Bag</li> <li>number: 784 , name: Cotton Dress Shirt</li> </ul> </html>
for $dept in distinct-values(doc("catalog.xml")/catalog/product/@dept) return element {$dept} {doc("catalog.xml")/catalog/product[@dept = $dept]/name}
<WMN> <name language="en">Fleece Pullover</name> </WMN> <ACC> <name language="en">Floppy Sun Hat</name> <name language="en">Deluxe Travel Bag</name> </ACC> <MEN> <name language="en">Cotton Dress Shirt</name> </MEN>