Improve XPath namespace support
Created by: leejarvis
The README demonstrates somewhat cumbersome namespace support. I'm wondering if there are any plans to improve the XPath API to increase the fluidity of parsing namespace heavy XML documents. You mention in the README:
In the future I might add an API to ease this process, although at this time I have little interest in providing an API similar to Nokogiri.
Which is a fair comment. I'm wondering if you had an alternative API in mind? I think the Nokogiri API is quite nice, and the only alternative I can think of is to dynamically update a list of supported namespace within the document itself, which I don't really think is any nicer.
Example
Take the following (contrived) XML:
<main:library xmlns="http://example.com" xmlns:main="http://example.com/library">
<main:name>The library</main:name>
<bs:bookshelf xmlns:bs="http://example.com/author/lee">
<bs:name>Lee's books</bs:name>
</bs:bookshelf>
<bs:bookshelf xmlns:bs="http://example.com/author/yorick">
<bs:name>Yorick's books</bs:name>
</bs:bookshelf>
</main:library>
We're interested in all of the name
attributes. Here's how we might parse them in Nokogiri:
require "nokogiri"
doc = Nokogiri.XML(DATA.read)
# auto registered namespace aliases
p doc.at_xpath("//main:name").text #=> "The library"
namespaces = {
lee: "http://example.com/author/lee",
yorick: "http://example.com/author/yorick",
}
p doc.at_xpath("//lee:bookshelf/lee:name", namespaces).text #=> Lee's books
p doc.at_xpath("//yorick:bookshelf/yorick:name", namespaces).text #=> Yorick's books
Any alternative Oga implementation as suggested by the README would be non-trivial.
My suggestion would be to provide an alternative API to Nokogiri. Happy to discuss to any caveats around that idea.
Any thoughts?