XSL应用于XML时不返回任何数据

时间:2014-07-28 15:25:17

标签: xml xslt xslt-1.0

我有一个XML,如下所示,由此网址http://www.huffingtonpost.com/tag/@sponsor_bluemoon-general/feed

表示
<feed xmlns="http://www.w3.org/2005/Atom">
  <script id="tinyhippos-injected"/>
  <title>sponsor_bluemoon General on Huffington Post</title>
  <link href="http://www.huffingtonpost.com/tag/@sponsor_bluemoon-general/feed" rel="self" type="application/rss+xml"/>
  <link rel="alternate" type="text/html" href="http://www.huffingtonpost.com/tag/%40sponsor_bluemoon-general"/>
  <logo>
    http://s.huffpost.com/images/HuffingtonPost_250x20.png
  </logo>
  <id>
    tag:www.huffingtonpost.com,2014:/tag/%40sponsor_bluemoon-general
  </id>
  <updated>2014-07-24T08:03:25Z</updated>
  <generator uri="http://www.huffingtonpost.com/">Huffington Post</generator>

  <entry>
    <title>
      The Second Half Of The MLB Season Is Going To Be Intense
    </title>
    <link rel="alternate" type="text/html" href="http://www.huffingtonpost.com/2014/07/24/ranking-the-mlb-division-_n_5610682.html"/>
    <id>
      http://www.huffingtonpost.com/2014/07/24/ranking-the-mlb-division-_n_5610682.html
    </id>
    <published>2014-07-24T08:03:25Z</published>
    <updated>2014-07-24T08:03:28Z</updated>
    <author>
      <name>The Huffington Post News Team</name>
      <uri>http://www.huffingtonpost.com/the-news/</uri>
    </author>
    <content type="html" xml:lang="en-US" xml:base="http://www.huffingtonpost.com/">
    It's going to be a fun, and very intense, second half of the baseball season. <br /> <br /> By the All-Star break, one division in the National League had a tie... <p>Read more: <a href="/tag/brew-pub">Brew-Pub</a>, <a href="/tag/baseball">Baseball</a>, <a href="/tag/mlb">Mlb</a>, <a href="/tag/major-league-baseball">Major League Baseball</a>, <a href="/tag/mlb-division-races">Mlb Division Races</a>, <a href="/sports">Sports News</a></p>
    </content>
    <link href="http://i.huffpost.com/gen/1924460/thumbs/s-OAKLAND-ANGELS-154x114.jpg" type="image/jpeg" rel="enclosure"/>
  </entry>
</feed>

节点被重复,因此我为每个&#34;这些入口节点提取标题作者id链接和内容。现在我写了xsl只提取作者和标题。但是我的xsl并没有返回任何东西。 我的XSL看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:media="http://search.yahoo.com/mrss/">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <rss xmlns:georss="http://www.georss.org/georss" xmlns:twitter="http://api.twitter.com" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
            <channel>
                <author>
                    <xsl:text>Content-Feed-Pics</xsl:text>
                </author>
                <xsl:for-each select="xml//entry[position() &lt; 10]">
                    <item>
                        <title><xsl:value-of select="title"/></title>
                        <author><xsl:value-of select="author/name"/></author>
                      <!-- <icon>
                       <xsl:value-of select="concat(entities/media//media_url,':thumb')"/>                    
                       </icon>-->


                        <!--link><xsl:value-of select="entities/media//media_url"/></link-->
                        <!--description><xsl:value-of select="text"/></description>
                        <pubDate><xsl:value-of select="created_at"/></pubDate-->
                       <!-- <xsl:apply-templates select="entities/media//media_url"/>-->
                    </item>
                </xsl:for-each>
            </channel>
        </rss>
    </xsl:template>

    <!--xsl:template match="entities/media//media_url"-->
      <!--  <enclosure>
                          <xsl:attribute name="url">
                         <xsl:value-of select="concat(., ':thumb')" />
                         </xsl:attribute>
                         <xsl:attribute name="type">
                          <xsl:text>image/jpg</xsl:text>
                          </xsl:attribute>
        </enclosure>-->
 <!--/xsl:template-->
</xsl:stylesheet>
​

请让我知道我哪里出错了。 在使用xml //条目之前,我使用了feed / entry和//条目,但这也没有用。 请指教。

http://feeds.contenthub.aol.com/syndication/2.0/feed/53d27420b4075的我的xsl是

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:media="http://search.yahoo.com/mrss/">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <rss xmlns:georss="http://www.georss.org/georss" xmlns:twitter="http://api.twitter.com" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
            <channel>
                <author>
                    <xsl:text>Content-Feed-Pics</xsl:text>
                </author>
                <!--xsl:for-each select="rss/channel/item[position() &lt; 30]"-->
                    <item>
                        <title><xsl:value-of select="rss/channel/item/title"/></title>
                        <author><xsl:value-of select="rss/channel/item/dc:creator"/></author>
                       <!--icon>
                       <xsl:value-of select="concat(entities/media//media_url,':thumb')"/>                    
                       </icon-->


                        <link><xsl:value-of select="rss/channel/item/link"/></link>
                        <description><xsl:value-of select="rss/channel/item/description"/></description>
                        <!--pubDate><xsl:value-of select="created_at"/></pubDate-->
                    </item>
                <!--/xsl:for-each-->
            </channel>
        </rss>
    </xsl:template>


</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

在这种情况下使用feed/entry是正确的,但是您的问题(假设您的XML格式正确)与命名空间的问题一致。

在原始XML中,由于http://www.w3.org/2005/Atom元素上的默认命名空间声明

,所有节点都是feed命名空间的一部分
<feed xmlns="http://www.w3.org/2005/Atom">

但是在你的XSLT中,你做(或做过)<xsl:for-each select="feed/entry[position() &lt; 10]">,那么这就是在NO命名空间中查找元素。这些元素与命名空间中的元素不同,因此不会匹配。

解决方案(至少在XSLT 1.0中)是确保在XSLT中正确声明命名空间,并在XPath中为所有元素添加相关前缀。例如

<xsl:for-each select="atom:feed/atom:entry[position() &lt; 10]">

试试这个XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" >
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <rss xmlns:georss="http://www.georss.org/georss" xmlns:twitter="http://api.twitter.com" version="2.0">
            <channel>
                <author>
                    <xsl:text>Content-Feed-Pics</xsl:text>
                </author>
                <xsl:apply-templates select="atom:feed/atom:entry[position() &lt; 10]" />
            </channel>
        </rss>
    </xsl:template>

    <xsl:template match="atom:entry">
       <item>
           <title><xsl:value-of select="atom:title"/></title>
           <author><xsl:value-of select="atom:author/atom:name"/></author>
        </item>
    </xsl:template>
</xsl:stylesheet>

请注意,我已将atom命名空间的声明移至主xsl:stylesheet,因此可以在整个XSLT中引用它。我还在这里展示了如何使用模板,因为如果没有别的东西,它会减少缩进。