How to merge two xml files using XSLT by matching attribute?

First input file:\[code\]<root> <node id="N1"> <fruit id="1" action="aaa"> <orange id="x"> <attribute> <color>Orange</color> <year>2000</year> </attribute> </orange> </fruit> </node></root>\[/code\]Second input file:\[code\]<root> <node id="N1"> <fruit id="1"> <orange id="y"> <attribute> <color>Orange</color> <year>2000</year> </attribute> </orange> <orange id="z"> <attribute> <color>Orange</color> <year>2000</year> </attribute> </orange> </fruit> <fruit id="2" action="bbb"> <orange id="x"> <attribute> <color>Pink</color> <year>2000</year> </attribute> </orange> </fruit> </node></root>\[/code\]Ouput after merged:\[code\]<root> <node id="N1"> <fruit id="1" action="aaa"> <orange id="x"> <attribute> <color>Orange</color> <year>2000</year> </attribute> </orange> <orange id="y"> <attribute> <color>Orange</color> <year>2000</year> </attribute> </orange> <orange id="z"> <attribute> <color>Orange</color> <year>2000</year> </attribute> </orange> </fruit> <fruit id="2" action="bbb"> <orange id="x"> <attribute> <color>Pink</color> <year>2000</year> </attribute> </orange> </fruit> </node></root>\[/code\]Is that possible to be done just by using XSLT only? Please advise me on the generalized solution for the problem. The algorithm of merge:
  • first find the node fruit id=1 then we find on the second file thesame fruit id=1, every children of fruit id=1 we'll be put inside thenode of fruit id=1 in first file.
  • if the fruit id is different we simply add it as a new node (not
    under fruit id=1)
  • We can assume that the merging will be based on the 'fruit' node, so we can use the variable 'fruit' in the xslt file to make it easier.
  • And it has to be under the same node id (N1 in the example above).
Thank you.John
 
Back
Top