5

I'm creating a website where I need to show the top 5 records from an RSS feed, and these need to be sorted by date and time.

The date fields in the RSS feed are in the following format: "Mon, 16 Feb 2009 16:02:44 GMT"

I'm having big problems getting the records to sort correctly - I've tried lots of different code examples I've seen, but none seem to sort the records correctly. The code for my XSL sheet is shown below, and the feed in question is here.

Very grateful for anyones help!!!

Thanks,

Chris.


XSL CODE:

<xsl:stylesheet version="1.1" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:digg="http://digg.com//docs/diggrss/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<xsl:template match="/">    
<xsl:for-each select="//*[local-name()='item'][position() < 6]">    
    <p>
        <a>
        <xsl:attribute name="href">
        <xsl:value-of select="*[local-name()='link']"/></xsl:attribute>
        <xsl:attribute name="target">
        <xsl:text>top</xsl:text>
        </xsl:attribute>
        <xsl:value-of select="*[local-name()='title']"/>
        </a>
        <br/>
    <span class="smaller"><xsl:value-of select="*[local-name()='pubDate']" disable-output-escaping="yes"/></span>
    </p>
</xsl:for-each>

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
pixelkicks
  • 866
  • 2
  • 11
  • 23
  • You haven't provided an example XML(RSS) file for which you want the sorting to be done, nor have you provided the exact output you want to be produced. Please, edit your question and provide these two important things. Then many people will be able to help. This is not that challenging. – Dimitre Novatchev Feb 17 '09 at 03:53

2 Answers2

9

