I have an XML file with following structure -
<Root>
<Doc>
<Ref>
<A></A>
<B></B>
<A></A>
<B></B>
<B></B>
<B></B>
</Ref>
</Doc>
<Doc>
<Ref>
<A></A>
<B></B>
<B></B>
<B></B>
</Ref>
</Doc>
<Doc>
<Ref>
<A></A>
<B></B>
<B></B>
<A></A>
<B></B>
<B></B>
</Ref>
</Doc>
</Root>
The node A
and all simultaneous nodesB
needs to grouped like
<Root>
<Doc>
<Ref>
<data>
<A></A>
<B></B>
</data>
<data>
<A></A>
<B></B>
<B></B>
<B></B>
</data>
</Ref>
</Doc>
<Doc>
<Ref>
<data>
<A></A>
<B></B>
<B></B>
<B></B>
</data>
</Ref>
</Doc>
<Doc>
<Ref>
<data>
<A></A>
<B></B>
<B></B>
</data>
<data>
<A></A>
<B></B>
<B></B>
</data>
</Ref>
</Doc>
</Root>
The number of nodes B
, after node A
can be anything, like 2, 3 ... or 10..
The best way to process such structures is with a 'tumbling window clause'. This will nicely organize the data in the groups that you want. I'll leave it up to you to figure out the exact xquery-update syntax required.
for $ref in //Ref
return <Ref>{
for tumbling window $w in $ref/element()
start $s when $s/self::A
return <data>{$w}</data>
}</Ref>