• Get the Flash Player to see the slideshow.
  • Categories

  • Authors

  • Great Quotes

    Honest differences are often a healthy sign of progress. — Mahatma Gandhi

Get in touch...

To have a chat about
your CMS needs...

Call us 0207 193 2014
or
Email us on

XSL:Sort By Date, Limit Returned Results

March 2nd, 2009 by Michael Thomas

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

<xsl:variable name="x-rows" select="//Datum[@Name='FixLimit']"/>
 
<xsl:template match="/">
 
<xsl:apply-templates select=”[some repeating datum]“&gt;
<xsl:sort order=”descending” select=”substring(//somedate,7,4)> <!-- year->
<xsl:sort order=”descending” select=”substring(//somedate,4,2)> <!-- month-->
<xsl:sort order=”descending” select=”substring(//somedate,1,2)> <-- day-->
 
</xsl:apply-templates>

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

<xsl:template match=”[match repeating datum]>
   <xsl:if test="position() &lt;= $x-rows">
   ...
   </xsl:if>
</xsl:template>

to stop processing once the row limit has been reached.

Tags: , ,

Leave a Reply

You must be logged in to post a comment.