简体   繁体   中英

Update all attributes of a node in XQUERY

I have a node with attributes and children.

<node attr1="value1" attr2="value2"><child1/><child2/></node>

I have a second node with a different set of attributes:

<node attr1="value1_new" attr3="value3_new"/>

I want to replace all attributes of the first node with the attributes from the second, preserving children. Missing attributes from the second node should be deleted. The desired result is:

<node attr1="value1_new" attr3="value3_new"><child1/><child2/></node>

This command will replace all contents of the node, thus removing children:

let $replacement = <node attr1="value1_new" attr3="value3_new"/>
replace node /node[1] with $replacement

How to update attributes and keep children?

With the various XQuery update extensions that BaseX supports the following works for me with BaseX 10.4

copy $n1 := <node attr1="value1" attr2="value2"><child1/><child2/></node>,
     $n2 := <node attr1="value1_new" attr3="value3_new"/>
modify ( 
 delete node $n1/@*,
 insert node $n2/@* into $n1
)
return $n1

to return the result

<node attr1="value1_new" attr3="value3_new"><child1/><child2/></node>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM