I have searched endlessly for a solution to this and I thought i had solved it when I got an image to display. However the thumbnail was only the one stored in the root element. Quite simply this works:
rss = Nokogiri::XML(open('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml'))
@news = rss.xpath('//item').map do |i|
{
'title' => i.xpath('title').text,
'link' => i.xpath('link').text,
'description' => i.xpath('description').text,
'thumbnail' => i.xpath('//media:thumbnail').attr('url')
}
end
But editing the media:thumbnail to reference that item seems to break it:
{
'title' => i.xpath('title').text,
'link' => i.xpath('link').text,
'description' => i.xpath('description').text,
'thumbnail' => i.xpath('media:thumbnail').attr('url')
}
I don’t understand why as both elements are identical. Any pointers in the right direction would be appreciated.
Thanks!
Your code breaks at the first element that doesn’t have a thumbnail child element. Try this:
Now
thumbnailwill either be the URL if it exists ornilif it doesn’t.