dudledok
dudledok

Reputation: 2880

How to use xslt to step through an rss feed and only display the wanted information

I want to look at an external rss feed and display it on my site but there's some excessive information in there which I don't want and I also want to add in my own content dependent on the content of the xml.

To try and make my explanation clearer a mock example would be an rss feed which includes the current day of the week and time. I would then want to pull that in, filter out the time to just have the day of the week, and replace that with an image relevant to the day of the week.

Edit: Okay, here is a possible example: Yahoo - Today's weather rss

This shows the current weather in London (along with a short forecast). How would I look at the text attribute of the yweather:condition node and display an image relevant to it, so if it says "Fog" as it currently does, I would display an image of fog, if it said sun, an image of a sun, and so on.

Upvotes: 0

Views: 1151

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243539

Use:

/*/*/item/yweather:condition/@text

where you have registered in your PL (that hosts the XPath engine) the prefix "yweather", associated to the namespace "http://xml.weather.yahoo.com/ns/rss/1.0".

XSLT-based verification:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"
 exclude-result-prefixes="yweather">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <img src="{/*/*/item/yweather:condition/@text}.jpg"/>
 </xsl:template>
</xsl:stylesheet>

when applied against the XML document pointed to by the link in the question:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
    <channel>
        <title>Yahoo! Weather - London, GB</title>
        <link>http://us.rd.yahoo.com/dailynews/rss/weather/London__GB/*http://weather.yahoo.com/forecast/UKXX0085_c.html</link>
        <description>Yahoo! Weather for London, GB</description>
        <language>en-us</language>
        <lastBuildDate>Sun, 20 Nov 2011 2:50 pm GMT</lastBuildDate>
        <ttl>60</ttl>
        <yweather:location city="London" region=""   country="United Kingdom"/>
        <yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/>
        <yweather:wind chill="7"   direction="0"   speed="3.22" />
        <yweather:atmosphere humidity="100"  visibility="0.9"  pressure="1015.92"  rising="0" />
        <yweather:astronomy sunrise="7:26 am"   sunset="4:05 pm"/>
        <image>
            <title>Yahoo! Weather</title>
            <width>142</width>
            <height>18</height>
            <link>http://weather.yahoo.com</link>
            <url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
        </image>
        <item>
            <title>Conditions for London, GB at 2:50 pm GMT</title>
            <geo:lat>51.51</geo:lat>
            <geo:long>-0.13</geo:long>
            <link>http://us.rd.yahoo.com/dailynews/rss/weather/London__GB/*http://weather.yahoo.com/forecast/UKXX0085_c.html</link>
            <pubDate>Sun, 20 Nov 2011 2:50 pm GMT</pubDate>
            <yweather:condition  text="Mostly Cloudy"  code="28"  temp="7"  date="Sun, 20 Nov 2011 2:50 pm GMT" />
            <description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/28.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 7 C<BR />
<BR /><b>Forecast:</b><BR />
Sun - Cloudy. High: 9 Low: 8<br />
Mon - Mostly Cloudy. High: 12 Low: 10<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/London__GB/*http://weather.yahoo.com/forecast/UKXX0085_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
            <yweather:forecast day="Sun" date="20 Nov 2011" low="8" high="9" text="Cloudy" code="26" />
            <yweather:forecast day="Mon" date="21 Nov 2011" low="10" high="12" text="Mostly Cloudy" code="28" />
            <guid isPermaLink="false">UKXX0085_2011_11_21_7_00_GMT</guid>
        </item>
    </channel>
</rss>
<!-- api9.weather.ac4.yahoo.com compressed/chunked Sun Nov 20 07:44:04 PST 2011 -->

produces the wanted result:

<img src="Mostly Cloudy.jpg"/>

Update: The OP has now provided in a comment the exact wanted output. Below is the transformation that produces this output:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"
 exclude-result-prefixes="yweather">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
    <xsl:text
    disable-output-escaping="yes">&lt;!DOCTYPE HTML></xsl:text>
    <xsl:text>&#xA;</xsl:text>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        </head>
        <body>
            <div id="weather">
                  <img src="{/*/*/item/yweather:condition/@text}.jpg"/>
            </div>
        </body>
    </html> 
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the same XML document (above), the wanted result is produced:

<!DOCTYPE HTML>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
   </head>
   <body>
      <div id="weather">
         <img src="Mostly Cloudy.jpg"/>
      </div>
   </body>
</html>

Upvotes: 1

Related Questions