ISBN: 0596006349

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

Chapter 21: Working with other XML components

Example 21-1. XML document with comments (comment.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a business document -->
<b:businessDocument xmlns:b="http://datypic.com/b">
     <!-- date created --><b:date>2006-10-15</b:date>
Example 21-2. Function that processes comments
declare function local:createCommentElement
  ($commentToAdd as comment()) as element() {
Example 21-3. XML comment constructors
let $count := count(doc("catalog.xml")//product)
(: unordered list :)
return <ul>
         <!-- {concat(" List of ", $count, " products ")} -->
         {comment{concat(" List of ", $count, " products ")}}
Example 21-4. XML document with processing instructions (pi.xml)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="formatter.xsl"?>
<b:businessDocument xmlns:b="http://datypic.com/b">
     <?doc-processor appl="BDS" version="4.3"?>
Example 21-5. Function that displays processing instructions
declare function local:displayPIValue
  ($pi as processing-instruction())as xs:string {
  concat("Target is ", name($pi),
         " and content is ", string($pi))
Example 21-6. Processing instruction constructors
  <?doc-processor version="4.3"?>,
  processing-instruction doc-processor2 {'version="4.3"'},
  processing-instruction {concat("doc-processor", "3")}
        {concat('version="', '4.3', '"')}
Example 21-7. Computed document constructor
document {
  element product {
    attribute dept { "ACC" },
    element number { 563 },
    element name { attribute language {"en"}, "Floppy Sun Hat"}
Example 21-8. Text nodes in XML (desc.xml)
<desc>Our <i>favorite</i> shirt!</desc>
Example 21-9. Function that displays text nodes
declare function local:displayTextNodeContent
  ($textNode as text()) as xs:string {
  concat("Content of the text node is ", $textNode)
Example 21-10. Testing for text nodes
declare function local:change-i-to-em
  ($node as element()) as node() {
  element {node-name($node)} {
    for $child in $node/node()
    return if ($child instance of text())
           then $child
           else if ($child instance of element(i))
                then <em>{$child/@*,$child/node()}</em>
                else if ($child instance of element())
                     then local:change-i-to-em($child)
                     else ()
Example 21-11. Query with XML entities
if (doc("catalog.xml")//product[@dept='ACC'])
then <h1>Accessories &amp; Misc List from &lt;catalog&gt;</h1>
else ()
Example 21-12. Two equivalent h1 elements, one with CDATA section
<h1><![CDATA[Catalog & Price List from <catalog>]]></h1>
<h1>Catalog &amp; Price List from &lt;catalog&gt;</h1>
Example 21-13. Query with CDATA section
if (doc("catalog.xml")//product)
then <h1><![CDATA[Catalog & Price List from <catalog>]]></h1>
else <h1>No catalog items to display</h1>
Datypic XQuery Services