
Hi, I have an issue with serializing ElementTree to XML when I'm using namespaces: when namespace has a prefix everything works as expected: nsmap={'myns': 'default_namespace_uri'} root=etree.Element('{default_namespace_uri}elem', {'{default_namespace_uri}attrib':'xyz'}, nsmap=nsmap ) stream=StringIO() tree=etree.ElementTree(root) tree.write(stream) print stream.getvalue() result is <myns:elem xmlns:myns="default_namespace_uri" myns:attrib="xyz"/> However if I set the namespace as a default namespace like this: nsmap={None: 'default_namespace_uri'} root=etree.Element('{default_namespace_uri}elem', {'{default_namespace_uri}attrib':'xyz'}, nsmap=nsmap ) stream=StringIO() tree=etree.ElementTree(root) tree.write(stream) print stream.getvalue() result is then strange: <elem xmlns="default_namespace_uri" xmlns:ns0="default_namespace_uri" ns0:attrib="xyz"/> as you can see namespace is defined twice - first as default and second with prefix ns0, which is then used for attribute. I would expect that xml in this case should look like this: <elem xmlns="default_namespace_uri" attrib="xyz"/> This looks to me as a bug or am I missing something ? Regards Ivan

Hi,
I have an issue with serializing ElementTree to XML when I'm using namespaces:
when namespace has a prefix everything works as expected: [...]
However if I set the namespace as a default namespace like this:
nsmap={None: 'default_namespace_uri'} root=etree.Element('{default_namespace_uri}elem', {'{default_namespace_uri}attrib':'xyz'}, nsmap=nsmap ) stream=StringIO() tree=etree.ElementTree(root) tree.write(stream) print stream.getvalue()
result is then strange:
<elem xmlns="default_namespace_uri" xmlns:ns0="default_namespace_uri" ns0:attrib="xyz"/>
as you can see namespace is defined twice - first as default and second with prefix ns0, which is then used for attribute. I would expect that xml in this case should look like this: <elem xmlns="default_namespace_uri" attrib="xyz"/>
This looks to me as a bug or am I missing something ?
See http://www.w3.org/TR/REC-xml-names/#defaulting: "The namespace name for an unprefixed attribute name always has no value." Or as Wikipedia (http://en.wikipedia.org/wiki/XML_namespace#Namespace_declaration) puts it: "Attributes are never subject to the default namespace. An attribute without an explicit namespace prefix is considered not to be in any namespace." Holger Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart

Hi, thanks for the explanation, I suspected there is something substantial about it. Regards Ivan On 23/12/13 09:15, Holger Joukl wrote:
Hi,
I have an issue with serializing ElementTree to XML when I'm using namespaces:
when namespace has a prefix everything works as expected: [...]
However if I set the namespace as a default namespace like this:
nsmap={None: 'default_namespace_uri'} root=etree.Element('{default_namespace_uri}elem', {'{default_namespace_uri}attrib':'xyz'}, nsmap=nsmap ) stream=StringIO() tree=etree.ElementTree(root) tree.write(stream) print stream.getvalue()
result is then strange:
<elem xmlns="default_namespace_uri" xmlns:ns0="default_namespace_uri" ns0:attrib="xyz"/>
as you can see namespace is defined twice - first as default and second with prefix ns0, which is then used for attribute. I would expect that xml in this case should look like this: <elem xmlns="default_namespace_uri" attrib="xyz"/>
This looks to me as a bug or am I missing something ? See http://www.w3.org/TR/REC-xml-names/#defaulting: "The namespace name for an unprefixed attribute name always has no value."
Or as Wikipedia (http://en.wikipedia.org/wiki/XML_namespace#Namespace_declaration) puts it: "Attributes are never subject to the default namespace. An attribute without an explicit namespace prefix is considered not to be in any namespace."
Holger
Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart
_________________________________________________________________ Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de https://mailman-mail5.webfaction.com/listinfo/lxml
participants (2)
-
Holger Joukl
-
Ivan Zderadicka