I have a second node with a different set of" /> I have a second node with a different set of" /> I have a second node with a different set of"/>

Update all attributes of a node in XQUERY

67 Views Asked by At

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?

1

There are 1 best solutions below

0
Martin Honnen On

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>