hrishi hrishi - 1 month ago 24
PHP Question

Skip values in PHP XPath

I am using

PHP
xpath
to read values from
XML
. I have condition like below where I want to get Id of "a" where there is only name tag in "b". and skip if any other tag is present along with name. expected out put is 1. but I am getting all the Ids of "a" tag. I am using
Xpath
as

$xml->xpath("//a[b/name = 'value1' ]/@id");


how I can skip ids 2 and 3 and get output 1

sample XML

<a id="1">
<b>
<name> value1 </name>
</b>
</a>
<a id="2">
<b>
<name> value1 </name>
<other> value2 </other>
</b>
</a>
<a id="3">
<b>
<name> value1 </name>
<other> value3 </other>
</b>
</a>

Answer

Try this:

//a/b[count(*) = 1 and count(name) = 1]/../@id