【问题标题】:How to use xslt to step through an rss feed and only display the wanted information如何使用 xslt 逐步浏览 rss 提要并仅显示想要的信息
【发布时间】:2011-11-20 00:44:13
【问题描述】:

我想查看外部 rss 提要并将其显示在我的网站上,但其中有一些我不想要的过多信息,我还想根据 xml 的内容添加我自己的内容。

为了让我的解释更清楚,一个模拟示例将是一个包含当前星期几和时间的 rss 提要。然后我想把它拉进去,过滤掉时间,只剩下星期几,然后用与星期几相关的图像替换它。

编辑:好的,这是一个可能的例子:Yahoo - Today's weather rss

这显示了伦敦的当前天气(以及一个简短的预测)。我将如何查看 yweather:condition 节点的文本属性并显示与其相关的图像,所以如果它像当前那样显示“雾”,我将显示雾的图像,如果它说太阳,则显示太阳等等。

【问题讨论】:

  • 请提供一个示例(完整和小型)XML 文档以及所需的确切输出结果。
  • @_dudledok:你还没有提供确切的想要的结果。

标签: xslt rss


【解决方案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">&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>

当此转换应用于同一个 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>

【讨论】:

  • 如果我使用您提供的 xslt 创建片段,我将如何在 html 页面中使用 html 输出,以便图像显示在指定的 div 中。
  • @dudledok:对不起,我不明白你上一条评论中的问题。核心原因是您没有提供确切想要的输出并继续提出模糊的问题。
  • 您的代码输出很完美,但我想将其输出到 html 页面中的 div 中。想要的输出:
  • @dudledok:我用一个转换更新了我的答案,该转换产生了您在评论中提供的输出。
  • 我不想对所有事情都使用 xsl,因为我已经创建了一个主要是 html 和 css 的解决方案,这就是为什么我问是否有可能拥有一个现有的 html 页面并从xslt.
猜你喜欢
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多