Parsing RSS with XSLT, in an iframe

(c) Wieland, 2005. Published under the GNU Free Documentation License, version 1.2. See here for details.
Last modified: 09 Sept 2005

Following this question in my FAQ on Multiblogging, I've been experimenting with parsing RSS in an iframe, using XSLT. Turns out it works perfectly, as long as your host serves XML with the correct MIME-type.

Below, you can see my sample RSS 2.0 feed (yes, it's valid). It's being parsed through XSLT and rendered as HTML. The feed is embedded in this page using an <object>-tag (the dashed border was added for clarity):

Update (13 Oct 2007): Firefox 2+ and IE7 ignore XSL(T) in RSS, applying their own styles in stead. Regretfully, the only way around this is a lame hack, which I've applied below. For more info, view the RSS source or see this article and, of course, bug #338621. Safari shows the same behaviour, but the hack doesn't work. IMHO, RSS/XSTL in Safari is fundamentally broken.

<object name="mbrss" type="application/xml" width="500" height="450" data="./multiblog.rss" style="border: 1px dashed #ddd;"></object>

Here's the source code for the RSS (multiblog.rss):

<?xml version="1.0" encoding="utf-8"?>
<!-- Firefox 2.0 and Internet Explorer 7 use simplistic feed sniffing to override desired presentation behavior 
for this feed, and thus we are obliged to insert this comment, a bit of a waste of bandwidth, unfortunately.
 This should ensure that the following stylesheet processing instruction is honored by these new browser versions. 
For some more background you might want to visit the following bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=338621
-->
<?xml-stylesheet type="text/xsl" href="multiblog.xsl"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
    <title>RSS 2.0 Feed</title>
    <link>http://www.alamagordo.org/</link>
    <description>This is an experiment. I'm trying to parse an RSS feed using XSLT.</description>
    <language>en-us</language>
    <item>
      <title>Multiblog</title>
      <link>http://www.alamagordo.org/multiblogfaq.html</link>
      <description>I got the idea while editing my Multiblog FAQ.</description>
      <dc:creator>Wieland</dc:creator>
      <dc:date>2005-09-09</dc:date>    
    </item>
    <item>
      <title>In particular...</title>
      <link>http://www.alamagordo.org/multiblogfaq.html#q6</link>
      <description>...while writing this bit on RSS.</description>
      <dc:creator>Wieland</dc:creator>
      <dc:date>2005-09-09</dc:date>    
    </item>
  </channel>
</rss>

And here's the XSLT (multiblog.xsl):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:dc="http://purl.org/dc/elements/1.1/" version="1.0">
<xsl:template match="//rss">
  <html>
  <body>
    <h1><xsl:value-of select="/rss/channel/title"/></h1>
    <p><xsl:value-of select="/rss/channel/description"/></p>
    <xsl:element name="a">
      	<xsl:attribute name="href">
      		<xsl:value-of select="link"/>
      	</xsl:attribute>
      	Read more...
    </xsl:element>
    <xsl:for-each select="/rss/channel/item">
        <h2><xsl:value-of select="title"/></h2>
        <p><xsl:value-of select="description"/></p>
        <p>
    <xsl:element name="a">
      	<xsl:attribute name="href">
        	<xsl:value-of select="link"/>
      	</xsl:attribute>
      	Read more...
    </xsl:element>
    </p>
    <p style="color: #ccc; font-size: 0.7em;"><xsl:value-of select="dc:creator"/></p>
    <p style="color: #ccc; font-style: italic; font-size: 0.7em;"><xsl:value-of select="dc:date"/></p>
    </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

If you have any questions or comments, please contact me.