Merge two or more XML node using XSLT

xpone

New Member
This is the input file:\[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> <node id="N2"> <car id="1"> <bmw id="i" action="change"> <attribute> <color>Blue</color> </attribute> </bmw> <bmw id="i" action="change"> <attribute> <color>Yellow</color> </attribute> </bmw> <bmw id="i" action="change"> <attribute> <color>Pink</color> </attribute> </bmw> <bmw id="j" action="delete"> <attribute> <color>Blue</color> </attribute> </bmw> <bmw id="j" action="delete"> <attribute> <color>Yellow</color> </attribute> </bmw> </car> </node></root>\[/code\]This is the 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> <node id="N2"> <car id="1"> <bmw id="i" action="change"> <attribute> <color>Pink</color> </attribute> </bmw> <bmw id="j" action="delete"> <attribute> <color>Yellow</color> </attribute> </bmw> </car> </node></root>\[/code\]The rule:[*]if there is node with 'create' method followed by one or more 'change' method, we merge them together and use ALL children from the last 'change' into the node with 'create' method. (Note: good is added )[*]if there is one or more 'change' method we merge them and use only children from the last 'change' method.[*]if there is one or more 'delete' method we merge them and use only children from the last 'delete' method.Note that the id must be the same for node to be merged. Please advise me on XSLT solution for this problem. Thanks so much.kind regards,John
 
Back
Top