Skip to Content

How to delete duplicate record from input file?

If i am getting input like below

<Root>

<row>

<field>A</field>

</row>

<row>

<field>B</field>

</row>

<row>

<field>A</field>

</row>

</Root>

Then i want output like this

<Root>

<row>

<field>B</field>

</row>

</Root>

Is it possible through xslt or xsd validation?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Feb 14, 2017 at 09:29 PM

    Hi Aditya!

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:template match="/">
            <Root>
                <xsl:apply-templates select="//field[not(text()=preceding::text()) and not(text()=following::text())]"/>
            </Root>
        </xsl:template>
        <xsl:template match="field">
            <xsl:copy-of select="."/>
        </xsl:template>
    </xsl:stylesheet>

    Source:

    <?xml version="1.0" encoding="UTF-8"?>
    <Root>
        <row>
            <field>A</field>
        </row>
        <row>
            <field>B</field>
        </row>
        <row>
            <field>A</field>
        </row>
        <row>
            <field>C</field>
        </row>
        <row>
            <field>D</field>
        </row>
        <row>
            <field>C</field>
        </row>    
    </Root>

    Result:

    <?xml version="1.0" encoding="utf-8"?><Root><field>B</field><field>D</field></Root>

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 10, 2017 at 04:43 PM

    Hi Aditya!

    Yes, it's possible with XSLT or java mapping. I guess it can be achieved even using graphical mapping.

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 10, 2017 at 05:47 PM

    Hello Aditya,

    Below will help you to do the same.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output method="xml" indent="yes"/>
        <xsl:key name="matchCase" match="row" use="field"/>
        <xsl:template match="text()">
            <xsl:value-of select="normalize-space(.)"/>
        </xsl:template>
        <xsl:template match="*">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="text()"/>
                <xsl:apply-templates select="*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="Root">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="text()"/>
                <xsl:apply-templates select="*[local-name() != 'row']"/>
                <xsl:for-each select="row">
                    <xsl:if test="generate-id(key('matchCase',field)[1]) = generate-id(.)">
                        <xsl:apply-templates select="."/>
                    </xsl:if>
                </xsl:for-each>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>

    Regards,

    Sriprasad Shivaram Bhat

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Sriprasad,

      Thanks for code!!!

      By using your code i am getting output :

      <Root>

      <row>

      <field>A</field>

      </row>

      <row>

      <field>B</field>

      </row>

      </Root>

      But i want output like:

      <Root>

      <row>

      <field>B</field>

      </row>

      </Root>

      And is it possible to catch the rest two duplicate filed

      <Root>

      <row>

      <field>A</field>

      </row>

      <row>

      <field>A</field>

      </row>

      </Root>