如何在 XPath 中使用 AND 运算符?
问题描述:
XML 就像
<a id="1">
<b>value1</b>
<b>value2</b>
</a>
我想编写 XPath 来查找 的 id,其中将有两个
子节点具有固定值
value1
和 value2
.我试图找出具有类似条件的 XPath
I want to write XPath to find id of <a>
where there will be two <b>
child nodes having fix value value1
and value2
. I tried to to find out XPath with condition like
$xml->xpath('*[b=value1] | *[b=value2]');
value1
和 value2
存在于 <b>
节点中,但我无法完全像我第一次使用 XPath 那样得到.
value1
and value2
are present in <b>
node, but I can not get exactly as I am using XPath first time.
答
下面的XPath,
//a[b = 'value1' and b = 'value2']/@id
将选择所有 a
元素的 id
属性,其中子 b
元素的字符串值等于 value1
AND 另一个子 b
元素的字符串值等于请求的 value2
.
will select id
attributes of all a
elements with a child b
element having string value equal to value1
AND another child b
element having string value equal to value2
as requested.