Altova Mailing List Archives


Re: [xsl] Return the 10 newest news articles

From: Jeni Tennison <jeni@---------------->
To:
Date: 8/24/2002 8:17:00 AM
Hi Exide,

> Currently i have an XML file that holds all the news input'd from
> certain users. Its then displayed on the 1st page viewers see.
> Currently there are only 13 articles to be listed, but as it grows i
> dont want the page load times to skyrocket. How would i convert the
> following XSL, to only show the 10 most recent news articles?

Are the articles sorted by date already, for example with the newest
articles appearing at the top or bottom of the list? If so, then it's
fairly easy to extract the first ten:

  <xsl:for-each select="article[position() &lt;= 10]">
    ...
  </xsl:for-each>

or the last ten:

  <xsl:for-each select="article[position() > last() - 10]">
    ...
  </xsl:for-each>

[Note that, used in this way, the last() function gives you the number
of articles in the list.]

If, on the other hand, the articles are in no particular order and you
have to extract them purely by date, then you need to use xsl:sort to
sort them into date order, and then test the position of each article
within the sorted list:

  <xsl:for-each select="article">
    <xsl:sort select="...date..." order="descending" />
    <xsl:if test="position() &lt;= 10">
      ...
    </xsl:if>
  </xsl:for-each>

I've put "...date..." in the select attribute of the xsl:sort there
because you'll probably need to do some kind of string manipulation on
the date to get it into a sortable form. If the date is in the ISO
8601 format of:

  YYYY-MM-DD

then you can just do:

  <xsl:sort select="date" order="descending" />

but if it's in some other format then you'll need to change the format
(into something like the ISO 8601 date format) in order to sort it, by
splitting up the string with substring() etc.

If the date format you're using has the names of months in it, then
it's harder still, and you'll need to do something like:

  string-length(
    substring-before('JanFebMarAprMayJunJulAugSepOctNovDec',
                     substring(date, ?, ?)))

to give yourself something to sort on. (Again without seeing the date
format you're using I can't tell what the expression should be
precisely.)
    
Cheers,

Jeni

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


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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.