Adam John Trickett
and 1 contributors

NAME

Example-4 Using a single XSLT stylesheet

Example 4

In this example we turn off RSS normalisation, and use a single XSLT stylesheet to deal with all possible RSS feeds. As before we take inputs from the command line in the form of a URI of the RSS feed and a file location for the stylesheet. The actual Perl is largely as in the previous example.

        #!/usr/bin/perl
        use strict;
        use XML::RSS::Tools;
        my $rss  = XML::RSS::Tools->new;
        $rss->set_version(0);            # This turns normalisation off
        eval {
          print $rss->rss_uri(shift)->xsl_file(shift)->transform->as_string;
          };
        print $rss->as_string('error') if ($@);

The XSLT stylesheet listed below should be able to read any of the 7 versions of RSS feed currently available and convert them consistently to a XHTML fragment. Becasue of the differences in RSS, it is not easy to convert all elements of each RSS standard into another standard, but it is practicle for the limited purposes of simple display in a web browser.

        <?xml version="1.0" encoding="UTF-8"?>
        <xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:dc="http://purl.org/dc/elements/1.1/"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                xmlns:rss="http://purl.org/rss/1.0/"
                xmlns:rss09="http://my.netscape.com/rdf/simple/0.9/"
                exclude-result-prefixes="xsl rdf dc rss rss09"
                >
        <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
        <xsl:template match="/">
                <div>
                <xsl:apply-templates select="rdf:RDF/rss:channel" />
                <xsl:apply-templates select="rdf:RDF/rss09:channel" />
                <xsl:apply-templates select="rss/channel" />
                <xsl:if test="rdf:RDF/rss:item">
                        <ul><xsl:apply-templates select="rdf:RDF/rss:item"/></ul>
                </xsl:if>
                <xsl:if test="rdf:RDF/rss09:item">
                        <ul><xsl:apply-templates select="rdf:RDF/rss09:item"/></ul>
                </xsl:if>
                </div>
        </xsl:template>
        <xsl:template match="rss:channel">
                <xsl:variable name="link" select="rss:link"/>
                <xsl:variable name="description" select="rss:description"/>
                <xsl:variable name="image" select="/rdf:RDF/rss:image/rss:url"/>
                <xsl:if test="$image">  
                        <img src="{$image}" style="float: right; margin: 2px;" />
                </xsl:if>
                <h3><a href="{$link}" title="{$description}"><xsl:value-of select="rss:title" /></a></h3>
                <hr/>
        </xsl:template>
        <xsl:template match="rss09:channel">
                <xsl:variable name="link" select="rss09:link"/>
                <xsl:variable name="description" select="rss09:description"/>
                <xsl:variable name="image" select="/rdf:RDF/rss09:image/rss09:url"/>
                <xsl:if test="$image">  
                        <img src="{$image}" style="float: right; margin: 2px;" />
                </xsl:if>
                <h3><a href="{$link}" title="{$description}"><xsl:value-of select="rss09:title" /></a></h3>
                <hr/>
        </xsl:template>
        <xsl:template match="channel">
                <xsl:variable name="link" select="link"/>
                <xsl:variable name="description" select="description"/>
                <xsl:variable name="image" select="image/url"/>
                <xsl:if test="$image">  
                        <img src="{$image}" style="float: right; margin: 2px;" />
                </xsl:if>
                <h3><a href="{$link}" title="{$description}"><xsl:value-of select="title" /></a></h3>
                <hr/>
                <ul><xsl:apply-templates select="item"/></ul>
        </xsl:template>
        <xsl:template match="item">
                <xsl:variable name="item_link" select="link"/>
                <xsl:variable name="item_title" select="description"/>
                <li><a href="{$item_link}" title="{$item_title}"><xsl:value-of select="title"/></a></li>
        </xsl:template>
        <xsl:template match="rss:item">
                <xsl:variable name="item_link" select="rss:link"/>
                <xsl:variable name="item_title" select="rss:description"/>
                <li><a href="{$item_link}" title="{$item_title}"><xsl:value-of select="rss:title"/></a></li>
        </xsl:template>
        <xsl:template match="rss09:item">
                <xsl:variable name="item_link" select="rss09:link"/>
                <xsl:variable name="item_title" select="rss09:description"/>
                <li><a href="{$item_link}" title="{$item_title}"><xsl:value-of select="rss09:title"/></a></li>
        </xsl:template>
        </xsl:stylesheet>