home contribute faq download

FunctX XQuery Functions

functx:remove-attributes

Removes attributes from an XML element, based on name

Google
Webdatypic.com

Description

The functx:remove-attributes function removes attributes from a sequence of one or more elements. 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 (using the same prefix). You can also specify wildcard values "*", "*:" and ":*" as the second argument. See the description for functx:name-test for details.

The function does not remove attributes from the descendants of the elements, just the elements themselves; see functx:remove-attributes-deep for this purpose.

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.

Arguments and Return Type

NameTypeDescription
$elements element()* the element(s) from which to remove the attributes
$names xs:string* the names of the attributes to remove, or * for all attributes
return value element()*

XQuery Function Declaration

See XSLT definition.
declare namespace functx = "http://www.functx.com";
declare function functx:remove-attributes
  ( $elements as element()* ,
    $names as xs:string* )  as element() {

   for $element in $elements
   return element
     {node-name($element)}
     {$element/@*[not(functx:name-test(name(),$names))],
      $element/node() }
 } ;

Examples

let $in-xml-1 :=
<a attr1="123" attr2="456">abc</a>
let $in-xml-2 :=
<a xmlns:a="http://a"
    a:attr1="123" attr1="456">abc</a>
return
XQuery ExampleResults
functx:remove-attributes(
     $in-xml-1, ('attr1','attr2'))
<a>abc</a>
functx:remove-attributes(
     $in-xml-1, ('attr1','attr3'))
<a attr2="456">abc</a>
functx:remove-attributes($in-xml-1, '*')
<a>abc</a>
functx:remove-attributes(
     $in-xml-2, ('a:attr1'))
<a attr1="456">abc</a>

Depends On

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

See Also

functx:remove-attributes-deepRemoves attributes from an XML fragment, 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, https://www.datypic.com
Datypic XQuery Services

Recommended Reading:

XQuery