XQuery

XQuery

(pwalmsley@datypic.com)

ISBN: 0596006349

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

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>
Table 20-2. Examples of the name functions
Nodenode-name returns an xs:QName with:name returnslocal-name returnsnamespace-uri returns
noNamespaceNamespace: empty
Prefix: empty
Local part:+noNamespace+
noNamespacenoNamespaceA zero-length string
pre:prefixedNamespace: http://datypic.com/pre
Prefix: pre
Local part: prefixed
pre:prefixedprefixedhttp://datypic.com/pre
unprefixedNamespace: http://datypic.com/unpre
Prefix: empty
Local part: unprefixed
unprefixedunprefixedhttp://datypic.com/unpre
@pre:prefAttrNamespace: http://datypic.com/pre
Prefix: pre
Local part: prefAttr
pre:prefAttrprefAttrhttp://datypic.com/pre
@noNSAttrNamespace: empty
Prefix: empty
Local part: noNSAttr
noNSAttrnoNSAttrA zero-length string
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>
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: 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)
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: 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])
Datypic XQuery Services