0

A normal query to the Youtube gdata API for a given video like this:

http://gdata.youtube.com/feeds/api/videos?q=H-x7UumBaFs

Returns a result like this:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
<!-- data -->
<entry>
<!-- data -->
<gd:rating average="3.2309198" max="5" min="1" numRaters="511" rel="http://schemas.google.com/g/2005#overall"/>
</entry>
</feed>

However sometimes the gd:rating line is not present.

I would like to read the data from this line if it exists. I tried to check for this with:

if node.at_xpath(".//gd:rating/@numRaters")

But this doesn't work, giving the error "Undefined namespace prefix: .//gd:rating/@numRaters"

Do you know how I can check for the existence of the gd:rating namespace before reading from it?

4

1 に答える 1

0

この質問Nokogiri docsのおかげで、これは私にとってうまくいった解決策です:

gd_namespace = {"xmlns:gd" => "http://schemas.google.com/g/2005"}

...

s.num_raters = node.at_xpath('.//gd:rating/@numRaters', gd_namespace).try(:text)
于 2012-09-26T19:12:54.160 に答える