xpath:find child,其中同一节点的另一个子节点具有特定值

时间:2013-05-15 10:05:54

标签: xpath

这是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

3 个答案:

答案 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']