Fabien SCHWOB wrote:
Just as Paul told you. I was actually wrong, you can't register a default namespace prefix for XPath expressions. Sorry.
If you try to do this:
.>>> root = XML('<a xmlns="uri:test"><b/></a>') .>>> root.xpath("//b", {None : "uri:test"})
you will receive a TypeError. That is somewhat unfortunate, but due to libxml2. I can't see a way to define the default namespace for an XPath expression in libxml2. This behaviour now also has a test case, since it's unlikely to change in the future. Thanks for pointing me at it.
Anyway, just go with a non-empty prefix.
The problem is that in the application I'm developing, I got the xpath expression from the users, and I can't them to change their expressions to add namespace.
It's sad, but I think that for this project I will use ruby. I will gave a try more deeply to Python for some personal projects.
If Ruby's XML library implements XPath 1.0 then you'll have the same issue with Ruby. lxml in this respect follows the XPath standard (and any libxml2-based library will do so unless it takes special measures to break XPath 1.0 compliance). If you really do not want this behavior, you might be able to get away with preprocessing the XML itself to rip off all namespaces. Then the (non-XPath 1.0 compliant) expressions you desire will work. As Kasimier mentioned in his followup, XPath 2.0 may do what you desire. I do not know if libxml2 will implement XPath 2.0 - if so it's certainly not anywhere close, I suspect. Regards, Martijn