home contribute faq download

FunctX XSLT Functions

functx:change-element-names-deep

Changes the names of elements in an XML fragment

Google
Webxsltfunctions.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.

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()*

XSLT Function Declaration

See XQuery definition.
<xsl:function name="functx:change-element-names-deep" as="node()*"
              xmlns:functx="http://www.functx.com">
  <xsl:param name="nodes" as="node()*"/>
  <xsl:param name="oldNames" as="xs:QName*"/>
  <xsl:param name="newNames" as="xs:QName*"/>

  <xsl:if test="count($oldNames) != count($newNames)">
    <xsl:sequence select="error(
         xs:QName('functx:Different_number_of_names'))"/>
  </xsl:if>
  <xsl:for-each select="$nodes">
    <xsl:variable name="node" select="."/>
    <xsl:choose>
      <xsl:when test="$node instance of element()">
        <xsl:variable name="theName"
                      select="functx:if-empty
                    ($newNames[index-of($oldNames, node-name($node))],
                     node-name($node))"/>
        <xsl:element name="{$theName}"
                     namespace="{namespace-uri-from-QName($theName)}">
           <xsl:sequence select="($node/@*,
                  functx:change-element-names-deep($node/node(),
                                           $oldNames, $newNames))"/>
        </xsl:element>
      </xsl:when>
      <xsl:when test="$node instance of document-node()">
        <xsl:document>
           <xsl:sequence select="functx:change-element-names-deep(
                $node/node(), $oldNames, $newNames)"/>
        </xsl:document>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="$node"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>

</xsl:function>

Examples

<xsl:stylesheet xmlns:dty="http://datypic.com">...
<xsl:variable name="in-xml-1" as="item()*">
<in-xml>
   <a>
      <b>b</b>
      <c>c</c>
   </a>
</in-xml>
</xsl:variable>
<xsl:variable name="in-xml-2" as="item()*">
<in-xml xmlns:dty="http://datypic.com">
   <a>
      <dty:b>b</dty:b>
      <c>c</c>
   </a>
</in-xml>
</xsl:variable>
XPath 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, http://www.datypic.com
Datypic XSLT Services

Recommended Reading:

XQuery