[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: Sorting and position


Subject: RE: Sorting and position
From: Kay Michael <Michael.Kay@xxxxxxx>
Date: Wed, 10 Nov 1999 18:10:21 -0000

>  I want to do is do something with the first two 
> <Product> elements, but I need to sort them by name first.
> 
> If I do this:
> 
> <xsl:template match="Products[position() >= 0 and position <= 1]">
> 	<xsl:for-each select="Product">
> 		<xsl:sort select="Name"/>
> 		<xsl:value-of select="Name"/>
> 	</xsl:for-each>
> </xsl:template>
> 
> I end up with the first two in document order, then sorted.  
> How can I come out with the first two in alpha order by Name?
>

Try:
 
 <xsl:template match="Products">
 	<xsl:for-each select="Product">
 		<xsl:sort select="Name"/>
            <xsl:if test="position()&lt;3">
 		    <xsl:value-of select="Name"/>
            </xsl:if>
 	</xsl:for-each>
 </xsl:template>

Note that position() starts at 1, your test on position()>=0 suggests some
confusion!

Actually there seems to be a terminology problem in the spec here. The spec
on sorting says that "the current node list consists of the complete list of
nodes being processed in sorted order", but the concept of "current node
list" has disappeared from XPath: position() is now defined to return the
"context position from the expression evaluation context".

Mike Kay


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



Current Thread