XQuery
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0596006349
1st edition, , O'Reilly Media, Inc.
Chapter 20: Working with qualified names, URIs and IDs
<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>
Node | node-name returns an xs:QName with: | name returns | local-name returns | namespace-uri returns |
---|---|---|---|---|
noNamespace | Namespace: empty Prefix: empty Local part:+noNamespace+ | noNamespace | noNamespace | A zero-length string |
pre:prefixed | Namespace: http://datypic.com/pre Prefix: pre Local part: prefixed | pre:prefixed | prefixed | http://datypic.com/pre |
unprefixed | Namespace: http://datypic.com/unpre Prefix: empty Local part: unprefixed | unprefixed | unprefixed | http://datypic.com/unpre |
@pre:prefAttr | Namespace: http://datypic.com/pre Prefix: pre Local part: prefAttr | pre:prefAttr | prefAttr | http://datypic.com/pre |
@noNSAttr | Namespace: empty Prefix: empty Local part: noNSAttr | noNSAttr | noNSAttr | A zero-length string |
<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>
<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>
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>
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>
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)
<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>
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])