3 ms·
I wonder why libraries make it harder to combine XPath and namespaces. For example, in Nokogiri (libxml2 bindings for Ruby, and probably the de facto XML librar
by lobster_johnson 13y ago
I wonder why libraries make it harder to combine XPath and namespaces. For example, in Nokogiri (libxml2 bindings for Ruby, and probably the de facto XML library in Ruby), you have to do:
doc = Nokogiri::XML(stream)
doc.xpath("//x:foo", {x: "http://somenamespace/"}).each ...
I always end up declaring the namespaces in a constant and passing it in, because there is no way to specify your mappings globally. It should have been something like:
doc = Nokogiri::XML(stream)
doc.namespaces = {x: "http://somenamespace/"}
doc.xpath("//x:foo").each ...
or at least something functional like:
doc = Nokogiri::XML(stream)
doc.using_namespaces(x: "http://somenamespace/").
xpath("//x:foo").each ...
I have seen this way of integrating namespaces and XPath in several libraries, it's not just Nokogiri.