Jason Salas' WebLog

On-air and online: making people laugh, making people think, pissing people off

Sponsors

ASP.NET sites that kick ass

Pals with blogs

Podcasts I listen to

Display podcast metadata in an RSS feed using XSLT

One thing that I like doing while listening to podcasts is reading the metadata contained within a show's RSS feed.  Being a geek, I don't mind reading straight XML, but I realize this isn't the most comfortable means of receiving sensory input for most people.

So, I built a very simple stylesheet in XSLT that parses/displays an RSS 2.0 feed's major points of interest.  This type of display is great if you're planning on building a directory and want a quick glance at the information about a show. 

There's really nothing particularly special or earth-shattering about the code, other than it accounts for the fact that many podcast producers put tagged HTML within their RSS feed's <DESCRIPTION> element, and allows the content to be formatted as such (using the disable-output-escaping="yes" attribute on the <xsl:value-of> element.  Also, it uses a compound function to display the length of a show's MP3 file in a more friendly decimal format "XX.XX", not the raw number of bytes (File size: <xsl:value-of select='format-number(number(enclosure/@length div "1024000"),"0.0")'/>MB).


XSLT CODE (formatRSSFeed.xslt)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <p style="font-size:13pt;font-weight:bold;">
            <xsl:value-of select="/rss/channel/title"/>
            <br/>
            <span style="font-size:11pt;">
                <xsl:value-of select="/rss/channel/description"/>
                <br/><br/>
                <a>
                <xsl:attribute name="href">
                    <xsl:value-of select="/rss/channel/link"/>
                </xsl:attribute>
                Subscribe to this podcast</a>
            </span>
        </p>
        <hr/>
        <br/>
        <xsl:for-each select="/rss/channel/item">
            <p style="font-size:9pt;">
                <a style="font-size:14pt;font-weight:bold;">
                    <xsl:attribute name="href">
                        <xsl:value-of select="link"/>
                    </xsl:attribute>
                    <xsl:value-of select="title"/>
                </a>
                <br/>
                <xsl:value-of select="description" disable-output-escaping="yes"/>
                <br/>  
                <p align="right" style="font-size:7pt;">
                         <b>File size: <xsl:value-of select='format-number(number(enclosure/@length div "1024000"),"0.0")'/>MB
                    ||
                        <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="link"/>
                            </xsl:attribute>
                            Download show
                        </a>
                        </b>
                </p>
                <hr/>
            </p>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>



Since this is XSLT, you could use any platform to develop clients, although in this particular example I built a web client with ASP.NET (below).




Just for posterity, here's the code I used for the client:

WEB CLIENT (podcast-main.aspx)

<%@ Page Language="C#" Debug="false" EnableSessionState="false" EnableViewState="false" ContentType="text/html" %>
<script runat="server">

    protected void Page_Load(object sender,EventArgs e)
    {
        const string defaultContent = "podcast";
        string content = (Request.QueryString["content"] != null) ? Request.QueryString["content"] : defaultContent;
        content = content + ".xml";
   
        xmlPodcasts.DocumentSource = content;
    }

</script>
<html>
    <head>
        <title>Display podcast RSS feeds with XSLT</title>
    </head>
    <body>
        <form runat="server">
            <asp:Xml id="xmlPodcasts" TransformSource="formatRSSFeed.xslt" runat="server"/>
        </form>
    </body>
</html>


Note that in the client's code, the physical locations of the XML files containing the RSS feeds are on the same box (my default RSS feed is an XML file named "podcast.xml" containing metadata about my show.  This was just my implementation, but it certainly isn't too hard to build a similar process to read RSS feeds over the Internet in ASP.NET (hint: use an XmlDocument object).

Happy programming!

Comments

K. Adams said:

Nice XSL stylesheet, but how can I get/display the "URL" value of the enclosure node. Because some RSS feeds are using the "link" value for the main URL and the enclosure node for the "MP3".
# July 13, 2005 7:47 AM

K. Adams said:

I've found the solution! :-D

<?xml version="1.0" ?>
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:template match="/rss/channel">
- <html>
- <head>
<title>WGBH Morning Stories</title>
</head>
- <body>
WGBH Morning Stories
<br />
<xsl:value-of select="description" />
<br />
<br />
<hr />
<br />
- <xsl:for-each select="item">
- <b>
<xsl:value-of select="title" />
</b>
<br />
<br />
<xsl:value-of select="description" />
<br />
<br />
- <!--
<a>
<xsl:attribute name="href">
<xsl:value-of select="link" />
</xsl:attribute>
<b>Stream</b>
</a>


-->
- <!-- Only show "Download MP3 if there are Enclosures
-->
- <xsl:if test="enclosure/@url">
<br />
- <a>
- <xsl:attribute name="href">
<xsl:value-of select="enclosure/@url" />
</xsl:attribute>
<b>Download MP3</b>
</a>
-
<xsl:value-of select="format-number(enclosure/@length div 1024 div 1024,"#.00")" />
MB
</xsl:if>
<br />
<br />
<hr />
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
# July 13, 2005 9:56 AM

Jason Salas said:

Good job!
# July 13, 2005 4:26 PM

sprite said:

Felicito, que palabras..., el pensamiento admirable  

http://eru1.myftp.biz/  

frank

# August 17, 2011 2:08 AM

rylee said:

su idea es muy buena  

http://rsfiles.servehttp.com/  

snowball

# August 26, 2011 9:02 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)