Skip to content

Transforming RSS with XSLT

Marvin Frederickson edited this page Jan 9, 2014 · 10 revisions

The Display Format of XSLT allows users to specify an XSL Transformation to apply to the entirety of the RSS XML that comes back. This provides for very powerful manipulation of the feed.

For a simple example, consider that we want to show the time for sunrise and sunset along with the current weather conditions. If we specify an RSS feed of http://weather.yahooapis.com/forecastrss?w=2508215, and a display format of XSLT, we can then use this xsl in the XLS Tranform field to get what we want. We could just as easily get the wind chill, speed, direction, humidity, barometric pressure, etc.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/rss/channel">
<p>Sunrise <xsl:value-of select="yweather:astronomy/@sunrise"/>,
 Sunset <xsl:value-of select="yweather:astronomy/@sunset"/>
</p>
<xsl:value-of disable-output-escaping="yes" select="item/description"/>
</xsl:template>
</xsl:stylesheet>

Or perhaps you want a more abbreviated weather status (two lines of text), along with any warnings from Wunderground http://rss.wunderground.com/auto/rss_full/NY/Troy.xml?units=english. You could use the XSL below to get:

  • Current Conditions : 1.1F, Light Snow - 6:13 PM EST Jan. 2
  • Temperature: 1.1°F | Humidity: 84% | Pressure: 30.03in (Rising) | Conditions: Light Snow | Wind Direction: North | Wind Speed: 1.2mph
  • Winter Storm Warning for Rensselaer County in effect until 10:00 AM EST on January 03, 2014
<?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"/>
  <xsl:template match="/">
    <xsl:apply-templates select="/rss/channel/item[1]"/>
    <xsl:apply-templates select="/rss/channel/item[contains(title,'Warning')]"/>
  </xsl:template>

  <xsl:template match="item">
    <p>
      <xsl:value-of select="title"/><br/>
      <xsl:value-of disable-output-escaping="yes" select="description"/>
    </p>
  </xsl:template>

  <xsl:template match="item[contains(title,'Warning')]">
    <p>
      <strong><xsl:value-of disable-output-escaping="yes" select="description"/></strong>
    </p>
  </xsl:template>
</xsl:stylesheet>

Or, lets say that you want the first 10 top stories from CNN as individual pieces of content. Your feed would be http://rss.cnn.com/rss/cnn_topstories.rss and your XSL would look like this:

<?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"/>
  <xsl:template match="/">
    <xsl:apply-templates select="//item[position() &lt; 11]"/>
  </xsl:template>

  <xsl:template match="item">
    <content-item>
      <h1><xsl:value-of select="title"/></h1>
      <p><xsl:value-of disable-output-escaping="yes" select="description"/></p>
    </content-item>
  </xsl:template>
</xsl:stylesheet>

Each node found matching the XPath //content-item in the resulting transformation will become a separate piece of content. If the resulting transformation does not contain any content-item nodes, or if it cannot be parsed as valid xml, then it will be treated as a single piece of content.

Don't want the feedflare links to show with your news items? Just import a custom template that includes a css file that contains:

content-item div.feedflare {
  display:none;
}
Clone this wiki locally