python - 在 lxml 中查找具有未知名称空间的元素
问题描述
我有一个包含多个级别的 XML。每个级别都可以附加命名空间。我想要find
一个我知道其名称但不知道其名称空间的特定元素。例如:
my_file.xml
<?xml version="1.0" encoding="UTF-8"?>
<data xmlns="aaa:bbb:ccc:ddd:eee">
<country name="Liechtenstein" xmlns="aaa:bbb:ccc:liechtenstein:eee">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore" xmlns="aaa:bbb:ccc:singapore:eee">
<continent>Asia</continent>
<holidays>
<christmas>Yes</christmas>
</holidays>
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama" xmlns="aaa:bbb:ccc:panama:eee">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
import lxml.etree as etree
tree = etree.parse('my_file.xml')
root = tree.getroot()
cntry_node = root.find('.//country')
以上find
不返回任何内容cntry_node
。在我的真实数据中,层次比这个例子更深。lxml 文档讨论了命名空间。当我这样做时:
root.nsmap
我看到这个:
{None: 'aaa:bbb:ccc:ddd:eee'}
如果有人可以解释如何访问完整nsmap
和/或如何将其用于find
特定元素?非常感谢。
解决方案
您可以声明所有名称空间,但鉴于示例 xml 的结构,我认为您最好完全忽略名称空间而只使用local-name()
; 所以
cntry_node = root.xpath('.//*[local-name()="country"]')
cntry_node
返回
[<Element {aaa:bbb:ccc:liechtenstein:eee}country at 0x1cddf1d4680>,
<Element {aaa:bbb:ccc:singapore:eee}country at 0x1cddf1d47c0>,
<Element {aaa:bbb:ccc:panama:eee}country at 0x1cddf1d45c0>]
推荐阅读
- wordpress - 如何根据另一个字段选择允许重力形式中的重复项?
- python - gcsa - 谷歌日历 API 的 Pythonic 包装器
- flutter - 在 initState 上声明时 setState 不更新变量
- matlab - 将矩阵元素作为函数句柄传递
- mysql - r2dbc:使用 in 和 out 参数在 mysql 中调用存储过程
- javascript - 承诺解决返回未定义的值,但我已经解决返回
- react-leaflet - 未捕获的类型错误:无法读取未定义的属性“appendChild”
- java - 为什么我的正则表达式不起作用?[Java] [正则表达式] [空白问题]
- linux - 使用 awk,减去所有列中的前一行并打印结果
- python - 如何使用 tensorflow-gpu 获得子任务的多处理?