Re: [xsl] I need to sort and then display only the first 12 entries of a node set

Subject: Re: [xsl] I need to sort and then display only the first 12 entries of a node set
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 12 Feb 2003 09:56:33 +0000
Hi Bill,

> I did a foreach, and sorted inside that and did an if (position)
> type thing...

Glad you found the solution! :)

> I am not sure why this worked... Why the position reflects
> differntly inside the foreach... But it works so what the heck :)

The position() function always gives you the position of the context
node (the node you're looking at) within the context node list (the
nodes you're processing in whatever order you're processing them).

In a predicate as in location path like:

  /Shsmith/Newsletters/Newsletter[position() &lt; 12]

the context node is the node that you're testing (a <Newsletter>
element in this case) and the context node list contains the nodes
that you've selected with the step (all the <Newsletter> elements in
this case). In a location path step, the nodes in the context node
list are arranged in an order dependant on the axis that you use:
here, using the child axis, it's in document order (the order in which
they appear in the document).

Within an <xsl:for-each> as in:

  <xsl:for-each select="/Shsmith/Newsletters/Newsletter">
    <xsl:sort select="@dateposted" order="descending" />
    <xsl:if test="position() &lt; 12">
      ...
    </xsl:if>
  </xsl:for-each>

The context node and nodes in the context node list are again the
<Newsletter> elements, but this time the order of the context node
list is the sorted order based on the @dateposted attribute.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread