2013-02-16 74 views
0

我造型的RSS源,但具有以下部分的問題:造型RSS提要使用XSLT,與CDATA標籤問題

<description> 
<![CDATA[ 
<img src="http://l.yimg.com/a/i/us/we/52/34.gif"/><br /> <b>Current Conditions:</b><br /> Fair, 73 F<BR /> <BR /><b>Forecast:</b><BR /> Sat - Clear. High: 78 Low: 62<br /> Sun - Mostly Sunny. High: 80 Low: 66<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Dubai__AE/*http://weather.yahoo.com/forecast/AEXX0004_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/> 
]]> 
</description> 

你可以看到我嘗試here

這裏是我的XSLT:

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html> 
    <head> 
    <title></title> 
    </head> 
    <body> 
    <table cellpadding="2" cellspacing="0" border="0" width="75%"> 
     <xsl:for-each select="rss/channel/item"> 

       <tr style="color:#0080FF;"> 
       <td style="text-align:left;font-weight:bold;"> 
        <xsl:value-of select ="title"></xsl:value-of> 
       </td> 
       </tr> 


       <tr style="color:#0080FF;"> 
       <td style="text-align:left;font-weight:bold;"> 
        <xsl:value-of select ="location"></xsl:value-of> 
        <xsl:value-of select="pubDate"/> 
       </td> 
       </tr> 


       <tr> 
       <td colspan="2" style="text-align:left;padding-top:10px;"> 
        <xsl:value-of select="description"/> 
       </td> 
       </tr> 

      <tr> 
      <td colspan="2" style="height:20px;"> 
       <hr></hr> 
      </td> 
      </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

我想從描述標記中獲取信息,以便我可以像我標題和發佈日期那樣對其進行設計。 Here是我嘗試設計的完整的XML RSS源。誰能幫我弄清楚爲什麼CDATA標籤搞砸了?

回答

4

儘量不要使用<xsl:for-each>。當您依賴<xsl:template><xsl:apply-templates>時,代碼將變得更清晰。

也嘗試使用CSS類並從輸出HTML中刪除內聯樣式。

如果您輸出的是經典HTML(不是XHTML),那麼請通過使用<xsl:output>來告訴XSLT處理器,並輸出文檔類型。

輸出問題將通過使用disable-output-escaping="yes"解決。請注意,並非每個XSL處理器都支持該屬性。根據XSLT規範,禁用輸出轉義的功能是可選的。

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> 
    <xsl:output method="html" indent="yes" 
    doctype-system='http://www.w3.org/TR/html4/strict.dtd' 
    doctype-public='-//W3C//DTD HTML 4.01//EN' 
    /> 

    <xsl:template match="/rss"> 
    <html> 
     <head> 
     <title></title> 
     </head> 
     <body> 
     <xsl:apply-templates select="channel" /> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="channel"> 
    <table cellpadding="2" cellspacing="0" border="0" width="75%"> 
     <xsl:apply-templates select="item" /> 
    </table> 
    </xsl:template> 

    <xsl:template match="item"> 
    <!-- ... --> 
    <tr> 
     <td colspan="2" style="text-align:left;padding-top:10px;"> 
     <xsl:value-of select="description" disable-output-escaping="yes" /> 
     </td> 
    </tr> 
    <!-- ... --> 
    </xsl:template> 
</xsl:stylesheet> 
+0

工作非常完美,非常感謝您的幫助,我會一直這樣做!謝謝! – deucalion0 2013-02-16 16:22:57