父节点中的Xml搜索

时间:2015-07-05 15:46:41

标签: xml xpath lxml

我有一个xml:

<data>
    <title>Nice day</title>
    <foo>
        <bar>
        <count>5</count>
        </bar>
    </foo>
</data>

首先,我使用Xpath搜索特定节点

e= root.findall('/title/foo/bar/count')

然后我检查具有特殊条件的节点的值

if int(e.text)>0:

如果节点满足我,我想获得 title 节点的值。

现在我正在为它寻找合适的xpath语法。 感谢。

2 个答案:

答案 0 :(得分:2)

fount foo哪个孙子计数有文本并取相同父母的标题文本

//foo[bar/count/text()]/../title/text()

答案 1 :(得分:1)

一个选项:

/data/title[./following-sibling::foo/bar/count[text() > 0]]/text()

选中xmllint

xmllint --xpath '/data/title[./following-sibling::foo/bar/count[text() > 0]]/text()' xmlfile

它产生:

Nice day
相关问题