[lxml-dev] namespace strangeness in lxml 1.1
Eric Jahn
eric at ejahn.net
Mon Jun 30 22:11:37 CEST 2008
Using python-lxml 1.1.1-1 which comes with Debian Etch, I am noticing
some odd behavior in how lxml handles attribute namespaces, and
namespaces in general. In the following code, all actual namespace urls
should be mapped to namespaces in the resulting output, and they are,
except if an attribute is involved:
type="{http://domain2.info}someattribute
Is this an lxml/libxml2 bug? The second odd behavior is that, for
subelements, all the namespace declarations are automatically redeclared
in the subelement tag, which is redundant.
Does anyone know how to get around these problems?
from lxml import etree
NS1_NAMESPACE = "http://domain1.info"
NS2_NAMESPACE = "http://domain2.info"
NS1 = "{%s}" % NS1_NAMESPACE
NS2 = "{%s}" % NS2_NAMESPACE
NSMAP = {"NS1" : NS1_NAMESPACE , "NS2" : NS2_NAMESPACE}
root = etree.Element(NS1 + "firstelement", nsmap=NSMAP)
element = etree.Element(NS2 + "secondelement", nsmap=NSMAP, type = NS2 +
"someattribute")
root.append(element)
print etree.tostring(root,pretty_print=True)
Which results in the following output:
<NS1:firstelement xmlns:NS1="http://domain1.info"
xmlns:NS2="http://domain2.info">
<NS2:secondelement xmlns:NS1="http://domain1.info"
xmlns:NS2="http://domain2.info"
type="{http://domain2.info}someattribute"/>
</NS1:firstelement>
More information about the lxml-dev
mailing list