Hpricot and Namespaces/XPath
Until today we were happy users of Hpricot (0.8.1) . Easy to use, nice API, fast (enough)… but today we had to move a project over to Nokogiri.Why? Hpricot does not support XPath with namespaces. Worse: it does not even complain when you use a namespaced XPath query. It just keeps telling you that your XPath ended up in Nirvana and that there is no result.“Hey pal, you are using namespaces in XPath and i do not understand this” would have been a nice message.Since Hpricot does properly parse the namespaces, you can work around the problem using the at (aliased as %) method. But we need full XPath support hence the switch to Nokogiri.Some links i came across while researching the issue:
- http://enfranchisedmind.com/blog/posts/hpricot-does-namespaces
- http://www.rubyinside.com/nokogiri-ruby-html-parser-and-xml-parser-1288.html

September 4th, 2009 at 19:50
Is it just failing to work with namespaces properly, or does it ignore prefixes completely?
September 9th, 2009 at 14:29
Hi Robert
It just did not work, the searches with XPath just did not return any result if the query contained the namespace more than once.
I.e.:
‘/EAD:ead/EAD:eadheader/EAD:filedesc/EAD:titlestmt/EAD:titleproper’
Would return nothing while
‘/EAD:ead/EAD:eadheader’ did not return a single result.
BTW:
path = (hpricot / ‘EAD:titleproper’).first.xpath
nodes = hpricot / path
-> nodes is empty