如何使用 lxml 和 XPATH 在单个查询中检索所有子节点

How to retrieve all child nodes in a single query using lxml & XPATH

这是我的xml数据

<location>
   <city>
      <name> New York</name>
      <type>non-capital</type>
   </city>

   <city>
        <name> London</name>
        <type>capital</type>
   </city>
</location>

使用 lxml & python

from lxml import etree as ET

parser = ET.XMLParser(recover=True)

tree = ET.fromstring(xml_data,parser)
print(tree.xpath('//city//name/text() | //city//type/text()'))

上面的代码有效,但我想要嵌套数组描述为 [['New York','non-capital'],['London','capital']]

queries/loops 的准确 xpath query/combination 是什么才能得到上述内容?

这是一种可能的方式:

.......
result = []
for city in tree.xpath('//city'):
    result.append([city.find('name').text, city.find('type').text])

print(result)
# output :
#[[' New York', 'non-capital'], [' London', 'capital']]

列表理解解决方案:

xml_data='''<location>
   <city>
      <name> New York</name>
      <type>non-capital</type>
   </city>
   <city>
        <name> London</name>
        <type>capital</type>
   </city>
</location>'''

from lxml import etree as ET

parser = ET.XMLParser(recover=True)

tree = ET.fromstring(xml_data,parser)
print(tree.xpath('//city'))


cities = [[c.text for c in n if c.tail] for n in tree.xpath('//city')]

结果:

[[' New York', 'non-capital'], [' London', 'capital']]