2014-03-19 73 views
2

XSLT新手問題。 我想使用xslt從rss文件創建一個html列表。 我能夠這樣做與rss文件和xslt文件。使用xslt創建一個rss文件的html列表

但現在我嘗試創建html列表輸出爲空。

這是我當前的代碼:

string xmlsrc = "http://.../News.rss"; 
string Password = "myPass"; 
string UserAccount = "myAcc"; 
string DomainName = "myDom"; 
string xslsrc = "RSS91.xslt"; 

if (xmlsrc != "") 
{ 
    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(xmlsrc); 

    if (UserAccount != "") 
    { 
     wr.Credentials = new NetworkCredential(UserAccount, Password, DomainName); 
    } 

    wr.Timeout = 10000; 
    WebResponse resp = wr.GetResponse(); 
    Stream stream = resp.GetResponseStream(); 

    XmlTextReader reader = new XmlTextReader(stream); 
    reader.XmlResolver = null; 

    XmlDocument doc = new XmlDocument(); 
    doc.Load(reader); 

    xmlRSS.Document = doc; 
} 
xmlRSS.TransformSource = xslsrc; 

我的XSLT

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="html" indent="yes" encoding="iso-8859-1" /> 

    <xsl:template match="/"> 
    <xsl:for-each select="menus"> 
     <ul> 
     <xsl:for-each select="menu"> 
      <li> 
      <a href="{title}"> 
       <xsl:value-of select="title" /> 
      </a> 
      <ul> 
       <xsl:for-each select="submenu"> 
       <li> 
        <a href="{title}"> 
        <xsl:value-of select="title" /> 
        </a> 
       </li> 
       </xsl:for-each> 
      </ul> 
      </li> 
     </xsl:for-each> 
     </ul> 
    </xsl:for-each> 
    </xsl:template> 

    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

回答

3

你似乎並不具備處理RSS文件模板的XSLT。

如果你有一個結構的RSS文件:

/rss 
    /channel 
     /item 
     /title 
     /link 
    /channel 
     /item 
     /title 
     /link 

您可以生成一個列表,打印該鏈接到您的XSLT menusrssmenuchannelsubmenuitem更換飼料。此外,您的href指向的文字不是鏈接,因此請將href="{title}"替換爲href="{link}"。此模板可能有效:

<xsl:template match="/"> 
    <xsl:for-each select="rss"> 
     <ul> 
      <xsl:for-each select="channel"> 
       <li> 
        <a href="{link}"> 
         <xsl:value-of select="title" /> 
        </a> 
        <ul> 
         <xsl:for-each select="item"> 
          <li> 
           <a href="{link}"> 
            <xsl:value-of select="title" /> 
           </a> 
          </li> 
         </xsl:for-each> 
        </ul> 
       </li> 
      </xsl:for-each> 
     </ul> 
    </xsl:for-each> 
</xsl:template> 

您仍可能有問題。可能有名稱空間,如​​或feedburner,或者您的根元素可能是channel而不是rss。對於命名空間,你應該將它們添加到標題:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
    xmlns:atom10="http://www.w3.org/2005/Atom" 
    xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"> ... 

爲了避免與不同的根元素的問題,讓你的模板匹配channel和使用/建立你的HTML的結構。這是一個完整的樣式表。如果您在<rss>有根文件中有很多頻道,它將打印它們的列表,其中包含項目的子列表。如果您有一個<channel>根目錄,它也可以工作,打印唯一列表項目及其子目錄中的rss項目。

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
    xmlns:atom10="http://www.w3.org/2005/Atom" 
    xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"> 
    <xsl:output method="html" indent="yes" encoding="iso-8859-1" /> 

    <xsl:template match="channel"> 
     <li> 
      <a href="{link}"> 
       <xsl:value-of select="title" /> 
      </a> 
      <ul> 
       <xsl:for-each select="item"> 
        <li> 
         <a href="{link}"> 
          <xsl:value-of select="title" /> 
         </a> 
        </li> 
       </xsl:for-each> 
      </ul> 
     </li> 
    </xsl:template> 

    <xsl:template match="/"> 
     <html> 
      <head> 
       <title>RSS feeds</title> 
      </head> 
      <body> 
       <ul> 
       <xsl:apply-templates select="//channel"/> 
       </ul> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 
+0

非常感謝幫助,現在正在工作! –

+0

@ user3436943如果解決了您的問題,請接受此答案(標記左側的勾號以綠色顯示)。 –