D A T Y P I C
XQuery

XQuery

Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0596006349
1st edition, 2007, O'Reilly Media, Inc.
Amazon.com
Buy at 37% off list price


Chapter 20: Working with Qualified Names, URIs and IDs

Example 20-1. Namespaces in XML (names.xml)

<noNamespace>
  <pre:prefixed xmlns="http://datypic.com/unpre"
           xmlns:pre="http://datypic.com/pre">
    <unprefixed pre:prefAttr="a" noNSAttr="b">123</unprefixed>
  </pre:prefixed>
</noNamespace>


Example 20-2. Using names as result data

<html>{
  for $prod in doc("catalog.xml")//product
   return (<p>Product # {string($prod/number)}</p>,
           <ul>{
             for $child in $prod/(* except number)
             return <li>{local-name($child)}: {string($child)}</li>
           }</ul>)
}</html>


Example 20-3. Document using xml:base (http://datypic.com/cats.xml)

<catalogs>
  <catalog name="ACC" xml:base="http://example.org/ACC/">
    <product number="443" href="prod443.html"/>
    <product number="563" href="prod563.html"/>
  </catalog>
  <catalog name="WMN" xml:base="http://example.org/WMN/">
    <product number="557" href="prod557.html"/>
  </catalog>
</catalogs>


Example 20-4. XML document with IDs and IDREFs (book.xml)

<book>
  <section id="preface">This book introduces XQuery...
    The examples are downloadable<fnref ref="fn1"/>...
  </section>
  <section id="context">...</section>
  <section id="language">...Expressions, introduced
   in <secRef refs="context"/>, are...
  </section>
  <section id="types">...As described in
    <secRef refs="context language"/>, you can...
  </section>
  <fn fnid="fn1">See http://datypic.com.</fn>
</book>


Useful Function. change-element-ns (see also functx:change-element-ns)

declare namespace functx = "http://www.functx.com";
declare function functx:change-element-ns
($element as element(), $newns as xs:string) as element()
 {
   let $newName := QName($newns, local-name($element))
   return (element {$newName} {$element/@*, $element/node()})
 };

(: Example call :)
<test xmlns:pre="pre">{
  functx:change-element-ns(
    <pre:x><pre:y>123</pre:y></pre:x>, "http://new")
}</test>


Useful Function. change-element-ns-deep (see also functx:change-element-ns-deep)

declare namespace functx = "http://www.functx.com";
declare function functx:change-element-ns-deep
($element as element(), $newns as xs:string) as element() {
  let $newName := QName ($newns, local-name($element))
  return (element {$newName}
   {$element/@*,
    for $child in $element/node()
    return if ($child instance of element())
           then functx:change-element-ns-deep($child, $newns)
           else $child
    }
  )
};

(: Example call :)
<test xmlns:pre="pre">{
  functx:change-element-ns-deep(
    <pre:x><pre:y>123</pre:y></pre:x>, "http://new")
}</test>


Useful Function. get-ID (see also functx:id-from-element)

declare namespace functx = "http://www.functx.com";
declare function functx:get-ID($element as element()?) as xs:ID?
{ data($element/@*[id(.) is ..])};

(: Example call :)
functx:get-ID(doc("book.xml")//section[1])


Useful Function. open-ref-document (see also functx:open-ref-document)

declare namespace functx = "http://www.functx.com";
declare function functx:open-ref-document
 ($refNode as node()) as document-node()
 {
   let $absoluteURI := resolve-uri($refNode, base-uri($refNode))
   return doc($absoluteURI)
 };

(: Example call :)
let $ref := doc("http://datypic.com/cats.xml")/catalogs/catalog[1]/product[1]/@href
return functx:open-ref-document($ref)