Undefined namespace prefix in Nokogiri and XPath

11,033

I'm not sure why, but it seems that you have to drop the namespace prefix to get the node:

xmlfeed.at_xpath("//totalresults")

Also note that I added the double forward slash, which scopes the search over the whole document (it won't work without it).

UPDATE:

Based on this answer: How do I get Nokogiri to understand my namespaces? I'd guess that the namespace (openSearch:totalResults) is not correctly declared as an attribute on the root node of the document, and hence Nokogiri is just ignoring it, which is why the selector above works but the namespaced one doesn't.

Share:
11,033
Иван Бишевац
Author by

Иван Бишевац

Software engineer

Updated on June 09, 2022

Comments

  • Иван Бишевац
    Иван Бишевац almost 2 years

    I am trying to parse Youtube Gdata to see if video with given id exists. But there isn't normal tag but with namespace. On the link http://gdata.youtube.com/feeds/api/videos?q=KgfdlZuVz7I there is tag:

    <openSearch:totalResults>1</openSearch:totalResults>
    

    There is namespace openSearch:

    xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
    

    but I dont know how to deal with it in Nokogiri and Ruby.

    Here is part of code:

    xmlfeed = Nokogiri::HTML(open("http://gdata.youtube.com/feeds/api/videos?q=#{video_id}"))
    xmlfeed.at_xpath("openSearch:totalResults")
    

    It gives error:

    Undefined namespace prefix: openSearch:totalResults