对于此xml
,
xmldata="""
<locations>
<continent region="Africa" >
<country id="84" countryname="Algeria" ></country>
</continent>
<continent region="Asia" >
<country id="84" countryname="India" ></country>
</continent>
<continent region="America" >
<country id="84" countryname="Mexico" ></country>
</continent>
</locations>
"""
我正在尝试使用国家/地区名称获取地区名称,如果国家/地区名称为India
,则代码应返回Asia
。
我尝试了什么,
import lxml.etree as ET
root = ET.fromstring(xmldata)
flag=False
continent=""
country=""
for neighbor in root.iter('continent'):
for i in neighbor.iter("country"):
if i.attrib.get('countryname')=="India":
flag=True
continent=neighbor.attrib.get('region')
country=i.attrib.get('countryname')
if flag==True:
break
print 'Continent is ',continent , 'And country is ', country
此代码有效,但执行此操作并不方便。
如何使用xpath
表达式实现此目的?
答案 0 :(得分:1)
使用//continent[./country/@countryname="India"]/@region
xpath表达式:
>>> import lxml.etree as ET
>>> xmldata="""
... <locations>
...
... <continent region="Africa" >
... <country id="84" countryname="Algeria" ></country>
... </continent>
...
... <continent region="Asia" >
... <country id="84" countryname="India" ></country>
... </continent>
...
... <continent region="America" >
... <country id="84" countryname="Mexico" ></country>
... </continent>
...
... </locations>
... """
>>> root = ET.fromstring(xmldata)
>>> print root.xpath('//continent[./country/@countryname="India"]/@region')
['Asia']