2011-11-20 7 views
0

外部のRSSフィードを見て自分のサイトに表示したいのですが、そこには私が欲しくない情報がいくつかあります。 xmlの内容に応じて独自のコンテンツを追加したいxsltを使用してrssフィードをステップ実行し、必要な情報のみを表示する方法

私の説明をわかりやすくしようとすると、今日の曜日と時間を含むRSSフィードになります。私はそれを取り入れて、曜日を持っている時間を除き、曜日に関連する画像で置き換えたいと思うでしょう。

編集:さて、ここで可能な例である:Yahoo - Today's weather rss

これは(短い予想と一緒に)ロンドンの現在の天気が表示されます。どのように私はyweather:条件ノードのテキスト属性を見てそれに関連する画像を表示するでしょうか?それで、現在のように「霧」と言えば、霧の画像を表示します。太陽などがあります。

+1

、サンプルを提供してください(以下、この出力を生成する変換であります完全で小型の)XML文書と正確に求められる出力結果。 –

+0

更新された質問 – dudledok

+0

@_dudledok:あなたは依然として正確な結果を提供していません。 –

答えて

1

使用

(XPathエンジンをホストする)あなたがPLに登録されている
/*/*/item/yweather:condition/@text 

名前空間"http://xml.weather.yahoo.com/ns/rss/1.0"に関連する接頭辞"yweather"、。

XSLTベースの検証は:

<?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 --> 

募集結果を生成します:XMLドキュメントに対して適用

<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> 

は疑問のリンクによって指さ

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

更新:OPはコメントに正確な出力を提供しました。この変換は、同じXML文書(上記)に印加さ​​れる

<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> 

、所望の結果が生成される:

<!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> 
+0

指定したxsltを使用してフラグメントを作成した場合、htmlページでhtml出力を使用すると、指定したdivにイメージが表示されます。 – dudledok

+0

@dudledok:申し訳ありませんが、私はあなたの最後のコメントの質問を理解していません。中核となるのは、正確な出力を提供しておらず、あいまいな質問を続けているということです。 –

+0

あなたのコードからの出力は完璧ですが、htmlページ内のdivに出力したいと思います。 募集出力: <!DOCTYPE HTML>

dudledok

関連する問題