-
-
Notifications
You must be signed in to change notification settings - Fork 904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
XML::Document#xpath inconsistent use of assumed namespaces #357
Comments
The problem with this code isn't about implicit namespaces, it's about the query's path. Here's some code:
If the query doesn't start with any explicit context (e.g., "/", "//" or ".") then a search on a node will implicitly search from the context node. A query on a document without an explicit context is simply a failure to write a good xpath query. You might ask, "why doesn't an xpath query on a document use the root node as its context node?" Let me get back to you on that. |
Aha! You're absolutely right, thanks, sorry for the false bug. Namespaces are fine, rather I got confused about xpath on document not using the root node as context (which I'm not really sure if it should or not, but if it's not the developer like me has to realize that). So, wait, any xpath query on document without an explicit context (in fact, any xpath query on document that doesn't begin //, I think) will always return the empty set, right? In that case, yeah, it seems like it might as well use the #root as the context node. Or raise an exception for any xpath that doesn't begin //. But yeah, nothing to do with namespaces. |
I'm investigating the "implicit context" for documents. Similar problem exists for fragments. I'll probably open a new issue for it, and close this one. Will let you know. |
Hmm, more complicated. It's not true, as I suggested before, that ANY query without a context on document will return empty set. There is ONE query that won't -- on the root element itself. doc.xpath("root") => works So, I dunno, I honestly have no idea what the least confusing behavior to the developer is here, if the implicit context were changed that query would stop working, breaking some backwards compatibility for anyone depending on it. I do know this isn't the first time I've gotten confused about this, yet failed to remember what I learned from the last time(s) I got confused about it, and i'm probably not alone, but breaking backwards compatibility is probably bad. I dunno. |
OK, after writing some tests to characterize current behavior, I believe that Nokogiri is doing the right thing. From a document, we need to be able to access the root node, so:
needs to return the root node. But the same XPath query from the root node:
should not return the root node. Therefore, the document context is different from the root node's context. I'm sure you have questions. Fire away. |
Nope, no questions actually, I think you are quite right. Thanks, sorry for the mistaken bug report. |
Report as script, as requested. Ooh, ruby code doesn't paste in to a ticket well, gist instead:
http://gist.github.com/647802
The text was updated successfully, but these errors were encountered: