[xsl] Re: Problem with selecting UNIQUE attribute

Subject: [xsl] Re: Problem with selecting UNIQUE attribute
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sun, 17 Jun 2001 03:52:44 -0700 (PDT)
Kevin Read wrote:
> It seems that no matter what combination of select expressions I use on the
> build-dirlist I am unable to select on the unique values. :-(

> <xsl:template name="build-dirlist">
>  <xsl:param name="LVL"/>
>   <xsl:for-each select="//dir[@level=$LVL and not(preceding::parameter[@name
> = @name])]">
>    <xsl:sort select="@name" order="ascending" />
>     <option>
>      <xsl:value-of select="@name"/>
>     </option>
>   </xsl:for-each>
> </xsl:template>

Hi Kevin,

The problem is in the following two lines of your code:
>   <xsl:for-each select="//dir[@level=$LVL and not(preceding::parameter[@name
> = @name])]">

What you want is to get all "dir" elements on a level, such that their "name"
attribute is not equal to the "name" attribute of any of the preceding ***dir*** 

This can be specified as:

//dir[@level=$LVL and not(@name = preceding::dir/@name)]

After this correction the template will be working correctly. Its code will be:

<xsl:template name="build-dirlist">
 <xsl:param name="LVL"/>
  <xsl:for-each select="//dir[@level=$LVL and not(@name = preceding::dir/@name)]">
   <xsl:sort select="@name" order="ascending" />
     <xsl:value-of select="@name"/>

Dimitre Novatchev.
P.S. It is usually recommended to optimise expressions like "//dir" and also to use
the Muenchian method for grouping, which uses keys and can be much more efficient
than what you're using -- comparing every node with all preceding identically typed
nodes -- this has complexity of O(N*N), where N is the number of all nodes of this
same type in the xml source document.

Do You Yahoo!?
Spot the hottest trends in music, movies, and more.

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

Current Thread