Python ElementTree default namespace?(Python ElementTree 默认命名空间?)
问题描述
有没有办法在 python ElementTree 中定义默认/无前缀命名空间?这似乎不起作用...
Is there a way to define the default/unprefixed namespace in python ElementTree? This doesn't seem to work...
ns = {"":"http://maven.apache.org/POM/4.0.0"}
pom = xml.etree.ElementTree.parse("pom.xml")
print(pom.findall("version", ns))
这也不是:
ns = {None:"http://maven.apache.org/POM/4.0.0"}
pom = xml.etree.ElementTree.parse("pom.xml")
print(pom.findall("version", ns))
确实如此,但我必须为每个元素添加前缀:
This does, but then I have to prefix every element:
ns = {"mvn":"http://maven.apache.org/POM/4.0.0"}
pom = xml.etree.ElementTree.parse("pom.xml")
print(pom.findall("mvn:version", ns))
在 OSX 上使用 Python 3.5.
Using Python 3.5 on OSX.
如果答案是否",您仍然可以获得赏金 :-).我只是想从一个花了很多时间使用它的人那里得到一个明确的不".
if the answer is "no", you can still get the bounty :-). I just want a definitive "no" from someone who's spent a lot of time using it.
推荐答案
注意:对于 Python 3.8+,请参阅 这个答案.
NOTE: for Python 3.8+ please see this answer.
没有直接的方法可以透明地处理默认命名空间.正如您已经提到的,为空命名空间分配一个非空名称是一种常见的解决方案:
There is no straight-forward way to handle the default namespaces transparently. Assigning the empty namespace a non-empty name is a common solution, as you've already mentioned:
ns = {"mvn":"http://maven.apache.org/POM/4.0.0"}
pom = xml.etree.ElementTree.parse("pom.xml")
print(pom.findall("mvn:version", ns))
请注意,lxml.etree
不允许显式使用空命名空间.你会得到:
Note that lxml.etree
does not allow the use of empty namespaces explicitly. You would get:
ValueError
:ElementPath 中不支持空的命名空间前缀
ValueError
: empty namespace prefix is not supported in ElementPath
您可以通过 在加载 XML 输入数据时删除默认命名空间定义:
import xml.etree.ElementTree as ET
import re
with open("pom.xml") as f:
xmlstring = f.read()
# Remove the default namespace definition (xmlns="http://some/namespace")
xmlstring = re.sub(r'sxmlns="[^"]+"', '', xmlstring, count=1)
pom = ET.fromstring(xmlstring)
print(pom.findall("version"))
这篇关于Python ElementTree 默认命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python ElementTree 默认命名空间?


- 我如何透明地重定向一个Python导入? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01