2012-06-04 131 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。我非常感谢这个详细的答案。我会在我的博客上发布解决方案,以便其他人可以受益。祝你有美好的一天.. –