home contribute faq download

FunctX XQuery Functions

functx:change-element-ns-deep

Changes the namespace of XML elements and its descendants

Google
Webdatypic.com

Description

The functx:change-element-ns-deep function changes the namespace of the XML elements in $nodes to $newns. Unlike the functx:change-element-ns function, it also changes the namespace of all their descendant elements.

Note: this function is intended to change the way elements appear in the results of a query, not to update them in an XML database. To update your XML database, you should use the implementation-specific update functions of your processor.

Arguments and Return Type

NameTypeDescription
$nodes node()* the nodes to change
$newns xs:string the new namespace
$prefix xs:string the prefix to use for the new namespace
return value node()*

XQuery Function Declaration

See XSLT definition.
declare namespace functx = "http://www.functx.com";
declare function functx:change-element-ns-deep
  ( $nodes as node()* ,
    $newns as xs:string ,
    $prefix as xs:string )  as node()* {

  for $node in $nodes
  return if ($node instance of element())
         then (element
               {QName ($newns,
                          concat($prefix,
                                    if ($prefix = '')
                                    then ''
                                    else ':',
                                    local-name($node)))}
               {$node/@*,
                functx:change-element-ns-deep($node/node(),
                                           $newns, $prefix)})
         else if ($node instance of document-node())
         then functx:change-element-ns-deep($node/node(),
                                           $newns, $prefix)
         else $node
 } ;

Examples

let $in-xml :=
<bar:a xmlns:bar="http://bar">
   <bar:b>557</bar:b>
   <bar:c>xyz</bar:c>
</bar:a>
return
XQuery ExampleResults
functx:change-element-ns-deep(
     $in-xml, 'http://foo','')
<a xmlns="http://foo">
  <b>557</b>
  <c>xyz</c>
</a>
functx:change-element-ns-deep(
     $in-xml, 'http://foo','foo')
<foo:a xmlns:foo="http://foo">
  <foo:b>557</foo:b>
  <foo:c>xyz</foo:c>
</foo:a>

See Also

functx:change-element-nsChanges the namespace of XML elements
functx:change-element-names-deepChanges the names of elements in an XML fragment

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Datypic XQuery Services

Recommended Reading:

XQuery