Skip to Content
0

How to delete duplicate record from input file?

Feb 10, 2017 at 04:28 PM

106

avatar image

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?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Evgeniy Kolmakov Feb 14, 2017 at 09:29 PM
0

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thanks alot Evgeniy !!!

0
Evgeniy Kolmakov Feb 10, 2017 at 04:43 PM
0

Hi Aditya!

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

Regards, Evgeniy.

Share
10 |10000 characters needed characters left characters exceeded
Sriprasad Shivaram Bhat Feb 10, 2017 at 05:47 PM
0

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

Show 1 Share
10 |10000 characters needed characters left 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>

0