Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Namespace issue with Nokogiri

Tags:

ruby

nokogiri

I have the following XML:

<body>
  <hello xmlns='http://...'>
     <world>yes</world>
  </hello>
</body>

When I load that into a Nokogiri XML document, and call document.at_css "world", I receive nil back. But when I remove the namespace for hello, it works perfectly. I know I can call document.remove_namespaces!, but why is it that it will not work with the namespace?

like image 833
TheDude Avatar asked Dec 20 '25 15:12

TheDude


1 Answers

Because Nokogiri requires you to register the XML namespaces you are querying within (read more about XML Namespaces). But you should still be able to query the element if you specify its namespace when calling at_css. To see the exact usage, check out the css method documentation. It should end up looking something like this:

document.at_css "world", 'namespace_name' => 'namespace URI'
like image 181
Stuart M Avatar answered Dec 23 '25 10:12

Stuart M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!