How do I get attribute/option values from XML with Nokogiri?
I need to extract the URL from this tag:
<media:content url="http://video.ted.com/talk/podcast/2011/None/MikeMatas_开发者_如何学C2011.mp4" fileSize="15533795" type="video/mp4" />
Currently I use this code but I only get nil values:
page_content = Nokogiri::XML(open("http://www.ted.com/talks/rss"))
page_content.xpath('//item').each {|item|
@url = course_hash[:videoUrl] = item.at_xpath('[media:content]')['url']
puts @url
}
The node you are trying to access has a media
namespace, so you'll need to take that into account when you try to locate it.
Generally we'd do something like:
require 'nokogiri'
xml = %q{
<xml xmlns:media="http://xml.my.org/file">
<media:content url="http://video.ted.com/talk/podcast/2011/None/MikeMatas_2011.mp4" fileSize="15533795" type="video/mp4" />
</xml>
}
doc = Nokogiri::XML(xml)
doc.search('//media:content', 'media' => 'http://xml.my.org/file').each do |n|
puts n['url']
end
# >> http://video.ted.com/talk/podcast/2011/None/MikeMatas_2011.mp4
Nokogiri will automatically register the namespace if it is defined in the <xml>
tag, meaning we could use a simpler form:
doc.search('//media:content').each do |n|
puts n['url']
end
# >> http://video.ted.com/talk/podcast/2011/None/MikeMatas_2011.mp4
Nokogiri also supports using CSS accessors with namespaces:
doc.search('media|content').each do |n|
puts n['url']
end
# >> http://video.ted.com/talk/podcast/2011/None/MikeMatas_2011.mp4
I think your xpath expression is messed up: try using item.at_xpath('media:content')['url']
instead.
精彩评论