home contribute faq download

FunctX XSLT Functions

functx:change-element-ns

Changes the namespace of XML elements

Google
Webxsltfunctions.com

Description

The functx:change-element-ns function changes the namespace of one or more elements in $elements to $newns. It does not change the namespace of their descendant elements; see the functx:change-element-ns-deep function for that purpose.

Arguments and Return Type

NameTypeDescription
$elements element()* the elements to change
$newns xs:string the new namespace
$prefix xs:string the prefix to use for the new namespace
return value element()?

XSLT Function Declaration

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

   <xsl:for-each select="$elements">
     <xsl:variable name="element" select="."/>
     <xsl:element name="{concat($prefix,
                                    if ($prefix = '')
                                    then ''
                                    else ':',
                                    local-name($element))}"
                     namespace="{$newns}">
       <xsl:sequence select="$element/@*, $element/node()"/>
     </xsl:element>
   </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(
     $in-xml, 'http://foo','')
<a xmlns="http://foo">
  <bar:b xmlns:bar="http://bar">557</bar:b>
  <bar:c xmlns:bar="http://bar">xyz</bar:c>
</a>
functx:change-element-ns(
     $in-xml, 'http://foo','foo')
<foo:a xmlns:foo="http://foo">
  <bar:b xmlns:bar="http://bar">557</bar:b>
  <bar:c xmlns:bar="http://bar">xyz</bar:c>
</foo:a>

See Also

functx:change-element-ns-deepChanges the namespace of XML elements and its descendants
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