这是xml:
<cat> <row> <col attr="w">111</col> <col attr="p">222</col> <col attr="g">333</col> <col attr="r">444</col> <col attr="n1">555</col> <col attr="n2">666</col> <col attr="s">777</col> </row> <row> <col attr="w">aaa</col> <col attr="p">bbb</col> <col attr="g">nnn</col> <col attr="r">mmm</col> <col attr="n1">xxx</col> <col attr="n2">ccc</col> <col attr="s">vvv</col> </row> </cat>
我在找什么: 找到
//cat/row/col[@attr='n2']仅
,如果在同一父节点
中//cat/row/col[@attr='w']==111
答案 0 :(得分:1)
你正确的XPATH是:
//cat/row/col[@attr='n2'][parent::row/col[@attr='w' and text()='111'] ]
获得输出:
<col attr="n2">666</col>
答案 1 :(得分:1)
这是一个替代解决方案,我认为随着您在XPath表达式的每个节点中添加条件时更具可读性,而无需使用parent
:
//cat/row[./col[@attr='w' and text()='111']]/col[@attr='n2']
具有相同的输出:
<col attr="n2">666</col>
答案 2 :(得分:0)
此xpath将找到带有attr = w和text ='111'的col,然后返回到它的父级并查找父级的子项col @ attr = n2
//row/col[@attr='w' and text()='111']/../col[@attr='n2']