I have an XML tree as follows:
<root>
<a>
<a1>A1</a1>
<a2>A2</a2>
...
...
<an>An</an>
</a>
<b>
<b1>B1</b1>
<b2>B2</b2>
...
...
<bm>Bm</bm>
</b>
<x>
<x1>X1</x1>
<x2>X2</x2>
...
...
</x>
<y>
...
...
</y>
...
...
</root>
Using Oracle XMLQuery and XQuery, I want to change this XML to:
<root>
<a>
<a1>A1</a1>
<a2>A2</a2>
...
...
<an>An</an>
<b>
<b1>B1</b1>
<b2>B2</b2>
...
...
<bm>Bm</bm>
</b>
</a>
<x> <!-- This x node and all its siblings have to stay where they are. -->
<x1>X1</x1>
<x2>X2</x2>
...
...
</x>
<y>
...
...
</y>
...
...
</root>
In short, I want to put the entire b
node and its descendents under a
by appending.
What I have tried so far is:
WITH xdata AS (
SELECT XMLTYPE('<root>
<a>
<a1>A1</a1>
<a2>A2</a2>
</a>
<b>
<b1>B1</b1>
<b2>B2</b2>
</b>
<x>
<x1>X1</x1>
<x2>X2</x2>
</x>
</root>') AS xmldata
FROM dual
)
SELECT XMLQuery ('for $a in $x1/root/a, $b in $x1/root/b
return <root>{$a}{$b}</root>'
PASSING x.xmldata AS "x1" RETURNING CONTENT) AS output
FROM xdata x;
This obviously gives me a very wrong result. Maybe what I'm trying is completely wrong. Please help.