[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] How to make this XSLT works for more than two section


Subject: [xsl] How to make this XSLT works for more than two section
From: Jo Na <jkoe888@xxxxxxxxx>
Date: Wed, 13 Jun 2012 18:55:14 +0700

I have this XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="RNC/*/*/*
         [deep-equal(.,
           preceding::*[name()=current()/name()]
                       [@id = current()/@id]
                       [../../@id = current()/../../@id]
                       [1])]" >
    </xsl:template>
    </xsl:stylesheet>

this is the input:
<myroot>
    <RNC>
        <nodeA id="a">
            <section id="i">
                <item1 id="0" method="delete"/>
                <item1 id="1" method="create">
                    <otherchild>a</otherchild>
                </item1>
            </section>
            <section id="i">
                <item1 id="0" method="delete"/> <!-- second
consecutive delete we remove this -->
                <item1 id="0" method="delete"> <!-- third consecutive
delete 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="create">
                    <otherchild>a</otherchild>
                </item1>
                <item1 id="0" method="create"> <!-- second consecutive
create, we remove this -->
                    <otherchild>a</otherchild>
                </item1>
            </section>
        </nodeA>
    </RNC>
</myroot>


Output:
<myroot>
    <RNC>
        <nodeA id="a">
            <section id="i">
                <item1 id="0" method="delete"/>
                <item1 id="1" method="create">
                    <otherchild>a</otherchild>
                </item1>
            </section>
            <section id="i">
                <item1 id="0" method="delete"> <!-- third consecutive
delete 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="create">
                    <otherchild>a</otherchild>
                </item1>
            </section>
        </nodeA>
    </RNC>
</myroot>

How to fix my XSLT to make it work in more than two section ?

For Example input:
<myroot>
    <RNC>
        <nodeA id="a">
            <section id="i">
                <item1 id="0" method="delete"/>
                <item1 id="1" method="create">
                    <otherchild>a</otherchild>
                </item1>
            </section>
            <section id="i">
                <item1 id="0" method="delete"/> <!-- second
consecutive delete we remove this -->
                <item1 id="0" method="delete"> <!-- third consecutive
delete 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="create">
                    <otherchild>a</otherchild>
                </item1>
                <item1 id="0" method="create"> <!-- second consecutive
create, we remove this -->
                    <otherchild>a</otherchild>
                </item1>
            </section>
            <section id="i">
                <item1 id="0" method="create"> <!-- third consecutive
create, we remove this -->
                    <otherchild>a</otherchild>
                </item1>
                <item1 id="1" method="create">
                    <otherchild>a</otherchild>
                </item1>
            </section>
        </nodeA>
    </RNC>
</myroot>

And desired Output:
<myroot>
    <RNC>
        <nodeA id="a">
            <section id="i">
                <item1 id="0" method="delete"/>
                <item1 id="1" method="create">
                    <otherchild>a</otherchild>
                </item1>
            </section>
            <section id="i">
                <item1 id="0" method="delete"> <!-- third consecutive
delete 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="create">
                    <otherchild>a</otherchild>
                </item1>
            </section>
            <section id="i">
                <item1 id="1" method="create">
                    <otherchild>a</otherchild>
                </item1>
            </section>
        </nodeA>
    </RNC>
</myroot>

How to fix the XSLT to apply to more than two sections with the same
id as shown on the example. (right now the algorithm works fine for
only two sections with the same id)

The objective is to remove node with successive duplicate with the
same element name item1 item2 etc, same id and same method.
An XML node followed by other node with exact same element name, same
id, same method and same children will be considered duplicate.
if the two nodes being compared did not share the same 'nodeA' level
node, then they should not be considered as duplicates to be removed

Thank you.
Jo


Current Thread
Keywords