• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1138
  • Last Modified:

Problem sorting RSS feed by date using XSL

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:
http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/business/rss.xml

Very grateful for anyones help!!!

Thanks,

Chris.


<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() &lt; 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>
</xsl:template>
</xsl:stylesheet>

Open in new window

0
WebVitality
Asked:
WebVitality
  • 5
  • 4
1 Solution
 
Ray PaseurCommented:
XML is not really made for sorting - the order is kind of set by the authors.  But I "sorted" some XML once with the code shown below.  

What you might want to do is grab the RSS feed pubDate into strtotime() and produce an ISO8601 date with date('c').  Insert this into the RSS data and into an external array, like shown in the example.  Then you can iterate over the array to extract the RSS feed items in the desired order.

Best regards, ~Ray
<?php // RAY_sort_XML.php
 
// TEST DATA
$response = '<all>
<CUST>
<NAME>cheese co</NAME>
<LVL>E1</LVL>
<STATE>FL</STATE>
</CUST>
<CUST>
<NAME>ABC Co.</NAME>
<LVL>A1</LVL>
<STATE>CA</STATE>
</CUST>
<CUST>
<NAME>ACME</NAME>
<LVL>A2</LVL>
<STATE>CA</STATE>
</CUST>
</all>';
 
// CONVERT TO OBJECT
$xmlobj = simplexml_load_string($response);
 
// ITERATE OVER THE OBJECT
$point = 0;
foreach ($xmlobj->CUST as $thing)
{
// EXTRACT THE NAME FOR SORTING
	$order["$point"] = "$thing->NAME";
// INSERT A POINTER
	$thing->ORDER = "$point";
	$point++;
}
 
// SORT THE NAMES IN SENSIBLE ORDER
natcasesort ($order);
 
// ITERATE BY ORDER
foreach ($order as $key => $value)
{
	$my_obj = $xmlobj->CUST[$key];
	$my_nom = "$my_obj->NAME";
	echo "<br/>$my_nom";
}

Open in new window

0
 
WebVitalityAuthor Commented:
Hi Ray,

What is the  <xsl:sort> element for then?  I've seen various examples of sorting using this. (Just that none of them seem to work with the date format I need.

I appreciate your PHP script, however I'm purely a classic ASP developer at present, so if you have any ready-made ASP scripts, that would help :)  I'd like to see a solution using the XSL styling however, as I've come close with a few examples, but just need to nail down the date problem.
0
 
WebVitalityAuthor Commented:
PS. Also Ray, you mention that the order is set by the authors. In the RSS feed link I posted above, it's already in the correct order so if I could selec the top 5 articles, then I would be happy - however they dont come out in the same order when i parse the feed.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Ray PaseurCommented:
I may not be much help in ASP, sorry  But if you take the XSL sort order out, do the articles come out in the correct order?
0
 
WebVitalityAuthor Commented:
No, they come out in a strange order, I cant actually fathom out what order they are in! :)
0
 
Ray PaseurCommented:
It appears that BBC puts them into the feed in the strange order!
0
 
Ray PaseurCommented:
Well, like I said, I am not much help in ASP, but this PHP code segment will inject a field named "ORDER" into the object inside each of the [item] elements.  Maybe you can sort on the ORDER via XSL.

Anyway, best of luck with it.  ~Ray
<?php // RAY_sort_rss.php
 
// GET THE NEWSFEED
$obj = SimpleXML_Load_File('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/business/rss.xml');
 
// ITERATE OVER THE OBJECT
foreach ($obj->channel->item as $item_thing)
{
 
// PRODUCE AN ISO8601 DATE
   $sort_date = date('c', strtotime($item_thing->pubDate));
   
// INJECT THE DATE INTO THE OBJECT   
   $item_thing->ORDER = $sort_date;
}
 
// SHOW THE OBJECT WITH THE INJECTED DATES
var_dump($obj);

Open in new window

0
 
WebVitalityAuthor Commented:
Hi Ray,

I've now found the solution. See below:
http://stackoverflow.com/questions/554909/problem-sorting-rss-feed-by-date-using-xsl

Thanks anyway,

Chris.
0
 
Ray PaseurCommented:
Have you tested that?  It would be good to know if it works for you.  And in unrelated matters, isn't it amazing that the RSS standard for the date does not use the ISO8601 (sortable) date.  Sheesh!  What were those people thinking?!
0
 
ee_autoCommented:
Question PAQ'd, 250 points refunded, and stored in the solution database.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now