Xml 在父节点中搜索

Xml search in parent node

我有一个 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:

如果node让我满意,我想得到一个title节点的值。

现在我正在为它寻找正确的 xpath 语法。 谢谢

fount foo which grandchild count 有文本并取相同的标题文本parent

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

一个选项:

/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