2

以下是 RSS 提要的示例:

<item>
<title>Stem cells being made from blood</title>
<description>A patient's own blood has been used to make personalised stem cells, which doctors hope can be used to treat a range of diseases.</description>
<link>http://www.bbc.co.uk/news/health-20539835#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa</link>
<guid isPermaLink="false">http://www.bbc.co.uk/news/health-20539835</guid>
<pubDate>Fri, 30 Nov 2012 00:45:53 GMT</pubDate>
<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" width="66" height="49" url="http://news.bbcimg.co.uk/media/images/64469000/jpg/_64469170_c0147104-red_blood_cells%2C_artwork-spl.jpg"/>
<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" width="144" height="81" url="http://news.bbcimg.co.uk/media/images/64469000/jpg/_64469171_c0147104-red_blood_cells%2C_artwork-spl.jpg"/>

我需要在 media:thumbnail 中获取 .jpg 图片的 url。我如何使用 xpath 或 xquery 来做到这一点?任何人都可以帮助我吗?谢谢!

4

1 回答 1

5

假设您的 XML 是:

<item>
  <title>Stem cells being made from blood</title>
  <description>A patient's own blood has been used to make personalised stem cells, which doctors hope can be used to treat a range of diseases.</description>
  <link>http://www.bbc.co.uk/news/health-20539835#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>
  <guid isPermaLink="false">http://www.bbc.co.uk/news/health-20539835</guid>
  <pubDate>Fri, 30 Nov 2012 00:45:53 GMT</pubDate>
  <media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" width="66" height="49" url="http://news.bbcimg.co.uk/media/images/64469000/jpg/_64469170_c0147104-red_blood_cells%2C_artwork-spl.jpg"/>
  <media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" width="144" height="81" url="http://news.bbcimg.co.uk/media/images/64469000/jpg/_64469171_c0147104-red_blood_cells%2C_artwork-spl.jpg"/>
</item>

XPath//item/media:thumbnail/@url选择您想要的那些节点。在这种情况下,您需要在 XPath 引擎中传递名称空间和前缀。
或者,您可以使用此 XPath:

//item/*[local-name() = 'thumbnail' 
    and namespace-uri() = 'http://search.yahoo.com/mrss/']/@url
于 2012-11-30T01:54:34.890 回答