当子元素作为父元素位于不同的命名空间时,我试图在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) )
我也试过这种方法:
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) )
在这两种情况下,我得到这样的东西:
虽然这是正确的,但我希望它不那么冗长 – 这可能成为大数据集的真正问题(特别是因为我使用比’urn:test’更大的NS).
如果我可以将’country’放在“urn:test”命名空间内,并像这样声明它(在上面的第一个例子中):
country = etree.Element("{test}country")
然后我得到以下输出:
但我真正想要的是:
有任何想法吗?
最佳答案
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)
上面将给出(没有注册任何命名空间):