This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Return the 10 newest news articles


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]