-->

how to merge two nodes having “the same father” an

2019-06-14 15:44发布

问题:

I need to merge the two nodes MANHATTAN:

because our system rule is the following:

"CREATE A + MODIFY A" is still a "CREATE A with the merged attributes"

input file:

<?xml version="1.0" encoding="UTF-8"?>
<world>
<COUNTRY id="USA" >
  <STATE id="NEW JERSEY">
     <CITY id="NEW YORK" method="modify">


        <DISTRICT id="MANHATTAN" method="create">
           <attributes>
              <population>99 </population> 
              <income> 10000</income>
           </attributes>
        </DISTRICT>

        <DISTRICT id="MANHATTAN" method="modify">
           <attributes>
              <temperature>78</temperature>
              <income>15000</income>
              <information>suburb of newyork</information>
           </attributes>
        </DISTRICT>

     </CITY>

  </STATE>

Expected output:

 <?xml version="1.0" encoding="UTF-8"?>
 <world>
 <COUNTRY id="USA" >
  <STATE id="NEW JERSEY">
     <CITY id="NEW YORK" method="modify">

        <DISTRICT id="MANHATTAN" method="create">
           <attributes>
              <population>99 </population> 
              <temperature>78</temperature>
              <income>15000</income>
              <information>suburb of newyork</information>
           </attributes>
        </DISTRICT>

     </CITY>
 </STATE>
</COUNTRY>
</world>

Please help, I have just began XSLT and doing it in Perl or Python will takes hours.

回答1:

Adapt solution from similar problem at how to merge two nodes having "the same father", the same method and the same id=0 (using XSLT)?

Read the solution to see how grouping works, and then vary the grouping keys accordingly.