Alyda

Just a Girl Weblog

Remembering Who I Really Am

Use XSLT to Transform Your RSS Feed into XHTML

XSLT is a language for transforming XML documents into other XML documents. RSS documents (feed, web feed, or channel) and XHTML documents (web pages) are just XML documents, so we can use XSLT to transform an RSS document into a styled XHTML web page, and display it in a browser.

There are two approaches to applying XSLT to your XML content. You can perform these XSLT transformations server‑side on your web server, and serve up the resulting XHTML web page to a browser. The other is to use XSLT is to send a source XML document and an XSLT stylesheet to a browser, and ask the browser to apply the XSLT transformation for you.

Given the inconsistencies between [X]HTML support in the different browsers, it should come as no surprise that they offer varying support for XSLT as well. In this article I"ll describe how to use PHP to perform a server‑side transformation of your RSS document into XHTML.

The Feed

Note: The content:encoded element can be used in conjunction with the description element to provide an item's full content along with a shorter summary. While either the description or the content:encoded elements may contain the full content of an item, the description element is more widely supported than the content:encoded element in the context of an RSS 2.0 feed.

feed.xml
  • <?xml version="1.0" encoding="utf-8"?>
  • <rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  • <channel>
  • <description>Characterization or summary of the feed</description>
  • <link>http://blog.example.com/</link>
  • <title>Title of the Weblog</title>
  • <item>
  • <title>Title of the Item</title>
  • <link>http://blog.example.com/title-of-the-item/</link>
  • <pubDate>Sun, 17 Oct 2010 08:00:00 -1000</pubDate>
  • <category><![CDATA[XSLT]]></category>
  • <category><![CDATA[Tips]]></category>
  • <description><![CDATA[A summary of the item"s content.]]></description>
  • <content:encoded><![CDATA[<p>Define the full content of the item, suitable for presentation as XHTML.</p>]]></content:encoded>
  • </item>
  • </channel>
  • </rss>

The XHTML Page

index.php
  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  • "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  • <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  • <head>
  • <title>The Title of Your Weblog</title>
  • <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  • <meta http-equiv="Content-Language" content="en-us" />
  • <link charset="utf-8" rel="alternate" type="application/rss+xml" title="The Title of Your Weblog" href="http://blog.example.com/feed.xml" />
  • </head>
  • <body>
  • <div id="content">
  • <div id="content-main">
  • <?php
  • $params = array('max' => POST_MAX);
  • $html = transform_XML(FEED_XSL, FEED_XML, $params);
  • echo($html);
  • ?>
  • </div>
  • <div id="sidebar">
  • <h2>Current</h2>
  • <?php
  • $params = array('max' => POST_MAX);
  • $html = transform_XML(CURRENT_XSL, FEED_XML, $params);
  • echo($html);
  • ?>
  • <h2>Tags</h2>
  • <?php
  • $html = transform_XML(TAGS_XSL, FEED_XML);
  • echo($html);
  • ?>
  • </div>
  • </div>
  • </body>
  • </html>

The XSL Stylesheets

Feed Stylesheet

The feed's content:encoded element must be suitable for presentation as [X]HTML, so no markup is applied in the stylesheet.

feed.xslt
  • <?xml version="1.0" encoding="utf-8"?>
  • <xsl:stylesheet version="1.0"
  • xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  • xmlns:content="http://purl.org/rss/1.0/modules/content/"
  • exclude-result-prefixes="content">
  • <xsl:param name="max">5</xsl:param>
  • <xsl:output method="xml" omit-xml-declaration="yes" indent="no"/>
  • <xsl:template match="/">
  • <xsl:apply-templates select="//item/content:encoded"/>
  • </xsl:template>
  • <xsl:template match="content:encoded">
  • <xsl:if test="position() &lt;= $max">
  • <xsl:value-of disable-output-escaping="yes" select="."/>
  • </xsl:if>
  • </xsl:template>
  • </xsl:stylesheet>

Current Stylesheet

Generate an [X]HTML list—including hyperlinks—to display the feed's title elements.

current.xslt
  • <?xml version="1.0" encoding="utf-8"?>
  • <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  • <xsl:param name="max">5</xsl:param>
  • <xsl:output method="xml" omit-xml-declaration="yes" indent="no"/>
  • <xsl:template match="/">
  • <ul class="current">
  • <xsl:apply-templates select="//item[position() &lt;= $max]/title"/>
  • </ul>
  • </xsl:template>
  • <xsl:template match="title">
  • <li>
  • <xsl:text disable-output-escaping="yes">&#187; </xsl:text>
  • <a href="{../link}" title="{.}"><xsl:value-of disable-output-escaping="yes" select='.' /></a>
  • </li>
  • </xsl:template>
  • </xsl:stylesheet>

Tags Stylesheet

Generate an [X]HTML list—including hyperlinks and item count—to display the feed's category elements.

Grouping and Sorting: the Muench Method

For this stylesheet, we'll look at using <xsl:key> to group items in an XML document. Commonly referred to as the Muench method—after Steve Muench—this approach has three steps:

  1. Define a key for the property we want to use for grouping.
  2. Select all of the category nodes, sorted alphabetically.
  3. Do some magic with the generate‑id() and key() functions to find unique values. Output is generated only if we are processing the first node returned by the key that matches the value of the current node.
tags.xslt
  • <?xml version="1.0" encoding="utf-8"?>
  • <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  • <xsl:param name="url" />
  • <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  • <xsl:key name="catname" match="category" use="." />
  • <xsl:template match="/">
  • <ul class="taglist">
  • <xsl:apply-templates select="//category">
  • <xsl:sort select="." />
  • </xsl:apply-templates>
  • </ul>
  • </xsl:template>
  • <xsl:template match="category">
  • <xsl:if test="generate-id()= generate-id(key('catname', .)[1])">
  • <li><xsl:text disable-output-escaping="yes">&#187; </xsl:text>
  • <xsl:variable name="lc">
  • <xsl:value-of disable-output-escaping="yes" select="translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ', 'abcdefghijklmnopqrstuvwxyz-')" />
  • </xsl:variable>
  • <xsl:variable name="cat">
  • <xsl:value-of select="$url" />
  • <xsl:text>category/</xsl:text>
  • <xsl:value-of disable-output-escaping="yes" select="$lc" />
  • <xsl:text>/</xsl:text>
  • </xsl:variable>
  • <a title="{.}" href="{$cat}"><xsl:value-of disable-output-escaping="yes" select="." /> (<xsl:value-of select="count(key('catname', .))"/>)</a>
  • </li>
  • </xsl:if>
  • </xsl:template>
  • </xsl:stylesheet>

Where to Go for More Information

Filed under: , , , ,