1

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>

1 Answer 1

2

Try this:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.