Simplify xml node using if-else condition by XSLT

kishan4uall

New Member
This is the input file:\[code\]<root> <node id="N1"> <fruit id="small_fruit" action="create"> <orange id="1" action="create"> <attribute> <color>yellow</color> </attribute> </orange> </fruit> <fruit id="small_fruit" action="create"> <orange id="1" action="destroy"> <attribute> <color>green</color> </attribute> </orange> </fruit> </node> <node id="N2"> <dog id="small_dog"> <poodle id="1" action="create"> <attribute> <color>Yellow</color> </attribute> </poodle> <terrier id="2" action="create"> <attribute> <color>White</color> </attribute> </terrier> <poodle id="1" action="change"> <attribute> <color>Brown</color> </attribute> </poodle> <terrier id="2" action="destroy"> <attribute> <color>Blue</color> </attribute> </terrier> </dog> <dog id="small_dog" action="create"> <poodle id="1" action="destroy"> <attribute> <color>Black</color> </attribute> </poodle> <terrier id="2" action="change"> <attribute> <color>White</color> </attribute> </terrier> <terrier id="2" action="change"> <attribute> <color>Grey</color> </attribute> </terrier> </dog> <dog id="large_dog"> <poodle id="1" action="create"> <attribute> <color>Red</color> </attribute> </poodle> </dog> </node> </root>\[/code\]This is the expected output:\[code\]<root> <node id="N1"> <fruit id="small_fruit" action="create"> </fruit> <fruit id="small_fruit" action="create"> <orange id="1" action="destroy"> <attribute> <color>green</color> </attribute> </orange> </fruit> </node><node id="N2"> <dog id="small_dog"> </dog> <dog id="small_dog" action="create"> <poodle id="1" action="destroy"> <attribute> <color>Black</color> </attribute> </poodle> <terrier id="2" action="change"> <attribute> <color>White</color> </attribute> </terrier> <terrier id="2" action="change"> <attribute> <color>Grey</color> </attribute> </terrier> </dog> <dog id="large_dog"> <poodle id="1" action="create"> <attribute> <color>Red</color> </attribute> </poodle> </dog></node></root>\[/code\]The rule: [*]if a node with the method 'destroy' appears at the end of the same parent (a fruit or an animal) we remove all the previous nodes.[*]if NOT, we remove all the nodes including the one with 'destroy' method and leave the rest unchanged. To simplify:
  • xxx/destroy -> destroy
  • xxx/destroy/aaa/bbb -> aaa/bbb
In summary we check the node that has the same id and node name (orange-id:1 or terrier-id:2 or poodle-id:1) and it must be under the same parent ex. (fruit or dog)
 
Back
Top