2012-06-04 49 views
0

我必須格式化Apple RSS提要才能在網站上顯示頂級iphone應用程序。我下載的XML文件,並認爲這會是簡單適用的樣式表,但它把一個工作赫克... 這裏是XSL IAM嘗試應用:非常簡單使用XSL將Apple XML轉換爲HTML

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:im="http://itunes.apple.com/rss"> 

<xsl:template match="/"> 


<tr> 
    <th>ID</th> 
    <th>Title</th> 
</tr> 
<xsl:for-each select="entry"> 
<tr> 
    <td><xsl:value-of select="id"/></td> 
    <td><xsl:value-of select="title"/></td> 
    <td><xsl:value-of select="category"/></td> 

</tr> 
</xsl:for-each> 

</xsl:template> 

</xsl:stylesheet> 

XML供稿我想格式可以從http://itunes.apple.com/rss/generator/下載(選擇iOS應用程序並點擊生成)。

在此請幫助.. XML文件並沒有改變我做出XSL文件的任何變化,它始終顯示XML文件的全部內容..

我可以在此找到上只有一個話題互聯網,它也沒有一個工作解決方案。如果人們現在正在用i-tunes應用展示網站,這應該是相當熟悉的問題。

回答

2

我認爲你遇到的問題是命名空間。您在XSLT中沒有適當地考慮它們。看一個樣品進料,根元素如下:

<feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en"> 

這意味着,除非另有說明,所有的元素都是與URI的命名空間的一部分「http://www.w3.org/2005/原子」。儘管您已在XSLT中聲明瞭這一點,但您並未真正使用它,而您的XSLT代碼正在嘗試匹配不屬於任何名稱空間的元素。

還有一個問題,就是您的XSLT並不會考慮飼料元素。你需要做的就是用下面的

<xsl:template match="/atom:feed"> 

XSL替換<xsl:template match="/">初始模板匹配:然後,每個將變得像這樣

<xsl:for-each select="atom:entry"> 

以下是完整的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:im="http://itunes.apple.com/rss"> 
    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="/atom:feed"> 
     <tr> 
     <th>ID</th> 
     <th>Title</th> 
     </tr> 

     <xsl:for-each select="atom:entry"> 
     <tr> 
      <td> 
       <xsl:value-of select="atom:id"/> 
      </td> 
      <td> 
       <xsl:value-of select="atom:title"/> 
      </td> 
      <td> 
       <xsl:value-of select="atom:category/@label"/> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

這應該有希望輸出一些結果。

請注意,通常最好使用模板匹配,而不是使用xsl:for-each以鼓勵重新使用模板,使用更少的縮進來整理代碼。這也會起作用

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:im="http://itunes.apple.com/rss"> 
    <xsl:output method="html" indent="yes"/> 
    <xsl:template match="/atom:feed"> 
     <tr> 
     <th>ID</th> 
     <th>Title</th> 
     </tr> 
     <xsl:apply-templates select="atom:entry"/> 
    </xsl:template> 

    <xsl:template match="atom:entry"> 
     <tr> 
     <td> 
      <xsl:value-of select="atom:id"/> 
     </td> 
     <td> 
      <xsl:value-of select="atom:title"/> 
     </td> 
     <td> 
      <xsl:value-of select="atom:category/@label"/> 
     </td> 
     </tr> 
    </xsl:template> 
</xsl:stylesheet> 
+0

非常感謝Tim。我非常感謝這個詳細的答案。我會在我的博客上發佈解決方案,以便其他人可以受益。祝你有美好的一天.. –