XSL:Sort By Date, Limit Returned Results

2 March 2009
by Billy Czajkowska

In an accompanying article, a simple method to sort by date was introduced.
Normally it is desirable to limit the number of results shown.
There are various complex ways of doing this posted on other sites, the fundamental problem being that <xsl:variable> works as a constant in practice, i.e set only once.
Trying to implement a counter as per scripting languages becomes difficult – although not impossible – so rather than fight against the restrictions XSLT gives, better to use the inbuilt position() function, which will tell you how far it has progressed along the current record set.
Provided all the select criteria have been applied to the record set FIRST, the position() function will effectively map to counter.
To complete the process, set a global variable (probably based on a Datum) to fix the displayed items


Now in the add a ‘count’ test for each time the executes:


to stop processing once the row limit has been reached.

Our Latest News