我认为你遇到的问题是命名空间。您在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>
非常感谢Tim。我非常感谢这个详细的答案。我会在我的博客上发布解决方案,以便其他人可以受益。祝你有美好的一天.. –