The following transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:variable name="vrtfMonths">
    <m name="Jan" num="01"/>
    <m name="Feb" num="02"/>
    <m name="Mar" num="03"/>
    <m name="Apr" num="04"/>
    <m name="May" num="05"/>
    <m name="Jun" num="06"/>
    <m name="Jul" num="07"/>
    <m name="Aug" num="08"/>
    <m name="Sep" num="09"/>
    <m name="Oct" num="10"/>
    <m name="Nov" num="11"/>
    <m name="Dec" num="12"/>
  </xsl:variable>

  <xsl:variable name="vMonths" select=
   "document('')/*/xsl:variable
                   [@name='vrtfMonths']/*"
   />

    <xsl:template match="channel">
      <xsl:for-each select="item">
        <xsl:sort data-type="number" order="descending" select=
        "concat(substring(pubDate,13,4),
                $vMonths[@name 
                        = 
                         substring(current()/pubDate,9,3)]/@num,

                substring(pubDate,6,2),
                translate(substring(pubDate,18,8),
                          ':',
                          ''
                          )
                )
         "/>

         <xsl:if test="not(position() > 5)">
           <xsl:copy-of select="."/>

         </xsl:if>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

when applied on the provided source XML document (too-long to list here, see the link),

produces the wanted result:

<item xmlns:media="http://search.yahoo.com/mrss">
        <title>Inflation rate expected to fall</title>
        <description>The latest figures for the rate of inflation, for the 12 months to January, are expected to show that it has fallen again.</description>
        <link>http://news.bbc.co.uk/go/rss/-/1/hi/business/7893873.stm</link>
        <guid isPermaLink="false">http://news.bbc.co.uk/1/hi/business/7893873.stm</guid>
        <pubDate>Tue, 17 Feb 2009 02:58:03 GMT</pubDate>
        <category>Business</category>
        <media:thumbnail width="66" height="49" url="http://newsimg.bbc.co.uk/media/images/45483000/jpg/_45483998_cash226.jpg" />
    </item><item xmlns:media="http://search.yahoo.com/mrss">
        <title>Farming pressures</title>
        <description>Are farmers being forced to use GM seeds?</description>
        <link>http://news.bbc.co.uk/go/rss/-/1/hi/business/7892328.stm</link>
        <guid isPermaLink="false">http://news.bbc.co.uk/1/hi/business/7892328.stm</guid>
        <pubDate>Tue, 17 Feb 2009 00:06:31 GMT</pubDate>
        <category>Business</category>
        <media:thumbnail width="66" height="49" url="http://newsimg.bbc.co.uk/media/images/45481000/jpg/_45481936_005666785-1.jpg" />
    </item><item xmlns:media="http://search.yahoo.com/mrss">
        <title>Engine room?</title>
        <description>Western Australia's economy may be hitting reverse. </description>
        <link>http://news.bbc.co.uk/go/rss/-/1/hi/business/7840370.stm</link>
        <guid isPermaLink="false">http://news.bbc.co.uk/1/hi/business/7840370.stm</guid>
        <pubDate>Tue, 17 Feb 2009 00:05:25 GMT</pubDate>
        <category>Business</category>
    </item><item xmlns:media="http://search.yahoo.com/mrss">
        <title>Coining it in? </title>
        <description>Investors turning to old and rare coins in the downturn </description>
        <link>http://news.bbc.co.uk/go/rss/-/1/hi/business/7878606.stm</link>
        <guid isPermaLink="false">http://news.bbc.co.uk/1/hi/business/7878606.stm</guid>
        <pubDate>Tue, 17 Feb 2009 00:04:29 GMT</pubDate>
        <category>Business</category>
        <media:thumbnail width="66" height="49" url="http://newsimg.bbc.co.uk/media/images/45459000/jpg/_45459789_dsc_4982.jpg" />
    </item><item xmlns:media="http://search.yahoo.com/mrss">
        <title>Home sales may rise say surveyors</title>
        <description>Property sales may pick in the next few months, according to the Royal Institution of Chartered Surveyors (Rics).</description>
        <link>http://news.bbc.co.uk/go/rss/-/1/hi/business/7893193.stm</link>
        <guid isPermaLink="false">http://news.bbc.co.uk/1/hi/business/7893193.stm</guid>
        <pubDate>Tue, 17 Feb 2009 00:02:19 GMT</pubDate>
        <category>Business</category>
        <media:thumbnail width="66" height="49" url="http://newsimg.bbc.co.uk/media/images/45483000/jpg/_45483075_006771638-1.jpg" />
    </item>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Brilliant Dimitre! Thats just what I've been looking for! Thank you so much. – pixelkicks Feb 17 '09 at 08:00
  • Can this same thing be done, without the use of document('')? Or is that what allows xpath access to the elements within the .xsl file? I am asking because DotNetNuke doesn't parse document('') within an XSL file by default. – Steve T May 20 '09 at 17:33
  • @steve-t One could use the xxx:node-set() extension function, or alternatively, put the contents of the $vrtfMonths variable in a separate xml file, then referenc it via document(thatFilesURL) – Dimitre Novatchev May 20 '09 at 19:56
  • @Dimitre Thank you so much, that worked perfectly. select="ms:node-set($vrtfMonths)/m instead of select="document('') I found how to use MSXML with node-set() here: http://social.msdn.microsoft.com/forums/en-US/xmlandnetfx/thread/9f7f8e31-4320-40e5-92db-7309d26fdf46/ – Steve T May 20 '09 at 20:41
  • @Dimitre Thank you. What about when the date is without weekday? for example 17 Feb 2009 00:06:31 +0100. Do you have such a code sample as well? – snahl Apr 20 '14 at 02:42
  • @snahl, In this case and in any other variations, one needs to adjust the arguments of the calls to the `substring()` function. It would be good if you ask a new question and many people will be able to provide an answer. – Dimitre Novatchev Apr 20 '14 at 03:34
  • @DimitreNovatchev - I'm trying to figure out how to integrate your code above into a different XSL file. Could you please take a look at http://stackoverflow.com/q/32902445/1745001? – Ed Morton Oct 02 '15 at 07:39
0

An other way to get around the document restriction is to use the following:

<xsl:template name="getMonth">
    <xsl:param name="name" />
    <xsl:choose>
        <xsl:when test="$name = 'Jan'">01</xsl:when>
        <xsl:when test="$name = 'Feb'">02</xsl:when>
        <xsl:when test="$name = 'Mar'">03</xsl:when>
        <xsl:when test="$name = 'Apr'">04</xsl:when>
        <xsl:when test="$name = 'May'">05</xsl:when>
        <xsl:when test="$name = 'Jun'">06</xsl:when>
        <xsl:when test="$name = 'Jul'">07</xsl:when>
        <xsl:when test="$name = 'Aug'">08</xsl:when>
        <xsl:when test="$name = 'Sep'">09</xsl:when>
        <xsl:when test="$name = 'Oct'">10</xsl:when>
        <xsl:when test="$name = 'Nov'">11</xsl:when>
        <xsl:when test="$name = 'Dec'">12</xsl:when>
        <xsl:otherwise>99</xsl:otherwise>
    </xsl:choose>
</xsl:template>

Called using

   <xsl:call-template name="getMonth">
       <xsl:with-param name="name" select="substring(./pubDate,9,3)" />
   </xsl:call-template>

It's not pretty, but at least you don't have to worry about security

Luk
  • 5,371
  • 4
  • 40
  • 55