更紧凑的ElementTree或lxml命名空间

Sha*_*son 5 python xml lxml elementtree

当子元素作为父元素位于不同的命名空间时,我试图在ElementTree或lxml中获得命名空间的紧凑表示.这是基本的例子:

from lxml import etree

country = etree.Element("country")

name = etree.SubElement(country, "{urn:test}name")
name.text = "Canada"
population = etree.SubElement(country, "{urn:test}population")
population.text = "34M"
etree.register_namespace('tst', 'urn:test')

print( etree.tostring(country, pretty_print=True) )
Run Code Online (Sandbox Code Playgroud)

我也试过这种方法:

ns = {"test" : "urn:test"}

country = etree.Element("country", nsmap=ns)

name = etree.SubElement(country, "{test}name")
name.text = "Canada"
population = etree.SubElement(country, "{test}population")
population.text = "34M"

print( etree.tostring(country, pretty_print=True) )
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,我得到这样的东西:

<country>
    <ns0:name xmlns:ns0="urn:test">Canada</ns0:name>
    <ns1:population xmlns:ns1="urn:test">34M</ns1:population>
</country>
Run Code Online (Sandbox Code Playgroud)

虽然这是正确的,但我希望它不那么冗长 - 这可能成为大数据集的真正问题(特别是因为我使用比'urn:test'更大的NS).

如果我可以将'country'放在"urn:test"命名空间内,并像这样声明它(在上面的第一个例子中):

country = etree.Element("{test}country")
Run Code Online (Sandbox Code Playgroud)

然后我得到以下输出:

<ns0:country xmlns:ns0="urn:test">
    <ns0:name>Canada</ns0:name>
    <ns0:population>34M</ns0:population>
</ns0:country>
Run Code Online (Sandbox Code Playgroud)

但我真正想要的是:

<country xmlns:ns0="urn:test">
    <ns0:name>Canada</ns0:name>
    <ns0:population>34M</ns0:population>
<country>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

nam*_*mit 1

from xml.etree import cElementTree as ET
##ET.register_namespace('tst', 'urn:test')
country = ET.Element("country")
name = ET.SubElement(country, "{urn:test}name")
name.text = "Canada"
population = ET.SubElement(country, "{urn:test}population")
population.text = "34M"
print prettify(country)
Run Code Online (Sandbox Code Playgroud)

上面将给出(不注册任何名称空间):

<?xml version="1.0" ?>
<country xmlns:ns0="urn:test">
  <ns0:name>Canada</ns0:name>
  <ns0:population>34M</ns0:population>
</country>
Run Code Online (Sandbox Code Playgroud)

而且,当我删除注释部分时,它将给出::

<?xml version="1.0" ?>
<country xmlns:tst="urn:test">
  <tst:name>Canada</tst:name>
  <tst:population>34M</tst:population>
</country>
Run Code Online (Sandbox Code Playgroud)

注意:prettify函数在这里