0

我必须格式化 Apple RSS 提要以显示网站中最热门的 iphone 应用程序。我下载了 XML 文件,并认为应用样式表很简单,但它变成了一项艰巨的工作......这是我试图应用的 XSL:非常简单

<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 文件的全部内容..

我在 Internet 上只能找到一个关于此的主题,而且它也没有有效的解决方案。如果人们现在正在展示带有 i-tunes 应用程序的网站,这应该是一个非常熟悉的问题。

4

1 回答 1

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/Atom”。尽管您已经在 XSLT 中声明了这一点,但您并没有真正使用它,而且您的 XSLT 代码正在尝试匹配不属于任何名称空间的元素。

还有一个问题是您的 XSLT 也没有考虑提要元素。您需要做的是将初始模板匹配替换<xsl:template match="/">为以下内容

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

xsl:for-each会变成这样

<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>
于 2012-06-04T09:03:23.143 回答