Description
The functx:add-or-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 added. 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. This is in contrast with the functx:add-attributes function, which will not update the value.
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. (There is no standard XQuery update syntax yet.)
Arguments and Return Type| Name | Type | Description |
$elements |
element()* |
the element(s) to which you wish to add 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. | | XQuery Syntax for January 2007 (1.0): |
|---|
declare namespace functx = "http://www.functx.com";
declare function functx:add-or-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 attribute {$attrName}
{$attrValues[$seq]},
$element/@*[not(node-name(.) = $attrNames)],
$element/node() }
} ; | | XQuery Syntax for July 2004 - November 2005 (CR): |
|---|
declare namespace functx = "http://www.functx.com";
declare function functx:add-or-update-attributes
( $elements as element()* ,
$attrNames as xs:QName* ,
$attrValues as xdt:anyAtomicType* ) as element()? {
for $element in $elements
return element { node-name($element)}
{ for $attrName at $seq in $attrNames
return attribute {$attrName}
{$attrValues[$seq]},
$element/@*[not(node-name(.) = $attrNames)],
$element/node() }
} ; | | XQuery Syntax for May 2003: |
|---|
declare namespace functx = "http://www.functx.com"
define function add-or-update-attributes
( $elements as element()* ,
$attrNames as xs:QName* ,
$attrValues as xdt:anyAtomicType* ) as element()? {
for $element in $elements
return element { fn:node-name($element)}
{ for $attrName at $seq in $attrNames
return attribute {$attrName}
{$attrValues[$seq]},
$element/@*[fn:not(fn:node-name(.) = $attrNames)],
$element/node() }
} |
Examplesdeclare namespace new = "http://new"; | let $in-xml := | <in-xml>
<a>x</a>
<b att1="x">x</b>
</in-xml> | return |
| XQuery Example | Results |
|---|
functx:add-or-update-attributes(
$in-xml/a, xs:QName('att1'), 1)
|
<a att1="1">x</a>
|
functx:add-or-update-attributes(
$in-xml/a,
(xs:QName('att1'),xs:QName('att2')),
(1,2))
|
<a att1="1" att2="2">x</a>
|
functx:add-or-update-attributes(
$in-xml/b,
(xs:QName('att1'),xs:QName('att2')),
(1,2))
|
<b att1="1" att2="2">x</b>
|
functx:add-or-update-attributes(
$in-xml/a,
xs:QName('new:att1'),
1)
|
<a xmlns:new="http://new" new:att1="1">x</a>
|
functx:add-or-update-attributes(
$in-xml/a,
QName('http://new','new:att1'),
1)
|
<a xmlns:new="http://new" new:att1="1">x</a>
|
See AlsoHistory |
|