将rss feed转换为xml时出现XSLT问题

时间:2014-04-29 12:06:19

标签: xml xslt transformation

这是我的xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1990/XSL/Transform" version="1.0">
<xsl:output method="xml" version="2.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:element name="root">
<xsl:apply-template select="rss/channel/item/pubDate"/>
</xsl:element>
</xsl:template>

<xsl:template match="rss/channel/item/pubDate">
<xsl:element name="rawr" >
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

以下是我正在以xml:http://feeds.bbci.co.uk/news/rss.xml?edition=uk

阅读的RSS Feed 关于这个令人愤怒的事情是我没有得到任何错误信息;我一无所获。

我期望的输出结果如下:

<root>
<rawr>
<pubDate>
22/12/1766
</pubDate>
</rawr>
</root>

非常感谢任何指导!

2 个答案:

答案 0 :(得分:2)

两个项目。首先你需要改变

xmlns:xsl="http://www.w3.org/1990/XSL/Transform"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:apply-template select="rss/channel/item/pubDate"/>

<xsl:apply-templates select="rss/channel/item/pubDate"/>

如果您想为每个<pubDate添加<rawr>,则需要更改

<xsl:template match="rss/channel/item/pubDate">
    <xsl:element name="rawr" >
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

<xsl:template match="rss/channel/item/pubDate">
    <xsl:element name="rawr" >
        <pubDate><xsl:value-of select="."/></pubDate>
    </xsl:element>
</xsl:template>

答案 1 :(得分:1)

  

我没有收到任何错误消息;我一无所获。

然后问题出在其他地方。如果您举一个传入Feed的示例:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/shared/bsp/xsl/rss/nolsol.xsl"?>

<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">  
  <channel> 
    <title>BBC News - Home</title>  
    <link>http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
    <description>The latest stories from the Home section of the BBC News web site.</description>  
    <language>en-gb</language>  
    <lastBuildDate>Tue, 29 Apr 2014 14:07:47 GMT</lastBuildDate>  
    <copyright>Copyright: (C) British Broadcasting Corporation, see http://news.bbc.co.uk/2/hi/help/rss/4498287.stm for terms and conditions of reuse.</copyright>  
    <image> 
      <url>http://news.bbcimg.co.uk/nol/shared/img/bbc_news_120x60.gif</url>  
      <title>BBC News - Home</title>  
      <link>http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
      <width>120</width>  
      <height>60</height> 
    </image>  
    <ttl>15</ttl>  
    <atom:link href="http://feeds.bbci.co.uk/news/rss.xml" rel="self" type="application/rss+xml"/>  
    <item> 
      <title>Stabbed teacher was due to retire</title>  
      <description>A teacher stabbed to death in front of her pupils at a Leeds school is described by the head teacher as "inspirational", as it emerges she was to retire this year.</description>  
      <link>http://www.bbc.co.uk/news/uk-england-27201812#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
      <guid isPermaLink="false">http://www.bbc.co.uk/news/uk-england-27201812</guid>  
      <pubDate>Tue, 29 Apr 2014 14:11:45 GMT</pubDate>  
      <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/74515000/jpg/_74515871_74499402.jpg"/>  
      <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/74515000/jpg/_74515872_74499402.jpg"/> 
    </item>  
    <item> 
      <title>UK economy grows by 0.8% in quarter</title>  
      <description>UK economy grows 0.8% in first quarter of 2014, the fifth consecutive quarter of growth, according to the ONS.</description>  
      <link>http://www.bbc.co.uk/news/business-27203433#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
      <guid isPermaLink="false">http://www.bbc.co.uk/news/business-27203433</guid>  
      <pubDate>Tue, 29 Apr 2014 11:26:59 GMT</pubDate>  
      <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/74508000/jpg/_74508863_brompton1.g.jpg"/>  
      <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/74508000/jpg/_74508864_brompton1.g.jpg"/> 
    </item>  
    <item> 
      <title>Rebels seize regional Ukraine HQ</title>  
      <description>A large crowd of pro-Russian activists storms the regional administration's headquarters in the eastern Ukrainian city of Luhansk.</description>  
      <link>http://www.bbc.co.uk/news/world-europe-27206280#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
      <guid isPermaLink="false">http://www.bbc.co.uk/news/world-europe-27206280</guid>  
      <pubDate>Tue, 29 Apr 2014 13:53:23 GMT</pubDate>  
      <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/74518000/jpg/_74518092_022076576-1.jpg"/>  
      <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/74518000/jpg/_74518093_022076576-1.jpg"/> 
    </item>  
    <item> 
      <title>Steel 'quizzed Smith on sex claims'</title>  
      <description>Lord Steel says he challenged Cyril Smith about allegations he abused boys after they appeared in Private Eye in 1979.</description>  
      <link>http://www.bbc.co.uk/news/uk-politics-27203110#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
      <guid isPermaLink="false">http://www.bbc.co.uk/news/uk-politics-27203110</guid>  
      <pubDate>Tue, 29 Apr 2014 13:28:10 GMT</pubDate>  
      <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/74515000/jpg/_74515070_74509905.jpg"/>  
      <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/74515000/jpg/_74515071_74509905.jpg"/> 
    </item>  
  </channel> 
</rss>

并将修正后的样式表应用于其中:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
<xsl:element name="root">
<xsl:apply-templates select="rss/channel/item/pubDate"/>
</xsl:element>
</xsl:template>

<xsl:template match="rss/channel/item/pubDate">
<xsl:element name="rawr" >
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

你应该得到以下结果:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <rawr>Tue, 29 Apr 2014 14:11:45 GMT</rawr>
   <rawr>Tue, 29 Apr 2014 11:26:59 GMT</rawr>
   <rawr>Tue, 29 Apr 2014 13:53:23 GMT</rawr>
   <rawr>Tue, 29 Apr 2014 13:28:10 GMT</rawr>
</root>

即使使用原始样式表,也应该收到错误消息 - 请检查实现转换的方式。