GNOME Bugzilla – Bug 775683
xpath context node is lost after nodeset merge
Last modified: 2018-11-30 20:00:54 UTC
Created attachment 341447 [details] Minimal example libxml2 loses the context node after joining nodesets from local and global "//". The sample program below prints two "bar" nodes for each "doc", but it should print only one. And it prints one "bar" node if I restore the context node (uncomment the line (2)) or simplify the expression on the line (1). import libxml2 s = """ <x> <doc> <foo /> <bar /> </doc> <doc> <foo /> <bar /> </doc> </x>""" xmldoc = libxml2.parseDoc(s) baton = xmldoc.xpathNewContext() res = baton.xpathEval("//doc") for doc_node in res: baton.setContextNode(doc_node) res = baton.xpathEval(".//foo | //foo") # (1) #baton.setContextNode(doc_node) # (2) res2 = baton.xpathEval(".//bar") # (3) print res2 baton.xpathFreeContext() xmldoc.freeDoc()
yes if you manipulate at that level, the input to a query on the evaluation context is both the XPath expression and the context node (c.f. XPath spec introduction) To be honnest I would be worried of changing the default behaviour at this point. There is always a risk of introducing a regression in software relying on some side effect. Daniel
For me, it looks very much like a bug, and I can't imagine how software can rely on re-setting the context node behind the scene. Anyway, I can believe that there are reasons not to correct the issue, but while such behavior is very unexpected, I'd recommend at least to document it.
*** Bug 648327 has been marked as a duplicate of this bug. ***
Should be fixed with: https://gitlab.gnome.org/GNOME/libxml2/commit/e22a83b1d095dac25ce05e1a2d9f263f41d11c68 https://gitlab.gnome.org/GNOME/libxml2/commit/938835e763277684274ac31afc08fc40fa419aae https://gitlab.gnome.org/GNOME/libxml2/commit/665df41dcc6c4c3a609907c979b6c16472593d0d