home contribute faq download

FunctX XSLT Functions

functx:change-element-ns-deep

Changes the namespace of XML elements and its descendants

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

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

XSLT Function Declaration

See XQuery definition.
<xsl:function name="functx:change-element-ns-deep" as="node()*"
              xmlns:functx="http://www.functx.com">
  <xsl:param name="nodes" as="node()*"/>
  <xsl:param name="newns" as="xs:string"/>
  <xsl:param name="prefix" as="xs:string"/>

  <xsl:for-each select="$nodes">
    <xsl:variable name="node" select="."/>
    <xsl:choose>
      <xsl:when test="$node instance of element()">
        <xsl:element name="{concat($prefix,
                                    if ($prefix = '')
                                    then ''
                                    else ':',
                                    local-name($node))}"
                     namespace="{$newns}">
          <xsl:sequence select="($node/@*,
                functx:change-element-ns-deep($node/node(),
                                           $newns, $prefix))"/>
        </xsl:element>
      </xsl:when>
      <xsl:when test="$node instance of document-node()">
        <xsl:document>
          <xsl:sequence select="functx:change-element-ns-deep(
                $node/node(), $newns, $prefix)"/>
        </xsl:document>
      </xsl:when>
      <xsl:otherwise>
        <xsl:sequence select="$node"/>
      </xsl:otherwise>
   </xsl:choose>
  </xsl:for-each>

</xsl:function>

Examples

<xsl:variable name="in-xml" as="item()*">
<bar:a xmlns:bar="http://bar">
   <bar:b>557</bar:b>
   <bar:c>xyz</bar:c>
</bar:a>
</xsl:variable>
XPath 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 XSLT Services

Recommended Reading:

XQuery