Re: [xsl] Selecting "distinct" elements

Subject: Re: [xsl] Selecting "distinct" elements
From: Mukul Gandhi <mukulgw3@xxxxxxxxx>
Date: Sat, 27 Sep 2003 00:41:35 -0700 (PDT)
Hi Marcus,
 It seems you need to use xsl:key for grouping. The
Muenchian grouping solution using keys for this
problem is --

<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>

<xsl:key name="x" match="element" use="@value"/>

<xsl:template match="root">
   <root>
    <xsl:for-each select="element">
      <xsl:if test="generate-id(.) =
generate-id(key('x', @value)[1])">
	<anotherElt value="{@value}"/>
      </xsl:if>	
    </xsl:for-each>
   </root>
</xsl:template>

</xsl:stylesheet>

Hope I understood the problem correctly..

Regards,
Mukul


--- Marcus Andersson <marcan@xxxxxxx> wrote:
> Hi
> I want to select one of each element with a certain
> attribute value and
> loop over these elements in a for-each (or possibly
> apply-templates). Is
> this possible with a single select statement or do I
> have to make a more
> cumbersome solution? If possible, how would a
> statement solving the
> problem look like?
> 
> Instance document:
> <root>
>  <element value="a"/>
>  <element value="b"/>
>  <element value="a"/>
>  <element value="c"/>
>  <element value="b"/>
>  <element value="d"/>
>  <element value="b"/>
> </root>
> 
> XSLT Template:
> <xsl:template match="root">
>  <xsl:for-each select="[insert your favourite
> statement here]">
>   <anotherElt value="{@value}"/>
>  </xsl:for-each>
> </xsl:template>
> 
> Resulting document:
> <root>
>  <anotherElt value="a"/>
>  <anotherElt value="b"/>
>  <anotherElt value="c"/>
>  <anotherElt value="d"/> 
> </root>
> 
> Thanks,
> 
> /Marcus
> 
> 
>  XSL-List info and archive: 
> http://www.mulberrytech.com/xsl/xsl-list
> 


__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

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


Current Thread