1

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..

John
  • 2,820
  • 3
  • 30
  • 50
  • What have you tried so far? Also, what do you mean by "simultaneous"? Looks to me like you want to group together all `` elements with following ``until the next `` element appears. Is this the correct interpretation? – dirkk Dec 09 '14 at 12:12
  • @dirkk - yes correct interpretation – John Dec 09 '14 at 12:13

1 Answers1

2

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>
Ewout Graswinckel
  • 1,263
  • 6
  • 9