XML : XQuery amalgamating repeating XML structures

I am trying to figure out how I can amalgamate any of the repeating structures below where 'code' is the same. I want to sum the number_of_units and value_of_units elements where code matches. Also, keep the distinct 'books' in the result set i.e code T101 would remain in the example below. Any help would be greatly appreciated.

  <ns0:book_breakdown>          <ns0:book>             <ns0:name>Life of Pi</ns0:name>             <ns0:code>T348</ns0:code>             <ns0:unit_price>                <ns0:price currency="GBP">11.99</ns0:price>             </ns0:unit_price>        <ns0:unit_holding>                <ns0:number_of_units>14</ns0:number_of_units>                <ns0:value_of_units>159.92</ns0:value_of_units>          <ns0:unit_holding>          </ns0:book>          <ns0:book>             <ns0:name>Catcher in the rye</ns0:name>             <ns0:code>T101</ns0:code>             <ns0:unit_price>                <ns0:price currency="GBP">10.99</ns0:price>             </ns0:unit_price>               <ns0:unit_holding>                <ns0:number_of_units>14</ns0:number_of_units>                <ns0:value_of_units>159.92</ns0:value_of_units>          <ns0:unit_holding>          </ns0:book>        <ns0:book>             <ns0:name>Life of Pi</ns0:name>             <ns0:code>T348</ns0:code>             <ns0:unit_price>                <ns0:price currency="GBP">11.50</ns0:price>             </ns0:unit_price>         <ns0:unit_holding>                <ns0:number_of_units>13</ns0:number_of_units>                <ns0:value_of_units>129.92</ns0:value_of_units>          <ns0:unit_holding>          </ns0:book>       </ns0:book_breakdown>    

No comments:

Post a Comment