Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging XML nodes

Tags:

xslt

I have a source xml in the following format:

 <xml>
   <node>
         <va-name>
               <source area="RSC1985s5c1"/>
         </va-name>
         <body>
              <div >
                  <heading>heading 1</heading>
               </div>
         </body>
   </node>
   <node>
         <va-name>
               <source area="RSC1985s5c2"/>
         </va-name>
         <body>
              <div >
                  <heading>heading 2</heading>
               </div>
         </body>
   </node>

   <node>
         <va-name>
               <source area="RSC1985s5c1"/>
         </va-name>
         <body>
              <div >
                  <heading>heading 3</heading>
               </div>
         </body>
   </node>

<xml>

What i want is to merge the nodes if xml/node/va-name/source[@area] values are equal among differnent xml/node. I have to copy only the xml/node/body and merge them into first node.

Output should be:

 <xml>
   <node>
         <va-name>
               <source area="RSC1985s5c1"/>
         </va-name>
         <body>
              <div >
                  <heading> heading 1 </heading>
               </div>
               <div >
                  <heading> heading 3 </heading>
               </div>
         </body>
   </node>
    <node>
         <va-name>
               <source area="RSC1985s5c2"/>
         </va-name>
         <body>
              <div >
                  <heading>heading 2</heading>
               </div>
         </body>
   </node>
 </xml>

In above output, the node 1 and node 3 has a same xml/node/va-name/source[@area] that's why in the output i have combined both and node 2 will be output as it is. If i have more nodes and if any one of them matched (xml/node/va-name/source[@area]) wih node 2 then we will combine it to node 2 and so on...

Appreciate if i can get any help...

like image 271
atif Avatar asked Feb 03 '26 06:02

atif


1 Answers

A very easy grouping solution in XSLT 2.0:

<xsl:template match="xml">
<xml>
  <xsl:for-each-group select="node" group-by="va-name/source/@area">
  <node>
    <xsl:copy-of select="va-name"/>
    <body>
      <xsl:copy-of select="current-group()/body/*"/>
    </body>
  </node>
  </xsl:for-each-group>
</xml>
</xsl:template>
like image 138
Michael Kay Avatar answered Feb 05 '26 07:02

Michael Kay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!