[objectify] namespace handling in child element attribute access
![](https://secure.gravatar.com/avatar/bc4259003fffe3e1cb3543db975bea0d.jpg?s=120&d=mm&r=g)
Hi, just noticed that namespace handling in child element access does not work correctly (any more) if the parent element is in no-namespace:
from lxml import etree, objectify print "etree.__version__:", etree.__version__ etree.__version__: 2.3.5 print etree.LIBXML_VERSION (2, 7, 8) objectify.enable_recursive_str() root = objectify.fromstring(""" ... <root> ... <foo:x xmlns:foo="/foo/bar">1</foo:x> ... <x>2</x> ... </root>""") print root root = None [ObjectifiedElement] {/foo/bar}x = 1 [IntElement] x = 2 [IntElement] print "root.x:", root.x root.x: 1 assert root.x == 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> AssertionError
As root lives in no-namespace root.x should return the second, no-namespace x child element. This worked correctly up to ~2.0 (based on libxml 2.6.27) and stopped working for ~2.1.5 (libxml 2.6.32), judging from the versions I have tried. Funny that we never noticed it up to now. Not too much insane namespace mixing going on here, obviously ;-)
from lxml import etree, objectify print "etree.__version__:", etree.__version__ etree.__version__: 2.0.alpha4 print etree.LIBXML_VERSION (2, 6, 27) objectify.enableRecursiveStr() root = objectify.fromstring(""" ... <root> ... <foo:x xmlns:foo="/foo/bar">1</foo:x> ... <x>2</x> ... </root>""") print root root = None [ObjectifiedElement] {/foo/bar}x = 1 [IntElement] x = 2 [IntElement] print "root.x:", root.x root.x: 2 assert root.x == 2
Btw this works correctly if the parent element is in a namespace, which is the case the documentation shows (and doctests). Holger Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart
![](https://secure.gravatar.com/avatar/8b97b5aad24c30e4a1357b38cc39aeaa.jpg?s=120&d=mm&r=g)
Holger Joukl, 18.12.2012 18:04:
just noticed that namespace handling in child element access does not work correctly (any more) if the parent element is in no-namespace:
from lxml import etree, objectify print "etree.__version__:", etree.__version__ etree.__version__: 2.3.5 print etree.LIBXML_VERSION (2, 7, 8) objectify.enable_recursive_str() root = objectify.fromstring(""" ... <root> ... <foo:x xmlns:foo="/foo/bar">1</foo:x> ... <x>2</x> ... </root>""") print root root = None [ObjectifiedElement] {/foo/bar}x = 1 [IntElement] x = 2 [IntElement] print "root.x:", root.x root.x: 1 assert root.x == 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> AssertionError
As root lives in no-namespace root.x should return the second, no-namespace x child element.
Thanks for the report and the test case, here's a fix: https://github.com/lxml/lxml/commit/c0858cd3ea4a9d1bef6418a45f9841e3f1154641 It sets the requested namespace to the empty string instead of NULL to indicate that only the empty namespace should be matched and not any namespace.
This worked correctly up to ~2.0 (based on libxml 2.6.27) and stopped working for ~2.1.5 (libxml 2.6.32), judging from the versions I have tried. Funny that we never noticed it up to now. Not too much insane namespace mixing going on here, obviously ;-)
Yes, quite impressive. I guess it's just a really rare case that namespaced tags are being mixed with namespaced tags in the same document. Given how long this went undetected, it won't pass as a simple bug fix anymore, so I'll keep it for lxml 3.1. Stefan
![](https://secure.gravatar.com/avatar/bc4259003fffe3e1cb3543db975bea0d.jpg?s=120&d=mm&r=g)
Hi, lxml-bounces@lxml.de schrieb am 18.12.2012 23:50:10:
Thanks for the report and the test case, here's a fix:
https://github.com/lxml/lxml/commit/c0858cd3ea4a9d1bef6418a45f9841e3f1154641
It sets the requested namespace to the empty string instead of NULL to indicate that only the empty namespace should be matched and not
anynamespace. Thanks, lightning-quick as ever.
Given how long this went undetected, it won't pass as a simple bug fix anymore, so I'll keep it for lxml 3.1.
Fine. If I ever needed this earlier I wouldn't hesitate to use a devel lxml, anyway. Holger Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart
participants (2)
-
Holger Joukl
-
Stefan Behnel