XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 1491915103
2nd edition, , O'Reilly Media, Inc.
Chapter 22: Working with Other XML Constructs
Please note that the book contains many inline examples and informal tables that are not provided here.
Example 22-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"> <b:header> <!-- date created --><b:date>2015-10-15</b:date> </b:header> </b:businessDocument>
Example 22-2. Function that processes comments
declare function local:createCommentElement ($commentToAdd as comment()) as element() { <comment>{string($commentToAdd)}</comment> };
Example 22-3. XML comment constructors
Query
let $count := count(doc("catalog.xml")//product) (: unordered list :) return <ul> <!-- {concat(" List of ", $count, " products ")} --> {comment{concat(" List of ", $count, " products ")}} </ul>
Results
<ul> <!-- {concat(" List of ", $count, " products ")} --> <!-- List of 4 products --> </ul>
Example 22-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"> <b:header> <?doc-processor appl="BDS" version="4.3"?> <b:date>2015-10-15</b:date> </b:header> </b:businessDocument>
Example 22-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 22-6. Processing-instruction constructors
Query
<ul>{ <?doc-processor version="4.3"?>, processing-instruction doc-processor2 {'version="4.3"'}, processing-instruction {concat("doc-processor", "3")} {concat('version="', '4.3', '"')} }</ul>
Results
<ul> <?doc-processor version="4.3"?> <?doc-processor2 version="4.3"?> <?doc-processor3 version="4.3"?> </ul>
Example 22-7. Computed document constructor
Query
document { element product { attribute dept { "ACC" }, element number { 563 }, element name { attribute language {"en"}, "Floppy Sun Hat"} } }
Results
<product dept="ACC"> <number>563</number> <name language="en">Floppy Sun Hat</name> </product>
Example 22-8. Text nodes in XML (desc.xml)
<desc>Our <i>favorite</i> shirt!</desc>
Example 22-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 22-10. Testing for text nodes
declare function local:change-i-to-em ($node as element()) as node() { element {node-name($node)} { $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 22-11. Query with XML entities
Query
if (doc("catalog.xml")//product[@dept='ACC']) then <h1>Accessories & Misc List from <catalog></h1> else ()
Results
<h1>Accessories & Misc List from <catalog></h1>
Example 22-12. Two equivalent
h1
elements, one with a CDATA section<h1><![CDATA[Catalog & Price List from <catalog>]]></h1> <h1>Catalog & Price List from <catalog></h1>
Example 22-13. Query with CDATA section
Query
if (doc("catalog.xml")//product) then <h1><![CDATA[Catalog & Price List from <catalog>]]></h1> else <h1>No catalog items to display</h1>
Results
<h1>Catalog & Price List from <catalog></h1>