ReverseFeed XSLT

From AwasuWiki
Revision as of 16:10, 26 April 2006 by Support (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Most channels present their feed items in reverse chronological order (i.e. the most recent one first) so it can be a little annoying when a publisher doesn't follow this convention.

Tero Ykspetaja has contributed an XSLT transformation that reverses the order of feed items within a feed. This requires version 1.1.1 of Awasu or higher and also [MSXML 4.0] to be installed.

Instructions

  • Save a copy of the XSL file below.

The recommended location is the XSLT directory in Awasu's installation directory.

  • Open the Channel Properties dialog for the channel you want to reverse (or start the New Channel Wizard), click on the Processing button and browse to the XSLT file you just saved. The feed items will be reversed the next time the channel is updated.
<?xml version="1.0" encoding="UTF-8"?>

<!--
    RSS-reverse - written by Tero Ykspetäjä

    Requires version 1.1.1 or higher of Awasu.
    
    Reverses item ordering in a feed, leaves otherwise untouched.
    This is just a code snippet. No warranties, no liabilities, no licenses.
    Use freely and of your own will, but with your own risk, too.
    Tested with RSS feed versions 0.91, 0.92, 2.0 and 1.0 (RDF) but could
    have some glitches anyway.

	Version 0.4: now works also with Atom (0.3) feeds.
-->

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:atom="http://purl.org/atom/ns#"
>

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="@*|/rss">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/rdf:RDF">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="*[name() != 'item']"/>
        <xsl:apply-templates select="*[name() = 'item']">
            <xsl:sort select="position()" data-type="number" order="descending"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="/atom:feed">
	<xsl:copy>
		<xsl:apply-templates select="@*"/>
		<xsl:apply-templates select="*[name() != 'entry']"/>
		<xsl:for-each select="*[name() = 'entry']">
			<xsl:sort select="position()" data-type="number" order="descending"/>
			<xsl:copy-of select="."/>
		</xsl:for-each>
	</xsl:copy>
</xsl:template>

<xsl:template match="*[name()='channel']">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="*[name() != 'item'] "/>
        <xsl:for-each select="item">
            <xsl:sort select="position()" data-type="number" order="descending"/>
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

<xsl:template match="*[name() = 'items']">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="rdf:Seq">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:for-each select="rdf:li">
            <xsl:sort select="position()" data-type="number" order="descending"/>
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

<!-- match all the rest -->
<xsl:template match="*|processing-instruction()">
    <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>