如何使用lxml&amp ;;检索单个查询中的所有子节点XPATH

时间:2015-03-27 05:36:33

标签: python xml xpath lxml

这是我的xml数据

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

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

使用lxml&amp;蟒

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']]

什么是准确的xpath查询/查询/循环组合才能获得上述内容?

2 个答案:

答案 0 :(得分:6)

这是一种可能的方式:

.......
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']]

答案 1 :(得分:2)

列表理解解决方案:

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']]