subject: xpath select distinct over 2 elements

Discussion in 'XML' started by will, Aug 15, 2007.

  1. will

    will Guest

    I have the following xsl which in effect does a distinct select on the
    id element
    (ie returns list with no repeating items) on subsequent xml..

    xml..

    <start>
    <test>
    <id>32354</id>
    <rate>2</rate>
    </test>
    <test>
    <id>32354</id>
    <rate>2</rate>
    </test>
    <test>
    <id>32354</id>
    <rate>4</rate>
    </test>
    <test>
    <id>32356</id>
    <rate>4</rate>
    </test>
    <test>
    <id>32357</id>
    <rate>2</rate>
    </test>
    <test>
    <id>32357</id>
    <rate>2</rate>
    </test>
    <test>
    <id>32358</id>
    <rate>5</rate>
    </test>
    </start>


    and xsl..

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
    Transform">
    <xsl:eek:utput method="xml" indent="yes"/>
    <xsl:template match="/">
    <xsl:for-each select="//test/id[not(node() =
    preceding::node())]">
    <xsl:sort select="." order="ascending"/>
    <xsl:element name="tk_id">
    <xsl:value-of select="concat(node(), ', ', ../rate)"/>
    </xsl:element>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>


    This returns a list of unique id

    <?xml version="1.0" encoding="UTF-16"?>
    <tk_id>32354, 2</tk_id>
    <tk_id>32356, 4</tk_id>
    <tk_id>32357, 2</tk_id>
    <tk_id>32358, 5</tk_id>





    This currently seems to be working.. however now i need a distinct
    select over 2 elements.
    ie selecting unique combinations of id and rate..

    The result i would like to get from the above xml would be something
    like..

    <?xml version="1.0" encoding="UTF-16"?>
    <tk_id>32354, 2</tk_id>
    <tk_id>32354, 4</tk_id>
    <tk_id>32356, 4</tk_id>
    <tk_id>32357, 2</tk_id>
    <tk_id>32358, 5</tk_id>

    as this is the list of unique id and rate combinations..

    Any help appreciated..

    Thanks
     
    will, Aug 15, 2007
    #1
    1. Advertising

  2. will

    Pavel Lepin Guest

    will <> wrote in
    <>:
    > <start>
    > <test>
    > <id>32354</id>
    > <rate>2</rate>
    > </test>
    > <test>
    > <id>32354</id>
    > <rate>2</rate>
    > </test>
    > <test>
    > <id>32354</id>
    > <rate>4</rate>
    > </test>
    > <test>
    > <id>32356</id>
    > <rate>4</rate>
    > </test>
    > <test>
    > <id>32357</id>
    > <rate>2</rate>
    > </test>
    > <test>
    > <id>32357</id>
    > <rate>2</rate>
    > </test>
    > <test>
    > <id>32358</id>
    > <rate>5</rate>
    > </test>
    > </start>


    [transformation snipped]

    > This currently seems to be working.. however now i need a
    > distinct select over 2 elements.
    > ie selecting unique combinations of id and rate..


    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="k" match="test"
    use="concat(id,', ',rate)"/>
    <xsl:key name="dist" match="test"
    use="count(.|key('k',concat(id,', ',rate))[1])"/>
    <xsl:template match="/">
    <result>
    <xsl:apply-templates select="key('dist',1)">
    <xsl:sort select="concat(id,', ',rate)"/>
    </xsl:apply-templates>
    </result>
    </xsl:template>
    <xsl:template match="test">
    <xsl:variable name="key"
    select="concat(id,', ',rate)"/>
    <xsl:copy>
    <xsl:attribute name="cnt">
    <xsl:value-of select="count(key('k',$key))"/>
    </xsl:attribute>
    <xsl:value-of select="$key"/>
    </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>

    --
    "Patience is a minor form of despair, disguised as
    virtue." -- Ambrose Bierce
     
    Pavel Lepin, Aug 15, 2007
    #2
    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. Jane
    Replies:
    0
    Views:
    540
  2. Chris
    Replies:
    1
    Views:
    972
    Joris Gillis
    Jun 19, 2006
  3. johkar
    Replies:
    2
    Views:
    1,388
    johkar
    Dec 4, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    415
    Kai-Uwe Bux
    Apr 11, 2010
  5. andrewmrichards

    XPATH equivalent of SELECT DISTINCT

    andrewmrichards, Dec 2, 2010, in forum: XML
    Replies:
    2
    Views:
    3,433
    Martin Honnen
    Dec 3, 2010
Loading...

Share This Page