开发者

Reading Elements by attribute Condition lxml

开发者 https://www.devze.com 2023-04-05 10:12 出处:网络
Using lxml libarary how can I read element value if an attribute is given. For example if \"qtn\" state=2 then return value required is \"Doctor Checkup\"

Using lxml libarary how can I read element value if an attribute is given. For example if "qtn" state=2 then return value required is "Doctor Checkup"

Thanks. If possible please also give me any tutorial/url for the same

<?xml version="1.0" encoding="UTF-8"?>
<elements>
<qtn state="1">I have 开发者_JS百科severe chest pain
    <an state="2" res="a">Check Up</an>
</qtn>
<qtn state="2">Doctor checkup
    <an state="9" res="a">Recovered</an>
    <an state="3" res="b">Blood Test</an>
    <an state="8" res="c">Untreatable</an>
</qtn>


I got my answer

from lxml import etree
xml = etree.parse(open('1.xml'))
xml.xpath("//qtn[@state="+state+"]/text()")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号