Thursday, April 30, 2009

DNN News v4.0.01 Custom XSL - pubDate Sorting

by Phil 'iwonder' Guerra (Happy Rock, MO) -

First, in order to provide aggregation features to the module, all feeds used in the News module must be converted to an RSSv2.0 type of feed, which I'll call a DNN_RSSv2.0 format. The intention and goal was to allow different formats of feed sources to be aggregated, and rendering such a diverse collection called for some type of standard format, and RSSv2.0 was chosen.

So, how do you render a feed sorted by pubDate? Well, here' s where some of that sanitation and validation of source news feeds pays off. The News module takes an incoming news feed source, validates it, and transforms it into a new RSS feed, and one of the elements used in this process is a special custom namespace element called, pubDateParsed. This element is created during the transformation process, and turns out to be handy for providing a simple way to sort the item elements by it. You can see the feed that the module creates by looking at the database table for the News module, and looking at the data in the Cache attribute. I suggest copying an example, and pasting it into a text file for review, but here's what an example item element contains:

<item>
<description>Polish prosecutors are raising the death toll from a fire that ripped through a homeless shelter in mid-April to 23 after DNA ...<p>
<a href="http://feedads.g.doubleclick.net/~at/RWMBZpjtAANqdf4thEFQrwJI_pI/0/da"><img src="http://feedads.g.doubleclick.net/~at/RWMBZpjtAANqdf4thEFQrwJI_pI/0/di" border="0" ismap="true">
</img>
</a>
<br/>
<a href="http://feedads.g.doubleclick.net/~at/RWMBZpjtAANqdf4thEFQrwJI_pI/1/da"><img src="http://feedads.g.doubleclick.net/~at/RWMBZpjtAANqdf4thEFQrwJI_pI/1/di" border="0" ismap="true">
</img>
</a>
</p>
<img src="http://feeds2.feedburner.com/~r/usatoday-NewsTopStories/~4/O6aWvd6_g_Q" height="1" width="1"/>
</description>
<link>http://rssfeeds.usatoday.com/~r/usatoday-NewsTopStories/~3/O6aWvd6_g_Q/2009-04-30-poland_N.htm</link>
<pubDate>Thu, 30 Apr 2009 15:16:07 GMT</pubDate>
<pubDateParsed xmlns="http://www.dotnetnuke.com/modules/news">2009-04-30T15:16:07</pubDateParsed&gt;
<title>DNA results in; Poland raises fire death toll to 23</title>
</item>

The pubDateParsed element uses the pubDate information to convert it to a sortable format date, although there is no real support for its use yet, we can create a custom XSL to 'activate' it. All we have to do is add the sort statements to the default.xsl and save it as a custom xsl.

Here's how it works. We use the pubDateParsed element and define a custom namespace to declare it. Then, add the sort function call, feed it the sort key, and sort order. Resulting in the XSL that follows...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dnn="http://www.dotnetnuke.com/modules/news">
<!-- DNNv4.0.1 pubDateSortDSC -->
<!-- by Phil 'iwonder' Guerra -->
<!-- 2008-11-23 -->
<!-- Use with attribution please -->
<xsl:output method="html" indent="yes"/>
<xsl:param name="ItemsToShow"/>
<xsl:param name="ShowItemDetails"/>
<xsl:param name="ShowItemDate"/>
<xsl:param name="Locale"/>
<xsl:template match="rss">
<xsl:for-each select="channel/item[position()&lt;=$ItemsToShow or $ItemsToShow&lt;1]">
<xsl:sort
select="dnn:pubDateParsed"
data-type="text"
order="descending"/>
<p class="DNN_News_ItemLink">
<a href="{link}" target="_main"> <xsl:value-of select="title"/>
</a>
</p>
<xsl:if test="$ShowItemDate='true'">
<p class="DNN_News_ItemDate">
<xsl:value-of select="pubDate"/>
</p>
</xsl:if>
<xsl:if test="$ShowItemDetails='true'">
<p class="DNN_News_ItemDetails">
<xsl:value-of select="description" disable-output-escaping="yes"/>
</p>
</xsl:if>
</xsl:for-each>
</xsl:template></xsl:stylesheet>

I just create 2 custom XSL files, one to sort ascending and the other sorts descending. Then, I choose which one I want to use in the module instance setup. It's pretty simple once you understand what the module is emitting and giving you.

Hopefully, the next version will add support in the setup to allow passing a sort order, and the option to sort. Until then, you'll need to create two versions, one that sorts ascending and another to sort descending. Of course, you could add support to the News module yourself, which I did, but I would not advise it, as custom work done once to a DNN module needs to be done over and over as new versions are released. Sometimes the effort is worth it, others may not be. This is one of those times where it's not really a good idea, in my opinion, I am comfortable with having the two versions handy, all I have to do to change the sort order, is just specify the desired custom XSL in the module setup.