Python 如何强制将所有命名空间声明附加到根元素?

Python 如何强制将所有命名空间声明附加到根元素?,python,xml,namespaces,lxml,Python,Xml,Namespaces,Lxml,我正在使用lxml生成一个xml文件 from lxml import etree as ET 我使用这一行注册一个名称空间 ET.register_namespace("exp", "http://www.example.com/exp/") 如果我添加一个元素 root_exp = ET.Element("{http://www.example.com/exp/}root_exp") 或具有 foo_hdr = ET.SubElement(root_exp, "{http://www.

我正在使用lxml生成一个xml文件

from lxml import etree as ET
我使用这一行注册一个名称空间

ET.register_namespace("exp", "http://www.example.com/exp/")
如果我添加一个元素

root_exp = ET.Element("{http://www.example.com/exp/}root_exp")
或具有

foo_hdr = ET.SubElement(root_exp, "{http://www.example.com/exp/}fooHdr") 
每次出现名称空间时都会定义名称空间,例如

<exp:bar xmlns:exp="http://www.example.com/exp/">
   <exp:fooHdr CREATEDATE="2013-03-22T10:28:27.137531">
更新2

更新的代码片段

#!/usr/bin/env python2
from lxml import etree as ET

ET.register_namespace("exa", "http://www.example.com/test")
ET.register_namespace("axx", "http://www.example.com/foo")

root = ET.Element("{http://www.example.com/test}root")
sub_element = ET.SubElement(root, "{http://www.example.com/test}sub_element")
foo_element = ET.SubElement(sub_element, "{http://www.example.com/foo}foo")
bar_element = ET.SubElement(sub_element, "{http://www.example.com/foo}bar")

tree = ET.ElementTree(root)
tree.write("example.xml",  encoding="UTF-8", pretty_print=True, xml_declaration=True)
预期:

<?xml version="1.0" encoding="UTF-8"?>
<exa:root xmlns:exa="http://www.example.com/test"/ xmlns:axx="http://www.example.com/foo">
  <exa:sub_element>
    <axx:foo />
    <axx:bar />
  </exa:sub_element>
</exa:root>

是:


使用名称空间映射:

NSMAP = { 'exa': 'http://www.example.com/test',
          'axx': 'http://www.example.com/foo' }

root = ET.Element('{http://www.example.com/test}root', nsmap=NSMAP)
sub_element = ET.SubElement(root, '{http://www.example.com/test}sub_element')
foo_element = ET.SubElement(sub_element, '{http://www.example.com/foo}foo')
bar_element = ET.SubElement(sub_element, '{http://www.example.com/foo}bar')

tree = ET.ElementTree(root)

print(ET.tostring(tree,encoding='UTF-8',pretty_print=True,xml_declaration=True))
结果:

<?xml version='1.0' encoding='UTF-8'?>
<exa:root xmlns:axx="http://www.example.com/foo" xmlns:exa="http://www.examplom/test">
  <exa:sub_element>
    <axx:foo/>
    <axx:bar/>
  </exa:sub_element>
</exa:root>


这正是所需的输出。

谢谢!它起作用了!我猜想,
register\u namespace
在某种程度上做到了这一点。
NSMAP = { 'exa': 'http://www.example.com/test',
          'axx': 'http://www.example.com/foo' }

root = ET.Element('{http://www.example.com/test}root', nsmap=NSMAP)
sub_element = ET.SubElement(root, '{http://www.example.com/test}sub_element')
foo_element = ET.SubElement(sub_element, '{http://www.example.com/foo}foo')
bar_element = ET.SubElement(sub_element, '{http://www.example.com/foo}bar')

tree = ET.ElementTree(root)

print(ET.tostring(tree,encoding='UTF-8',pretty_print=True,xml_declaration=True))
<?xml version='1.0' encoding='UTF-8'?>
<exa:root xmlns:axx="http://www.example.com/foo" xmlns:exa="http://www.examplom/test">
  <exa:sub_element>
    <axx:foo/>
    <axx:bar/>
  </exa:sub_element>
</exa:root>