Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AND operator in XPath?

Tags:

php

xml

xpath

XML is like

 <a id="1">
   <b>value1</b>
   <b>value2</b>
 </a>

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 and value2 are present in <b> node, but I can not get exactly as I am using XPath first time.

like image 489
hrishi Avatar asked Mar 12 '23 19:03

hrishi


1 Answers

The following XPath,

//a[b = 'value1' and b = 'value2']/@id

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.

like image 197
kjhughes Avatar answered Mar 21 '23 01:03

kjhughes