XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0596006349
1st edition, , O'Reilly Media, Inc.
Chapter 5: Adding elements and attributes to results
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC'] return $prod/name
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC'] return $prod
<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> <p>A <i>huge</i> list of {count(doc("catalog.xml")//product)} products.</p> </html>
for $prod in doc("catalog.xml")/catalog/product return <li>number: {$prod/number}</li>
for $prod in doc("catalog.xml")/catalog/product return <li>{$prod/@dept}number: {$prod/number}</li>
for $prod in doc("catalog.xml")/catalog/product return <li>{$prod/@dept,"string",5+3,$prod/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>
<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>
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC'] return <product id="P{$prod/number}"> {$prod/(@*, *)} </product>
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC'] return <product> {$prod/(@*, * except number)} </product>
(: line below added to give $prod a value :) let $prod := doc("catalog.xml")//product[1] return <ul> { <li> <b> number:</b> { $prod/number } </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)} } }
for $dept in distinct-values(doc("catalog.xml")/catalog/product/@dept) return element {$dept} {doc("catalog.xml")/catalog/product[@dept = $dept]/name}