首页 > 解决方案 > 在具有特定路径的 lxml 树中查找元素

问题描述

假设我有一个 XML 文件,如下所示:

my_data.xml

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <country name="Liechtenstein" xmlns="aaa:bbb:ccc:liechtenstein:eee">
    <rank updated="yes">2</rank>
    <holidays>
      <christmas>Yes</christmas>
    </holidays>
    <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>
  <ethnicity xmlns="aaa:bbb:ccc:ethnicity:eee">
    <malay>
      <holidays>
        <ramadan>Yes</ramadan>
      </holidays>
    </malay>
  </ethnicity>
</data>

解析后:

xtree = etree.parse('my_data.xml')
xroot = xtree.getroot()

我想搜索带有标签的元素holidays,但只能在ethnicity. 这一行:

holiday_nodes = xroot.xpath('.//*[local-name()="holidays"]')

会给我所有的假期节点,像这样:

[<Element {aaa:bbb:ccc:liechtenstein:eee}holidays at 0x19013f926c0>, 
<Element {aaa:bbb:ccc:singapore:eee}holidays at 0x19013f92880>, 
<Element {aaa:bbb:ccc:ethnicity:eee}holidays at 0x19012cdc0c0>]

实现此搜索的语法是什么?谢谢。

标签: pythonxmllxml

解决方案


尝试以下 xpath ...

.//*[local-name()="ethnicity"]//*[local-name()="holidays"]

推荐阅读