0

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?

haael
  • 972
  • 2
  • 10
  • 22
  • While asking an XQuery question you need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example): (1) Input XML. (2) Your logic, and XQuery that tries to implement it. (3) Desired output based on the #1 above. (4) XQuery processor and its conformance with the XQuery standards: 1.0, 3.0, 3.1, or 4.0. – Yitzhak Khabinsky Dec 19 '22 at 21:04

1 Answers1

1

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>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110