我想查看一个外部rss feed并在我的网站上显示它,但是那里有一些我不想要的过多信息,我也想根据xml的内容添加我自己的内容。 / p>
为了让我的解释更清楚,一个模拟示例将是一个rss feed,其中包括当前的一周和时间。然后我想把它拉进来,过滤掉一周中某一天的时间,然后将其替换为与星期几相关的图像。
编辑:好的,这是一个可能的例子:Yahoo - Today's weather rss
这显示了伦敦目前的天气(以及短暂的预测)。我如何查看yweather:condition节点的text属性并显示与之相关的图像,因此如果它现在显示“Fog”,我会显示雾的图像,如果它说太阳,则会显示图像太阳等等。
答案 0 :(得分:1)
使用强>:
/*/*/item/yweather:condition/@text
您在PL(托管XPath引擎)中注册了与名称空间"yweather"
关联的前缀"http://xml.weather.yahoo.com/ns/rss/1.0"
。
基于XSLT的验证:
<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>
应用于问题中链接所指向的XML文档:
<?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 -->
产生想要的结果:
<img src="Mostly Cloudy.jpg"/>
更新:OP现在已在评论中提供了确切的所需输出。下面是产生此输出的转换:
<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"><!DOCTYPE HTML></xsl:text>
<xsl:text>
</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>
将此转换应用于同一XML文档(上图)时,会生成所需结果:
<!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>