gpt4 book ai didi

python - 如何从 lxml 树中剥离命名空间?

转载 作者:行者123 更新时间:2023-12-04 16:52:11 24 4
gpt4 key购买 nike

Removing child elements in XML using python ...

感谢@Tichodroma,我有这个代码:

如果您可以使用 lxml , 试试这个:

 import lxml.etree

tree = lxml.etree.parse("leg.xml")
for dog in tree.xpath("//Leg1:Dog",
namespaces={"Leg1": "http://what.not"}):
parent = dog.xpath("..")[0]
parent.remove(dog)
parent.text = None
tree.write("leg.out.xml")

现在 leg.out.xml看起来像这样:
 <?xml version="1.0"?>
<Leg1:MOR xmlns:Leg1="http://what.not" oCount="7">
<Leg1:Order>
<Leg1:CTemp id="FO">
<Leg1:Group bNum="001" cCount="4"/>
<Leg1:Group bNum="002" cCount="4"/>
</Leg1:CTemp>
<Leg1:CTemp id="GO">
<Leg1:Group bNum="001" cCount="4"/>
<Leg1:Group bNum="002" cCount="4"/>
</Leg1:CTemp>
</Leg1:Order>
</Leg1:MOR>

如何修改我的代码以删除 Leg1:来自所有元素标签名称的命名空间前缀?

最佳答案

从每个元素中删除命名空间前缀的一种可能方法:

def strip_ns_prefix(tree):
#iterate through only element nodes (skip comment node, text node, etc) :
for element in tree.xpath('descendant-or-self::*'):
#if element has prefix...
if element.prefix:
#replace element name with its local name
element.tag = etree.QName(element).localname
return tree

另一个版本在 xpath 中进行命名空间检查而不是使用 if陈述 :
def strip_ns_prefix(tree):
#xpath query for selecting all element nodes in namespace
query = "descendant-or-self::*[namespace-uri()!='']"
#for each element returned by the above xpath query...
for element in tree.xpath(query):
#replace element name with its local name
element.tag = etree.QName(element).localname
return tree

关于python - 如何从 lxml 树中剥离命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46431446/

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