gpt4 book ai didi

python - 更改 lxml 中子元素的默认命名空间

转载 作者:太空宇宙 更新时间:2023-11-04 01:28:22 24 4
gpt4 key购买 nike

我想用 lxml 生成这个 xml:

<aroot xmlns="http://a/">
<broot xmlns="http://b/" xmlns:a="http://a/">
<child1/>
<child2/>
<a:smalltag1/>
<a:smalltag2/>
</broot>
</aroot>

但是下面的代码(对于这个输出似乎是正确的)并没有生成上面的 xml。

from lxml import etree
from lxml.builder import ElementMaker

NS_A = 'http://a/'
NS_B = 'http://b/'

A = ElementMaker(namespace=NS_A, nsmap={None: NS_A, 'b': NS_B})
B = ElementMaker(namespace=NS_B, nsmap={None: NS_B, 'a': NS_A})

elem = A.aroot(
B.broot(
B.child1,
B.child2,
A.smalltag1,
A.smalltag2,
),
)

print(etree.tostring(elem, pretty_print=True).decode('ascii'))

这会产生:

<aroot xmlns:b="http://b/" xmlns="http://a/">
<b:broot>
<b:child1/>
<b:child2/>
<smalltag1/>
<smalltag1/>
</b:broot>
</aroot>

这是一个有效的 xml,但我无法更改子元素 broot 上的默认命名空间。

如果我按如下方式更改 A ElementMaker:

A = ElementMaker(namespace=NS_A, nsmap={None: NS_A})

输出变为:

<aroot xmlns="http://a/">
<broot xmlns="http://b/">
<child1/>
<child2/>
<smalltag1/>
<smalltag2/>
</broot>
</aroot>

这是一个无效的 xml(smalltag1 的命名空间现在是 b)

如果我按如下方式更改 AB:

A = ElementMaker(namespace=NS_A, nsmap={None: NS_A})
B = ElementMaker(namespace=NS_B, nsmap={None: NS_B})

输出是:

<aroot xmlns="http://a/">
<broot xmlns="http://b/">
<child1/>
<child2/>
<smalltag1 xmlns="http://a/"/>
<smalltag2 xmlns="http://a/"/>
</broot>
</aroot>

这是有效的,但不是期望的输出。

最佳答案

使用etree:

from lxml import etree

NS_A = 'http://a/'
NS_B = 'http://b/'

aroot = Element('{%s}aroot' % (NS_A), nsmap={None: NS_A})
broot = etree.SubElement(aroot, '{%s}broot' % (NS_B), nsmap={None: NS_B, 'a': NS_A})
etree.SubElement(broot, '{%s}child1' % (NS_B))
etree.SubElement(broot, '{%s}child2' % (NS_B))
etree.SubElement(broot, '{%s}smalltag1' % (NS_A))
etree.SubElement(broot, '{%s}smalltag2' % (NS_A))

print etree.tostring(aroot, pretty_print=True)

你得到:

<aroot xmlns="http://a/">
<broot xmlns:a="http://a/" xmlns="http://b/">
<child1/>
<child2/>
<a:smalltag1/>
<a:smalltag2/>
</broot>
</aroot>

关于python - 更改 lxml 中子元素的默认命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15894591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com