Datypic Logo


Priscilla Walmsley (pwalmsley@datypic.com)

ISBN: 0596006349

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

Chapter 20: Working with qualified names, URIs and IDs

Example 20-1. Namespaces in XML (names.xml)
  <pre:prefixed xmlns="http://datypic.com/unpre"
    <unprefixed pre:prefAttr="a" noNSAttr="b">123</unprefixed>
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
unprefixedNamespace: http://datypic.com/unpre
Prefix: empty
Local part: unprefixed
@pre:prefAttrNamespace: http://datypic.com/pre
Prefix: pre
Local part: prefAttr
@noNSAttrNamespace: empty
Prefix: empty
Local part: noNSAttr
noNSAttrnoNSAttrA zero-length string
Example 20-2. Using names as result data
  for $prod in doc("catalog.xml")//product
   return (<p>Product # {string($prod/number)}</p>,
             for $child in $prod/(* except number)
             return <li>{local-name($child)}: {string($child)}</li>
Example 20-3. Document using xml:base (http://datypic.com/cats.xml)
  <catalog name="ACC" xml:base="http://example.org/ACC/">
    <product number="443" href="prod443.html"/>
    <product number="563" href="prod563.html"/>
  <catalog name="WMN" xml:base="http://example.org/WMN/">
    <product number="557" href="prod557.html"/>
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">{
    <pre:x><pre:y>123</pre:y></pre:x>, "http://new")
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}
    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">{
    <pre:x><pre:y>123</pre:y></pre:x>, "http://new")
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)
  <section id="preface">This book introduces XQuery...
    The examples are downloadable<fnref ref="fn1"/>...
  <section id="context">...</section>
  <section id="language">...Expressions, introduced
   in <secRef refs="context"/>, are...
  <section id="types">...As described in
    <secRef refs="context language"/>, you can...
  <fn fnid="fn1">See http://datypic.com.</fn>
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 :)
Datypic XQuery Services