XQuery

XQuery

(pwalmsley@datypic.com)

ISBN: 0596006349

1st edition, , O'Reilly Media, Inc.

Chapter 5: Adding elements and attributes to results

Example 5-1. Including elements from the input document
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC']
return $prod/name
Example 5-2. Including complex elements from the input document
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC']
return $prod
Example 5-3. Constructing elements using XML-like syntax
<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>
Example 5-4. Embedded direct element constructors
<html>
  <h1>Product Catalog</h1>
  <p>A <i>huge</i> list of {count(doc("catalog.xml")//product)} products.</p>
</html>
Example 5-5. Enclosed expressions that evaluate to elements
for $prod in doc("catalog.xml")/catalog/product
return <li>number: {$prod/number}</li>
Example 5-6. Enclosed expressions that evaluate to attributes
for $prod in doc("catalog.xml")/catalog/product
return <li>{$prod/@dept}number: {$prod/number}</li>
Example 5-7. Enclosed expressions with multiple subexpressions
for $prod in doc("catalog.xml")/catalog/product
return <li>{$prod/@dept,"string",5+3,$prod/number}</li>
Example 5-8. Specifying attributes directly using XML-like syntax
<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>
Example 5-9. Using a namespace declaration in a constructor
<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>
Example 5-10. Adding an attribute to an element
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC']
return <product id="P{$prod/number}">
          {$prod/(@*, *)}
       </product>
Example 5-11. Removing a child from an element
for $prod in doc("catalog.xml")/catalog/product[@dept = 'ACC']
return <product>
          {$prod/(@*, * except number)}
       </product>
Example 5-12. Constructor with boundary whitespace
(: 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>
Example 5-13. Simple computed constructor
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)}
  }
}
Example 5-14. Turning content into markup
for $dept in distinct-values(doc("catalog.xml")/catalog/product/@dept)
return element {$dept}
               {doc("catalog.xml")/catalog/product[@dept = $dept]/name}
Datypic XQuery Services