How to fix this elimination transformation in XSLT?

djbeatz

New Member
I have this input:\[code\]<root> <sector> <nodeA id="a"> <section id="i"> <item1 id="1" method="delete"/> <item1 id="1" method="create"> <somechild>a</somechild> </item1> <item1 id="1" method="change"> <somechild>a</somechild> </item1> </section> <section id="i"> <item1 id="1" method="change"> <somechild>a</somechild> </item1> </section> <section id="i"> <item1 id="1" method="delete"/> <item1 id="1" method="create"> <somechild>a</somechild> </item1> </section> </nodeA> </sector> </root>\[/code\]My XSL is:\[code\]<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="*[not(.//*[@id!=''])][@method='delete']"> <xsl:if test="not(following::*[not(.//*[@id!=''])][@id=current()/@id][../@id = current()/../@id][generate-id(../..) = generate-id(current()/../..)])"/> </xsl:template> <xsl:template match="*[not(.//*[@id!=''])][@method!='delete']"> <xsl:if test="not(following::*[not(.//*[@id!=''])][@method='delete'][@id=current()/@id][../@id = current()/../@id][generate-id(../..) = generate-id(current()/../..)])"/> </xsl:template> <xsl:template match="@*|node()" name="identity"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template></xsl:stylesheet>\[/code\]My output:\[code\]<root> <sector> <nodeA id="a"> <section id="i"> </section> <section id="i"> </section> <section id="i"> </section> </nodeA> </sector></root>\[/code\]Expected Output:\[code\]<root> <sector> <nodeA id="a"> <section id="i"> <item1 id="1" method="delete"/> <!-- leave this node --> </section> <section id="i"> </section> <section id="i"> <item1 id="1" method="create"> <!-- leave this node --> <somechild>a</somechild> </item1> </section> </nodeA> </sector> </root>\[/code\]The idea is that I want to remove a combination of element node with
  • one method create followed by one or more modify followed by one delete method and leave the rest untouched.
  • It has to be the same element name for example \[code\]<item1>\[/code\] and \[code\]@id\[/code\] and under the same parent for example \[code\]<section id=1>\[/code\].
Could anyone help me with the transformation ?Thanks.John
 
Back
Top