Re: Remove duplicates from a list

Subject: Re: Remove duplicates from a list
From: Tim Taylor <ttaylor@xxxxxxxxxx>
Date: Fri, 29 Oct 1999 17:20:48 -0400
I have a similar, but more complicated problem.  Given the following XML
snippet:

    <skill title="java"/>
    <skill title="perl"/>
    <skill title="java"/>

I would like to transform to:

    java
    perl

Actually, there's a bit more to this.  The source XML is more like:

    <employee id="ttaylor">
        <skill title="java"/>
    </employee>

    <employee id="jdoe">
        <skill title="perl"/>
        <skill title="java"/>
    </employee>

And the desired tranformation (order unimportant):

    <h2>java</h2>
    <ol>
        <li>ttaylor</li>
        <li>jdoe</li>
    </ol>

    <h2>perl</h2>
    <ol>
        <li>jdoe</li>
    </ol>

However, I already figured out the XSL for generating the list of
employees with a skill, given a list of skill nodes.  I'm in search of
the XSL for the simpler problem stated above, i.e. listing the first
occurance of an element for each title attribute.  Starting with Clark
Evans' example, I have tried variations on the following idea with no
success:

    <xsl:variable name="unique-list"
        select="//employee/skill[ 
            not (. = following::skill[@title = current()/@title]) ]"/>

I'm sure I mustn't be grokking something.  I succesfully used a similar
approach to generate the list of employees.  The following will
transform to the desired output, sans removal of skill following the
first occurance:

    <xsl:for-each select="//employee/skill">
        <h2><xsl:value-of select="@title"/></h2>
        <ol>
            <xsl:for-each
select=".|following::skill[@title=current()/@title]
                <li><xsl:value-of select="parent::employee/@id"/></li>
            </xsl:for-each>
        </ol>
    </xsl:for-each>

Also, since this is really a learning excercise of my own creation, I'm
really after comprehension more than the solution.  Please explain the
solution, or provide pointers to information explaining it.  Feel free
to only address the simpler problem above.  I provided the more
complicated examples mostly as background info.

Much thanks,
Tim


"Clark C. Evans" wrote:
> 
>   <xsl:variable name="unique-list"
>     select="//state[not(.=following::state)]" />
> 
>   <xsl:for-each select="$unique-list">
>         <xsl:value-of select="." />
>   </xsl:for-each>
> 
> On Wed, 27 Oct 1999, Minita Jha wrote:
> >  <location>
> >    <state>xxxx</state>
> >  </location>
> >
> >  <location>
> >     <state>yyyy</state>
> >  </location>
> >
> >   <location>
> >     <state>xxxx</state>
> >  </location>
> >
> >  The desired output is:
> >
> >    xxxx
> >    yyyy
> >
> >  That is, duplicate values of state should not be printed.
> >  Can this be done?
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

-- 
Tim Taylor


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread