Use XSL to remove "outdated" nodes?

Discussion in 'XML' started by Jim Bancroft, Jul 2, 2003.

  1. Jim Bancroft

    Jim Bancroft Guest

    Hi everyone,

    Could someone reccomend a way to remove duplicate/outmoded nodes in an XML
    document?

    I didn't describe that well, so let me give an example.

    For instance, if I have an XML doc like so:

    <inventory>
    <part>
    <name="CPU"/>
    <version="4"/>
    </part>
    <part>
    <name="Hard Disk"/>
    <version="3"/>
    </part>
    <part>
    <name="CPU"/>
    <version="5"/>
    </part>
    <part>
    <name="Keyboard"/>
    <version="3"/>
    </part>
    <part>
    <name="Keyboard"/>
    <version="3"/>
    </part>
    </inventory>

    I'd like to get a final document with a single CPU element whose
    version="5", a Hard Disk element of version="3", and *one* Keyboard element
    of version "3". If you could give me a lead, I'd be grateful. Thanks.

    -Jim
     
    Jim Bancroft, Jul 2, 2003
    #1
    1. Advertising

  2. Jim Bancroft

    Marrow Guest

    Hi Jim,

    Assuming that your XML were well-formed and looked something like...

    <inventory>
    <part>
    <name>CPU</name>
    <version>4</version>
    </part>
    <part>
    <name>Hard Disk</name>
    <version>3</version>
    </part>
    <part>
    <name>CPU</name>
    <version>5</version>
    </part>
    <part>
    <name>Keyboard</name>
    <version>3</version>
    </part>
    <part>
    <name>Keyboard</name>
    <version>3</version>
    </part>
    </inventory>

    Then something like (using Muenchian technique to find distinct)...

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml"/>
    <!-- key for finding distinct part names -->
    <xsl:key name="kDistinctPartName" match="part" use="name"/>
    <xsl:template match="inventory">
    <inventory>
    <xsl:apply-templates select="part[generate-id() =
    generate-id(key('kDistinctPartName',name))]"/>
    </inventory>
    </xsl:template>

    <!-- template for handling distinct part names -->
    <xsl:template match="part">
    <!-- copy only the highest version of this distinct part name -->
    <xsl:copy-of select="key('kDistinctPartName',name)[not(version &lt;
    key('kDistinctPartName',name)/version)][1]"/>
    </xsl:template>
    </xsl:stylesheet>


    Hope this helps
    Marrow
    http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    http://www.topxml.com/Xselerator

    "Jim Bancroft" <> wrote in message
    news:g4FMa.893$...
    > Hi everyone,
    >
    > Could someone reccomend a way to remove duplicate/outmoded nodes in an XML
    > document?
    >
    > I didn't describe that well, so let me give an example.
    >
    > For instance, if I have an XML doc like so:
    >
    > <inventory>
    > <part>
    > <name="CPU"/>
    > <version="4"/>
    > </part>
    > <part>
    > <name="Hard Disk"/>
    > <version="3"/>
    > </part>
    > <part>
    > <name="CPU"/>
    > <version="5"/>
    > </part>
    > <part>
    > <name="Keyboard"/>
    > <version="3"/>
    > </part>
    > <part>
    > <name="Keyboard"/>
    > <version="3"/>
    > </part>
    > </inventory>
    >
    > I'd like to get a final document with a single CPU element whose
    > version="5", a Hard Disk element of version="3", and *one* Keyboard

    element
    > of version "3". If you could give me a lead, I'd be grateful. Thanks.
    >
    > -Jim
    >
    >
    >
     
    Marrow, Jul 2, 2003
    #2
    1. Advertising

  3. Jim Bancroft

    Marrow Guest

    Hi Jim,

    Assuming that your XML were well-formed and looked something like...

    <inventory>
    <part>
    <name>CPU</name>
    <version>4</version>
    </part>
    <part>
    <name>Hard Disk</name>
    <version>3</version>
    </part>
    <part>
    <name>CPU</name>
    <version>5</version>
    </part>
    <part>
    <name>Keyboard</name>
    <version>3</version>
    </part>
    <part>
    <name>Keyboard</name>
    <version>3</version>
    </part>
    </inventory>

    Then something like (using Muenchian technique to find distinct)...

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml"/>
    <!-- key for finding distinct part names -->
    <xsl:key name="kDistinctPartName" match="part" use="name"/>
    <xsl:template match="inventory">
    <inventory>
    <xsl:apply-templates select="part[generate-id() =
    generate-id(key('kDistinctPartName',name))]"/>
    </inventory>
    </xsl:template>

    <!-- template for handling distinct part names -->
    <xsl:template match="part">
    <!-- copy only the highest version of this distinct part name -->
    <xsl:copy-of select="key('kDistinctPartName',name)[not(version &lt;
    key('kDistinctPartName',name)/version)][1]"/>
    </xsl:template>
    </xsl:stylesheet>


    Hope this helps
    Marrow
    http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    http://www.topxml.com/Xselerator

    "Jim Bancroft" <> wrote in message
    news:g4FMa.893$...
    > Hi everyone,
    >
    > Could someone reccomend a way to remove duplicate/outmoded nodes in an XML
    > document?
    >
    > I didn't describe that well, so let me give an example.
    >
    > For instance, if I have an XML doc like so:
    >
    > <inventory>
    > <part>
    > <name="CPU"/>
    > <version="4"/>
    > </part>
    > <part>
    > <name="Hard Disk"/>
    > <version="3"/>
    > </part>
    > <part>
    > <name="CPU"/>
    > <version="5"/>
    > </part>
    > <part>
    > <name="Keyboard"/>
    > <version="3"/>
    > </part>
    > <part>
    > <name="Keyboard"/>
    > <version="3"/>
    > </part>
    > </inventory>
    >
    > I'd like to get a final document with a single CPU element whose
    > version="5", a Hard Disk element of version="3", and *one* Keyboard

    element
    > of version "3". If you could give me a lead, I'd be grateful. Thanks.
    >
    > -Jim
    >
    >
    >
     
    Marrow, Jul 2, 2003
    #3
  4. Jim Bancroft

    Jim Bancroft Guest

    "Dimitre Novatchev" <> wrote in message
    news:bdv6va$10c4fm$...
    > This is not well-formed XML.
    >


    Sorry, let's try that again-- just something I typed quickly into the news
    posting:

    <inventory>
    <part>
    <name>CPU</name>
    <version>4</version>
    </part>
    <part>
    <name>Hard Disk</name>
    <version>"3"</version>
    </part>
    <part>
    <name>CPU</name>
    <version>5</version>
    </part>
    <part>
    <name>Keyboard</name>
    <version>"3"</version>
    </part>
    <part>
    <name>Keyboard</name>
    <version>3</version>
    </part>
    </inventory>
     
    Jim Bancroft, Jul 2, 2003
    #4
  5. Jim Bancroft

    Marrow Guest

    Hi Jim,

    Assuming that your XML were well-formed and looked something like...

    <inventory>
    <part>
    <name>CPU</name>
    <version>4</version>
    </part>
    <part>
    <name>Hard Disk</name>
    <version>3</version>
    </part>
    <part>
    <name>CPU</name>
    <version>5</version>
    </part>
    <part>
    <name>Keyboard</name>
    <version>3</version>
    </part>
    <part>
    <name>Keyboard</name>
    <version>3</version>
    </part>
    </inventory>

    Then something like (using Muenchian technique to find distinct)...

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml"/>
    <!-- key for finding distinct part names -->
    <xsl:key name="kDistinctPartName" match="part" use="name"/>
    <xsl:template match="inventory">
    <inventory>
    <xsl:apply-templates select="part[generate-id() =
    generate-id(key('kDistinctPartName',name))]"/>
    </inventory>
    </xsl:template>

    <!-- template for handling distinct part names -->
    <xsl:template match="part">
    <!-- copy only the highest version of this distinct part name -->
    <xsl:copy-of select="key('kDistinctPartName',name)[not(version &lt;
    key('kDistinctPartName',name)/version)][1]"/>
    </xsl:template>
    </xsl:stylesheet>


    Hope this helps
    Marrow
    http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    http://www.topxml.com/Xselerator

    "Jim Bancroft" <> wrote in message
    news:g4FMa.893$...
    > Hi everyone,
    >
    > Could someone reccomend a way to remove duplicate/outmoded nodes in an XML
    > document?
    >
    > I didn't describe that well, so let me give an example.
    >
    > For instance, if I have an XML doc like so:
    >
    > <inventory>
    > <part>
    > <name="CPU"/>
    > <version="4"/>
    > </part>
    > <part>
    > <name="Hard Disk"/>
    > <version="3"/>
    > </part>
    > <part>
    > <name="CPU"/>
    > <version="5"/>
    > </part>
    > <part>
    > <name="Keyboard"/>
    > <version="3"/>
    > </part>
    > <part>
    > <name="Keyboard"/>
    > <version="3"/>
    > </part>
    > </inventory>
    >
    > I'd like to get a final document with a single CPU element whose
    > version="5", a Hard Disk element of version="3", and *one* Keyboard

    element
    > of version "3". If you could give me a lead, I'd be grateful. Thanks.
    >
    > -Jim
    >
    >
    >
     
    Marrow, Jul 2, 2003
    #5
  6. In my solution that uses attributes, just replace in the xsl:sort
    instructions:

    "@name" with "name"
    and
    "@version" with "version"



    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL


    "Jim Bancroft" <> wrote in message
    news:GsFMa.894$...
    > "Dimitre Novatchev" <> wrote in message
    > news:bdv6va$10c4fm$...
    > > This is not well-formed XML.
    > >

    >
    > Sorry, let's try that again-- just something I typed quickly into the news
    > posting:
    >
    > <inventory>
    > <part>
    > <name>CPU</name>
    > <version>4</version>
    > </part>
    > <part>
    > <name>Hard Disk</name>
    > <version>"3"</version>
    > </part>
    > <part>
    > <name>CPU</name>
    > <version>5</version>
    > </part>
    > <part>
    > <name>Keyboard</name>
    > <version>"3"</version>
    > </part>
    > <part>
    > <name>Keyboard</name>
    > <version>3</version>
    > </part>
    > </inventory>
    >
    >
    >
     
    Dimitre Novatchev, Jul 2, 2003
    #6
  7. Jim Bancroft

    Jim Bancroft Guest

    Dimitre, Marrow:

    Thanks for the assistance-- appreciate it.

    -Jim
     
    Jim Bancroft, Jul 2, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. asd
    Replies:
    3
    Views:
    458
    Arnaud Berger
    May 23, 2005
  2. gavnosis
    Replies:
    0
    Views:
    544
    gavnosis
    Aug 2, 2003
  3. Replies:
    1
    Views:
    3,689
    A. Bolmarcich
    May 27, 2005
  4. Replies:
    6
    Views:
    700
  5. Snaky Love
    Replies:
    1
    Views:
    837
    Snaky Love
    Feb 19, 2010
Loading...

Share This Page