Transforming the RSS Feed to HTML
Now that you've obtained the RSS document, you need to write the instructions to display the contents. There are a number of sites on the Web that will perform RSS-to-HTML transformations for you, and
DevX exposes the feeds as HTML directly, in a form suitable for inclusion in an
<IFRAME>. However, you can easily construct your own XSLT stylesheet to customize the display. For example, the following template creates a 300-pixel wide
<div> containing the feed data, with the item titles formatted as links.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<! -- match the root element -->
<xsl:template match="/">
<xsl:apply-templates select="rss" />
</xsl:template>
<! -- match the rss element -->
<xsl:template match="rss">
<xsl:if test="@version != '2.0'">
<div id="error">
Unrecognized RSS feed version.</div>
</xsl:if>
<xsl:if test="@version = '2.0'">
<xsl:apply-templates select="channel" />
</xsl:if>
</xsl:template>
<! -- match the channel element -->
<xsl:template match="channel">
<div id="feed" style="width:300">
<a>
<xsl:attribute name="href">
<xsl:value-of select="link" />
</xsl:attribute>
<xsl:attribute
name="target">_blank</xsl:attribute>
<h2>
<xsl:value-of select="title" />
</h2>
</a>
<br />
<div style="font-size: xx-small">
<xsl:value-of select="copyright" />
</div>
<br />
<div id="items">
<xsl:apply-templates select="item" />
</div>
</div>
</xsl:template>
<! -- match the item element -->
<xsl:template match="item">
<div>
<a>
<xsl:attribute
name="target">_blank</xsl:attribute>
<xsl:attribute name="href">
<xsl:value-of select="link" />
</xsl:attribute>
<xsl:value-of select="title" />
</a>
<br />
<xsl:value-of select="description" />
</div>
<br />
</xsl:template>
</xsl:stylesheet>
The stylesheet shown above has four templates that matches the main RSS elementsthe ubiquitous root template (match="/"), which matches the root node of the document, the <rss> element, the <channel> element, and the <item> element.
- When the parser encounters an <rss> element, it checks the version attribute value. If it's not 2.0, the stylesheet returns an error; otherwise, it parses the <channel> element
- When the stylesheet encounters the <channel> element, it creates the 300-pixel wide <div> tag and displays the value of the channel <title> element, formatted as a link (using the <link> element as the link destination), and the copyright notice. Next, it performs the<xsl:apply-templates select="item" /> command, which selects each <item> element, in turn.
- When the stylesheet encounters an <item> element, it displays the title as a link and the description.