XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0596006349
1st edition, , O'Reilly Media, Inc.
Chapter 21: Working with other XML components
<?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>2006-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>
<?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>2006-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>
document { element product { attribute dept { "ACC" }, element number { 563 }, element name { attribute language {"en"}, "Floppy Sun Hat"} } }
<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 ()
<root> <h1><![CDATA[Catalog & Price List from <catalog>]]></h1> <h1>Catalog & Price List from <catalog></h1> </root>
if (doc("catalog.xml")//product) then <h1><![CDATA[Catalog & Price List from <catalog>]]></h1> else <h1>No catalog items to display</h1>