Creating a set in xsl

Discussion in 'XML' started by David Schwartz, Mar 11, 2008.

  1. I want to create the equivalent of a Smalltalk set collection, i.e., a
    collection of values that are unique wrt each other -- no duplicates.

    The structure is as follows:

    <root>
    <record>
    <collection>
    <item>
    <item>
    <item>
    </collection>
    </record>
    <record>
    <collection>
    <item>
    <item>
    <item>
    </collection>
    </record>
    </root>

    I need a sorted, single listing of the all items without dupes.

    Any help would be appreciated.

    TIA,
    David
     
    David Schwartz, Mar 11, 2008
    #1
    1. Advertising

  2. David Schwartz wrote:
    > I want to create the equivalent of a Smalltalk set collection, i.e., a
    > collection of values that are unique wrt each other -- no duplicates.
    >
    > The structure is as follows:
    >
    > <root>
    > <record>
    > <collection>
    > <item>
    > <item>
    > <item>
    > </collection>
    > </record>
    > <record>
    > <collection>
    > <item>
    > <item>
    > <item>
    > </collection>
    > </record>
    > </root>
    >
    > I need a sorted, single listing of the all items without dupes.


    Use Muenchian grouping with XSLT 1.0 e.g.
    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:key name="item"
    match="item"
    use="."/>

    <xsl:template match="root">
    <xsl:copy>
    <xsl:apply-templates
    select="record/collection/item[generate-id() =
    generate-id(key('item', .)[1])]"
    <xsl:sort select="."/>
    </xsl:copy>
    </xsl:template>

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

    </xsl:stylesheet>

    With XSLT 2.0 you can use xsl:for-each-group.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 11, 2008
    #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. Kevin Flood
    Replies:
    0
    Views:
    1,040
    Kevin Flood
    Sep 8, 2004
  2. Kevin Flood
    Replies:
    1
    Views:
    2,792
    Kevin Flood
    Sep 13, 2004
  3. Klaus Friese
    Replies:
    0
    Views:
    488
    Klaus Friese
    Nov 22, 2004
  4. Ray Tayek
    Replies:
    3
    Views:
    530
    Ray Tayek
    Nov 30, 2003
  5. Replies:
    1
    Views:
    3,653
    A. Bolmarcich
    May 27, 2005
Loading...

Share This Page