home contribute faq download

FunctX XSLT Functions

functx:remove-attributes-deep

Removes attributes from an XML fragment, based on name

Google
Webxsltfunctions.com

Description

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

Note: this function is intended to change the way elements and attributes 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. (There is no standard XQuery update syntax yet.)

Arguments and Return Type

NameTypeDescription
$nodes node()* the root(s) to start from
$names xs:string* the names of the attributes to remove, or * for all attributes
return value node()*

XSLT Function Declaration

See XQuery definition.
XSLT 2.0 Syntax:
<xsl:function name="functx:remove-attributes-deep" as="node()*" 
              xmlns:functx="http://www.functx.com" >
  <xsl:param name="nodes" as="node()*"/> 
  <xsl:param name="names" as="xs:string*"/> 
 
   <xsl:for-each select="$nodes">
     <xsl:choose>
       <xsl:when test=". instance of element()">
         <xsl:element name="{node-name(.)}">
           <xsl:sequence select="
              (@*[not(functx:name-test(name(),$names))],
               functx:remove-attributes-deep(node(), $names))"/>
         </xsl:element>
       </xsl:when>
       <xsl:when test=". instance of document-node()">
         <xsl:document>
           <xsl:sequence select="
                functx:remove-attributes-deep(node(), $names)"/>
         </xsl:document>
       </xsl:when>
       <xsl:otherwise>
         <xsl:sequence select="."/>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:for-each>
 
</xsl:function>

Examples

<xsl:variable name="in-xml-1" as="item()*">
<a attr1="123" attr2="456">
  <b attr1="xzy">abc</b>
</a>
</xsl:variable>
<xsl:variable name="in-xml-2" as="item()*">
<a xmlns:a="http://a" a:attr1="123" attr1="456">
  <b a:attr1="ghi" attr1="xzy">abc</b>
</a>
</xsl:variable>
XSLT ExampleResults
functx:remove-attributes-deep(
     $in-xml-1,
     ('attr1','attr2'))
<a>
  <b>abc</b>
</a>
functx:remove-attributes-deep(
     $in-xml-1,
     ('attr1','attr3'))
<a attr2="456">
  <b>abc</b>
</a>
functx:remove-attributes-deep(
     $in-xml-2,
     'a:attr1')
<a attr1="456">
  <b attr1="xzy">abc</b>
</a>

Depends On

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

See Also

functx:remove-attributesRemoves attributes from an XML element, based on name
functx:add-attributesAdds attributes to XML elements

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Need XSLT Help?
D A T Y P I C
Training | Consulting | Development

XQuery by Priscilla WalmsleyGet the book!
XQuery by Priscilla Walmsley