XQuery

XQuery

(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.

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