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

2
  • What have you tried so far? Also, what do you mean by "simultaneous"? Looks to me like you want to group together all <A/> elements with following <B/>until the next <A/> element appears. Is this the correct interpretation?
    – dirkk
    Dec 9, 2014 at 12:12
  • @dirkk - yes correct interpretation
    – John
    Dec 9, 2014 at 12:13

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.