Input file:
<root>
<sector>
<nodeA id="a">
<section id="i">
<item1 id="1" method="create">
<somechild>a</somechild>
</item1>
<item1 id="1" method="delete" />
</section>
</nodeA>
<nodeA id="b">
<cell id="ii">
<item2 id="2" method="create">
<otherchild>a</otherchild>
</item2>
</cell>
<cell id="ii">
<item2 id="2" method="delete" />
</cell>
</nodeA>
<nodeB id="i">
<cell id="ii">
<item3 id="1" method="create">
<child>b</child>
</item3>
</cell>
<cell id="ii">
<item3 id="1" method="delete" />
<item3 id="1" method="create">
<otherchild>a</otherchild>
</item3>
</cell>
</nodeB>
</sector>
</root>
Expected Output:
<root>
<sector>
<nodeA id="a">
<section id="i">
</section>
</nodeA>
<nodeA id="b">
<cell id="ii">
</cell>
<cell id="ii">
</cell>
</nodeA>
<nodeB id="i">
<cell id="ii">
<item3 id="1" method="create"> <!-- this is not eliminated as it violates the rule by having create followed by delete and followed by create again -->
<child>b</child>
</item3>
</cell>
<cell id="ii">
<item3 id="1" method="delete" />
<item3 id="1" method="create">
<otherchild>a</otherchild>
</item3>
</cell>
</nodeB>
</sector>
</root>
I need to eliminate node in xml using this rule:
- one node with
method="create"followed with one node withmethod="delete"will be eliminated - the scenario can happen in one parent or spread in two parent as long as it has the same element name and id
<cell id="ii">
How can I do this transformation using XSLT 1.0 or 2.0?
Thank you.
Here is one approach. You define a key to group your items you are looking to remove. I think you are grouping by the @id attribute of the element, together with the @id attribute of the two parent nodes
Next, you could have a template to match your @method=’create’ items where there are two elements in the key, and the other item is a @method=’delete’
You would also need a template to match the other @method=’delete’ in a similar manner.
Here is the full XSLT
When applied to your sample XML, the following is output