繁体   English   中英

使用XSL将Apple XML转换为HTML

[英]Apple XML to HTML using XSL

我必须格式化Apple RSS feed以显示网站中的顶级iPhone应用程序。 我下载了XML文件,并认为应用样式表很简单但是它变得很糟糕...这是XSL iam试图应用的:非常简单

<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应用程序显示网站应该是非常熟悉的问题。

我认为你遇到的问题是命名空间。 您在XSLT中没有正确地考虑它们。 查看示例Feed,根元素如下:

<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也没有考虑feed元素。 您需要做的是将<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM