如何解决python xml解析中的Xpath运算符问题

时间:2019-07-13 03:04:38

标签: xml python-3.x xpath xml-parsing

我正在寻找maxtemp大于0的日子。但是,以下代码给了我一个空列表。

我尝试在w3schools xpath示例中使用一个示例,在该示例中可以找到定价超过35本书的标题。

示例:     / bookstore / book [price> 35] /标题

xml:

<bookstore>

<book category="cooking">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="children">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="web">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="web">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

</bookstore>

这是我要使用的代码和xml文件:

import xml.etree.ElementTree as ET 
tree = ET.parse('SaintJohn.xml')
root = tree.getroot()
list=root.findall('.//stationdata[maxtemp>0]/maxtemp')
for i in list:
    print(i.text)

这是XML文件:

<climate>
<stationdata day="1" month="1" year="2019">
<maxtemp description="Maximum Temperature" 
units="°C">1.9</maxtemp><mintemp description="Minimum Temperature" 
units="°C">-10.6</mintemp><meantemp description="Mean Temperature" 
units="°C">-4.4</meantemp><heatdegdays description="Heating Degree 
Days" units="°C">22.4</heatdegdays>
</stationdata>
<stationdata day="2" month="1" year="2019"><maxtemp 
description="Maximum Temperature" units="°C">-10.6</maxtemp> 
<mintemp description="Minimum Temperature" 
units="°C">-22.4</mintemp><meantemp description="Mean Temperature" 
units="°C">-16.5</meantemp><heatdegdays description="Heating 
Degree Days" units="°C">34.5</heatdegdays></stationdata>
</climate>

我只想要列表中的一项。

1 个答案:

答案 0 :(得分:0)

这可以使用lxml完成​​:

temp = [your xml above]

from lxml import html
root = html.fromstring(temp)

list = root.xpath('.//stationdata/maxtemp[text()>0]')
for i in list:
    print(i.text)

输出:

  

1.9