home contribute faq download

FunctX XSLT Functions

functx:remove-elements

Removes child elements from an XML node, based on name

Google
Webdatypic.com

Description

The functx:remove-elements function removes child elements from the elements in $elements. The $names argument is a sequence of strings that represent child element names to remove. Prefixes can (and must) be used in the $names values for elements that are prefixed in the input documents (using the same prefix). You can also specify wildcard values "*", "*:" and ":*" in the second argument. See the description for functx:name-test for details.

The function does not remove all descendants, just children; see functx:remove-elements-deep to remove all descendants with the specified names.

Arguments and Return Type

NameTypeDescription
$elements element()* the element(s) from which you wish to remove the children
$names xs:string* the names of the child elements to remove
return value element()*

XSLT Function Declaration

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

   <xsl:for-each select="$elements">
     <xsl:element name="{node-name(.)}">
       <xsl:sequence select="(@*,
      node()[not(functx:name-test(name(),$names))])"/>
     </xsl:element>
   </xsl:for-each>

</xsl:function>

Examples

<xsl:variable name="in-xml-1" as="item()*">
<in-xml>
   <a>123</a>
   <a>456</a>
   <c>Mixed <b>content</b></c>
</in-xml>
</xsl:variable>
<xsl:variable name="in-xml-2" as="item()*">
<in-xml xmlns:x="http://x">
   <a>123</a>
   <x:a>456</x:a>
   <c>Mixed <x:a>content</x:a></c>
</in-xml>
</xsl:variable>
XPath ExampleResults
functx:remove-elements(
     $in-xml-1,
     'c')
<in-xml>
  <a>123</a>
  <a>456</a>
</in-xml>
functx:remove-elements(
     $in-xml-1,
     ('a','b'))
<in-xml>
  <c>Mixed <b>content</b>
  </c>
</in-xml>
functx:remove-elements(
     $in-xml-2,
     'x:a')
<in-xml>
  <a>123</a>
  <c>Mixed <x:a xmlns:x="http://x">content</x:a>
  </c>
</in-xml>

Depends On

functx:name-testWhether a name matches a list of names or name wildcards

See Also

functx:remove-elements-deepRemoves descendant elements from an XML node, based on name
functx:remove-elements-not-contentsRemoves descendant XML elements but keeps their content

History

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

Recommended Reading:

XQuery