当邻居属性值匹配时,Xpath获取元素值

时间:2019-12-13 14:55:50

标签: xpath

我的XML如下:

<prop type="x-ConfirmationLevel">Approved</prop>
<prop type="x-Internal project number:SingleString">O-74205</prop>
<prop type="x-Product Line:MultiplePicklist">Services</prop>
<prop type="x-Product Line:MultiplePicklist">Raman</prop>
<prop type="x-Product Line:MultiplePicklist">Support</prop>

,当我 prop时,我想获取元素值“ prop type =” x-内部项目编号:SingleString“ ” type =“ x-Product Line:MultiplePicklist” ==拉曼 ”。

我的Xpath如下:

//prop[@type = "x-Product Line:MultiplePicklist" and text() = "Raman"]

在我的XML中,Xpath应该仅返回O-74025。

有什么主意吗?谢谢!

2 个答案:

答案 0 :(得分:1)

喜欢这个:

样本输入:

<?xml version="1.0"?>
<root>
<x>
<prop type="x-ConfirmationLevel">Approved</prop>
<prop type="x-Internal project number:SingleString">O-74205</prop>
<prop type="x-Product Line:MultiplePicklist">Services</prop>
<prop type="x-Product Line:MultiplePicklist">Raman</prop>
<prop type="x-Product Line:MultiplePicklist">Support</prop>
</x>
<x>
<prop type="x-ConfirmationLevel">Approved</prop>
<prop type="x-Internal project number:SingleString">O-74205</prop>
<prop type="x-Product Line:MultiplePicklist">Services</prop>
<prop type="x-Product Line:MultiplePicklist">Foobar</prop>
<prop type="x-Product Line:MultiplePicklist">Support</prop>
</x>
</root>

代码:

xmllint --xpath '
    //prop[@type="x-Product Line:MultiplePicklist" and \
    text()="Raman"]/../prop[@type="x-Internal project number:SingleString"]/text()
' file.xml

输出:

O-74205

答案 1 :(得分:1)

只需在表达式中使用parent::node()引用: //prop[@type = "x-Product Line:MultiplePicklist" and text() = "Raman"]/parent::node()/prop[@type = "x-Internal project number:SingleString"]/text()