user1598696
user1598696

Reputation: 560

How to fetch picture url from rss feed with xpath/xquery

Here is an example of rss feed:

<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"/>

I need to fetch url of .jpg pictures in media:thumbnail.. How can i do that with xpath or xquery? Can anyone help me please? Thank you!

Upvotes: 2

Views: 1033

Answers (1)

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Assuming your XML is:

<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 selects those nodes you want. In this case you need to pass namespace and prefix in your XPath engine.
Alternatively you can use this XPath:

//item/*[local-name() = 'thumbnail' 
    and namespace-uri() = 'http://search.yahoo.com/mrss/']/@url

Upvotes: 5

Related Questions