How to remove this tricky XML duplicate node using XSLT?

danielfss

New Member
I would like to remove consecutive duplicate node from the same parent and which also has exact same children.Input Scenario 1:\[code\]<myroot> <nodeA id="a"> <section id="i"> <item1 id="0" method="create"> <somechild>a</somechild> </item1> <item1 id="1" method="create"> <otherchild>a</otherchild> </item1> </section> <section id="i"> <item1 id="0" method="create"> <!-- second consecutive create, we remove this --> <somechild>a</somechild> </item1> <item1 id="0" method="create"> <!-- third consecutive create, but children have different value , so we don't remove this --> <somechild>bbb</somechild> </item1> <item1 id="3" method="create"> <other>xx</other> </item1> <item1 id="0" method="change"> <otherchild>a</otherchild> </item1> </section> </nodeA> <nodeA id="b"> <section id="i"> <item1 id="0" method="create"> <somechild>a</somechild> </item1> <item1 id="1" method="create"> <otherchild>a</otherchild> </item1> </section> <section id="i"> <item1 id="0" method="create"> <!-- second consecutive create, we remove this --> <somechild>a</somechild> </item1> <item1 id="0" method="create"> <!-- third consecutive create, but children have different value , so we don't remove this --> <somechild>bbb</somechild> </item1> <item1 id="3" method="create"> <other>xx</other> </item1> <item1 id="0" method="change"> <otherchild>a</otherchild> </item1> </section> </nodeA> <nodeB id="b"> <section id="i"> <item1 id="0" method="create"> <somechild>a</somechild> </item1> <item1 id="1" method="create"> <otherchild>a</otherchild> </item1> </section> <section id="i"> <item1 id="0" method="create"> <!-- second consecutive create, we remove this --> <somechild>a</somechild> </item1> <item1 id="0" method="create"> <!-- third consecutive create, but children have different value , so we don't remove this --> <somechild>bbb</somechild> </item1> <item1 id="3" method="create"> <other>xx</other> </item1> <item1 id="0" method="change"> <otherchild>a</otherchild> </item1> </section> </nodeB></myroot>\[/code\]My result:\[code\]<myroot> <nodeA id="a"> <section id="i"> <item1 id="0" method="create"> <somechild>a</somechild> </item1> <item1 id="1" method="create"> <otherchild>a</otherchild> </item1> </section> <section id="i"> <item1 id="0" method="create"> <somechild>bbb</somechild> </item1> <item1 id="3" method="create"> <other>xx</other> </item1> <item1 id="0" method="change"> <otherchild>a</otherchild> </item1> </section> </nodeA> <nodeA id="b"> <section id="i"/> <section id="i"/> </nodeA> <nodeB id="b"> <section id="i"/> <section id="i"/> </nodeB></myroot>\[/code\]Expected Output:\[code\]<myroot> <nodeA id="a"> <section id="i"> <item1 id="0" method="create"> <somechild>a</somechild> </item1> <item1 id="1" method="create"> <otherchild>a</otherchild> </item1> </section> <section id="i"> <item1 id="0" method="create"> <somechild>bbb</somechild> </item1> <item1 id="3" method="create"> <other>xx</other> </item1> <item1 id="0" method="change"> <otherchild>a</otherchild> </item1> </section> </nodeA> <nodeA id="b"> <section id="i"> <item1 id="0" method="create"> <somechild>a</somechild> </item1> <item1 id="1" method="create"> <otherchild>a</otherchild> </item1> </section> <section id="i"> <item1 id="0" method="create"> <somechild>bbb</somechild> </item1> <item1 id="3" method="create"> <other>xx</other> </item1> <item1 id="0" method="change"> <otherchild>a</otherchild> </item1> </section> </nodeA> <nodeB id="b"> <section id="i"> <item1 id="0" method="create"> <somechild>a</somechild> </item1> <item1 id="1" method="create"> <otherchild>a</otherchild> </item1> </section> <section id="i"> <item1 id="0" method="create"> <somechild>bbb</somechild> </item1> <item1 id="3" method="create"> <other>xx</other> </item1> <item1 id="0" method="change"> <otherchild>a</otherchild> </item1> </section> </nodeB></myroot>\[/code\]In the first scenario above: only the second consecutive create has the same children and the third consecutive \[code\]create\[/code\] method has different children that's why we only remove the second one. Second input scenario (more variation):\[code\]<myroot> <nodeB id="a"> <cell id="i"> <item2 id="1" method="create"> <otherchild>a</otherchild> </item2> <item2 id="0" method="create"> <otherchild>a</otherchild> </item2> <item2 id="1" method="modify"> <otherchild>a</otherchild> </item2> </cell> <cell id="i"> <item2 id="1" method="modify"> <!-- second consecutive modify, we remove this --> <otherchild>a</otherchild> </item2> <item2 id="1" method="modify"> <!-- third consecutive modify, BUT different chldren, we do NOT remove this --> <otherchild>a</otherchild> <somechild>aa</somechild> </item2> <item2 id="1" method="delete" /> <item2 id="0" method="create"> <somechild>bbb</somechild> </item2> <item2 id="1" method="delete" /> <!-- second consecutive delete, we remove this --> <item2 id="3" method="create"> <other>xx</other> </item2> <item2 id="1" method="delete" /> <!-- third consecutive delete, we remove this --> </cell> </nodeB></myroot>\[/code\]Output:\[code\]<myroot> <nodeB id="a"> <cell id="i"> <item2 id="1" method="create"> <otherchild>a</otherchild> </item2> <item2 id="0" method="create"> <otherchild>a</otherchild> </item2> <item2 id="1" method="modify"> <otherchild>a</otherchild> </item2> </cell> <cell id="i"> <item2 id="1" method="modify"> <otherchild>a</otherchild> <somechild>aa</somechild> </item2> <item2 id="1" method="delete" /> <item2 id="0" method="create"> <somechild>bbb</somechild> </item2> </cell> </nodeB></myroot>\[/code\]Not working on this:\[code\] <myroot> <node1 id="a"> <section id="i"> <item1 id="0" method="start"> <somechild>a</somechild> </item1> <item1 id="0" method="start"> <!-- this one is successive from the previous so we eliminate --> <somechild>a</somechild> </item1> <item1 id="0" method="stop"/> <item1 id="0" method="start"> <!-- this will be treated as new starting point --> <somechild>a</somechild> </item1> </section> <section id="i"> <item1 id="0" method="start"> <!-- this one is successive from the previous so we eliminate --> <somechild>a</somechild> </item1> </section> </node1> </myroot>output: <myroot> <node1 id="a"> <section id="i"> <item1 id="0" method="start"> <somechild>a</somechild> </item1> <item1 id="0" method="start"> <!-- this one is successive from the previous so we eliminate --> <somechild>a</somechild> </item1> <item1 id="0" method="stop"/> </section> <section id="i"/> </node1> </myroot>The correct output should be: <myroot> <node1 id="a"> <section id="i"> <item1 id="0" method="start"> <somechild>a</somechild> </item1> <item1 id="0" method="stop"/> <item1 id="0" method="start"> <!-- this will be treated as new starting point --> <somechild>a</somechild> </item1> </section> <section id="i" /> </node1> </myroot>\[/code\]Can anyone helped me with this tricky removal using XSLT?THanks very much.John
 
Back
Top