[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Return the 10 newest news articles

Subject: Re: Return the 10 newest news articles
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sat, 24 Aug 2002 17:17:37 +0100
xsl news
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.