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.
<?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>
declare function local:createCommentElement ($commentToAdd as comment()) as element() { <comment>{string($commentToAdd)}</comment> };
let $count := count(doc("catalog.xml")//product) (: unordered list :) return <ul> <!-- {concat(" List of ", $count, " products ")} --> {comment{concat(" List of ", $count, " products ")}} </ul>
<ul> <!-- {concat(" List of ", $count, " products ")} --> <!-- List of 4 products --> </ul>
<?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>
declare function local:displayPIValue ($pi as processing-instruction())as xs:string { concat("Target is ", name($pi), " and content is ", string($pi)) };
<ul>{ <?doc-processor version="4.3"?>, processing-instruction doc-processor2 {'version="4.3"'}, processing-instruction {concat("doc-processor", "3")} {concat('version="', '4.3', '"')} }</ul>
<ul> <?doc-processor version="4.3"?> <?doc-processor2 version="4.3"?> <?doc-processor3 version="4.3"?> </ul>
document { element product { attribute dept { "ACC" }, element number { 563 }, element name { attribute language {"en"}, "Floppy Sun Hat"} } }
<product dept="ACC"> <number>563</number> <name language="en">Floppy Sun Hat</name> </product>
<desc>Our <i>favorite</i> shirt!</desc>
declare function local:displayTextNodeContent ($textNode as text()) as xs:string { concat("Content of the text node is ", $textNode) };
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 () } };
if (doc("catalog.xml")//product[@dept='ACC']) then <h1>Accessories & Misc List from <catalog></h1> else ()
<h1>Accessories & Misc List from <catalog></h1>
<h1><![CDATA[Catalog & Price List from <catalog>]]></h1> <h1>Catalog & Price List from <catalog></h1>
if (doc("catalog.xml")//product) then <h1><![CDATA[Catalog & Price List from <catalog>]]></h1> else <h1>No catalog items to display</h1>
<h1>Catalog & Price List from <catalog></h1>