home contribute faq download

FunctX XQuery Functions

functx:update-attributes

Updates the attribute value of an XML element

Google
Webxqueryfunctions.com

Description

The functx:update-attributes function takes one or more XML element nodes, along with a sequence of attribute names and a sequence of attribute values, and returns a newly constructed element with those attributes updated. The attribute names and values are positionally related, i.e. the first attribute name corresponds to the first attribute value, the second name to the second value, etc. It leaves all the original attributes and content of the element, except that if it already has an attribute with the same name of one of $attrNames, its value is updated to match the corresponding value in $attrValues. Unlike the functx:add-or-update-attributes function, if the attribute did not previous exist on the element, it is not added.

Each name must be specified as an xs:QName value. QNames can be constructed with calls to the xs:QName type constructor or the fn:QName function, as shown in the examples.

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.

The functx:update-attributes function takes one or more XML element nodes, along with an attribute name and value, and returns a newly constructed element with that attribute's value updated (and all the same content and attributes that existed on the original element.) If the attribute did not previous exist on the element, it is not added.

Arguments and Return Type

NameTypeDescription
$elements element()* the element(s) for which you wish to update the attribute
$attrNames xs:QName* the name(s) of the attribute(s) to add
$attrValues xs:anyAtomicType* the value(s) of the attribute(s) to add
return value element()?

XQuery Function Declaration

See XSLT definition.
declare namespace functx = "http://www.functx.com";
declare function functx:update-attributes
  ( $elements as element()* ,
    $attrNames as xs:QName* ,
    $attrValues as xs:anyAtomicType* )  as element()? {

   for $element in $elements
   return element { node-name($element)}
                  { for $attrName at $seq in $attrNames
                    return if ($element/@*[node-name(.) = $attrName])
                           then attribute {$attrName}
                                     {$attrValues[$seq]}
                           else (),
                    $element/@*[not(node-name(.) = $attrNames)],
                    $element/node() }
 } ;

Examples

let $in-xml :=
<in-xml xmlns:new='http://new'>
   <a att1='def'>x</a>
   <b>x</b>
   <c new:att1='def'>x</c>
</in-xml>
return
XQuery ExampleResults
functx:update-attributes(
   $in-xml/a, xs:QName('att1'), 123)
<a att1="123">x</a>
functx:update-attributes(
   $in-xml/a,
   (xs:QName('att1'),xs:QName('att2')),
   (1,2))
<a att1="1">x</a>
functx:update-attributes(
   $in-xml/b, xs:QName('att1'), 123)
<b>x</b>
functx:update-attributes(
     $in-xml/c,
     QName('http://new','att1'),
     123)
<c xmlns:new="http://new" new:att1="123">x</c>

See Also

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

History

Published OnLast UpdatedContributor(s)
2006-06-272007-08-08Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Datypic XQuery Services

Recommended Reading:

XQuery