home contribute faq download

FunctX XQuery Functions

functx:change-element-names-deep

Changes the names of elements in an XML fragment

Google
Webdatypic.com

Description

The functx:change-element-names-deep function changes the names of elements that are in $nodes or among their descendants, based on a list of $oldNames to change from, and a list of $newNames to change to. They are positionally related, e.g. the first name in $oldNames is converted to the first name in $newNames. Names that do not appear in the $oldNames list are unchanged.

Each name must be specified as an xs:QName value. QNames can be constructed with calls to the xs:QName type constructor or the fn:QName function, as shown in the examples.

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 element(s) to change
$oldNames xs:QName* the sequence of names to change from
$newNames xs:QName* the sequence of names to change to
return value node()*

XQuery Function Declaration

See XSLT definition.
declare namespace functx = "http://www.functx.com";
declare function functx:change-element-names-deep
  ( $nodes as node()* ,
    $oldNames as xs:QName* ,
    $newNames as xs:QName* )  as node()* {

  if (count($oldNames) != count($newNames))
  then error(xs:QName('functx:Different_number_of_names'))
  else
   for $node in $nodes
   return if ($node instance of element())
          then element
                 {functx:if-empty
                    ($newNames[index-of($oldNames,
                                           node-name($node))],
                     node-name($node)) }
                 {$node/@*,
                  functx:change-element-names-deep($node/node(),
                                           $oldNames, $newNames)}
          else if ($node instance of document-node())
          then functx:change-element-names-deep($node/node(),
                                           $oldNames, $newNames)
          else $node
 } ;

Examples

declare namespace dty = "http://datypic.com";
let $in-xml-1 :=
<in-xml>
   <a>
      <b>b</b>
      <c>c</c>
   </a>
</in-xml>
let $in-xml-2 :=
<in-xml xmlns:dty="http://datypic.com">
   <a>
      <dty:b>b</dty:b>
      <c>c</c>
   </a>
</in-xml>
return
XQuery ExampleResults
functx:change-element-names-deep(
 $in-xml-1,
 xs:QName('b'),
 xs:QName('y'))
<in-xml>
  <a>
    <y>b</y>
    <c>c</c>
  </a>
</in-xml>
functx:change-element-names-deep(
 $in-xml-1,
 (xs:QName('a'),
   xs:QName('b'),xs:QName('c')),
 (xs:QName('x'),
   xs:QName('y'),xs:QName('z')))
<in-xml>
  <x>
    <y>b</y>
    <z>c</z>
  </x>
</in-xml>
functx:change-element-names-deep(
 $in-xml-2,
 (xs:QName('dty:b'),xs:QName('c')),
 (xs:QName('q'),
    QName('http://new','new:c')))
<in-xml>
  <a>
    <q>b</q>
    <new:c xmlns:new="http://new">c</new:c>
  </a>
</in-xml>

Depends On

functx:if-emptyThe first argument if it is not blank, otherwise the second argument

See Also

functx:change-element-nsChanges the namespace of XML elements
functx:change-element-ns-deepChanges the namespace of XML elements and its descendants

History

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

Recommended Reading:

XQuery