-2

I have the following XML snippet:

<topic>
  <data attribute1="1">some data 1<data>
  <data attribute1="1">some data x<data>
  <data2 type="sometype">some data 2</data2>
</topic>
<topic>
  <data attribute1="1">some data 1<data>
  <data attribute2="2">some data 2<data>
</topic>

I'd like to generate a super XML node from these topicssuch that the final output is a single topic with elements grouped by attribute, element names and values:

<topic>
  <data attribute1="1">some data 1<data>
  <data attribute1="1">some data x<data>
  <data2 type="sometype">some data 2</data2>
  <data attribute2="2">some data 2<data>
</topic>

What's the best approach to achieve this? Could I use XQuery? I've been playing around with http://basex.org/ but not had much luck.

Imran Azad
  • 1,008
  • 2
  • 12
  • 30

1 Answers1

1

After modifying your input XML to make it a valid XML file with a root element and closing tags like this

<root>
    <topic>
        <data attribute1="1">some data 1</data>
        <data attribute1="1">some data x</data>
        <data2 type="sometype">some data 2</data2>
    </topic>
    <topic>
        <data attribute1="1">some data 1</data>
        <data attribute2="2">some data 2</data>
    </topic>
</root>

you can merge all sub-nodes of the <topic> elements in one <topic> element with this XQuery:

let $file := doc("a.xml")/root 
return
  <topic>{for $item in $file/topic/* return $item}</topic>

Its output is

<topic>
    <data attribute1="1">some data 1</data>
    <data attribute1="1">some data x</data>
    <data2 type="sometype">some data 2</data2>
    <data attribute1="1">some data 1</data>
    <data attribute2="2">some data 2</data>
</topic>

which differs from your expected output. But because you didn't specify any rules for the output, this is as good as it gets.

zx485
  • 28,498
  • 28
  • 50
  • 59