merge two or more node in xml using xslt

There are 3 scenarios in this problem:First possibility:Input:\[code\]<root> <node id="N1"> <fruit id="1" action="aaa"> <orange id="x" action="create"> <attribute> <color>Orange</color> <year>2012</year> </attribute> </orange> <orange id="x" action="change"> <attribute> <color>Red</color> </attribute> </orange> <orange id="x" action="change"> <attribute> <color>Blue</color> <condition>good</condition> </attribute> </orange> </fruit> </node></root>\[/code\]Expected output:\[code\]<root> <node id="N1"> <fruit id="1" action="aaa"> <orange id="x" action="create"> <attribute> <color>Blue</color> <year>2012</year> <condition>good</condition> </attribute> </orange> </fruit> </node></root>\[/code\]Second Possibility: Input:\[code\]<root> <node id="N1"> <car id="1"> <bmw id="i" action="change"> <attribute> <color>Blue</color> <owner>a</owner> </attribute> </bmw> <bmw id="i" action="change"> <attribute> <color>Yellow</color> <status>avaailable</status> </attribute> </bmw> </car> </node></root>\[/code\]Expected Output:\[code\]<root> <node id="N1"> <car id="1"> <bmw id="i" action="change"> <attribute> <color>Yellow</color> <owner>a</owner> <status>available</status> </attribute> </bmw> </car> </node></root>\[/code\]Third Scenario:\[code\]<root> <node id="N1"> <car id="1"> <bmw id="j" action="delete"> <attribute> <color>Blue</color> <year>2000</year> </attribute> </bmw> <bmw id="j" action="delete"> <attribute> <color>Pink</color> <status>available</status> </attribute> </bmw> </car> </node></root>\[/code\]Expected Output:\[code\]<root> <node id="N1"> <car id="1"> <bmw id="j" action="delete"> <attribute> <color>Pink</color> <year>2000</year> <status>available</status> </attribute> </bmw> </car> </node></root>\[/code\]Explanation on second and third scenario:
  • Two or more node with 'action=change' will be merged into one node with 'action=change'
  • Two or more node with 'action=delete' will be merged into one node with 'action=delete'
  • while merging, we update we only keep the value from the last node, keep the initial node and add any new additional node with it.
I hope the explanation is clear. Please advise me on XSLT solution for this problem. Thank you.kind regards,John
 
Back
Top