How to get the attribute name from the element value in XPath

Viewed 27

I have an XML

<root xmlns:foo="http://www.eample.com/" xmlns:bar="http://www.example.com">
            <actors>
                <actor id="1">Christian Bale</actor>
                <actor id="2">Liam Neeson</actor>
                <actor id="3">Michael Caine</actor>
            </actors>
            <foo:singers>
                <foo:singer id="4">Tom Waits</foo:singer>
                <foo:singer id="5">B.B. King</foo:singer>
                <foo:singer id="6">Ray Charles</foo:singer>
            </foo:singers>
        </root>
    

I want to get the attribute id value from the XPath based on the element value.

Example:

For the above XML how to get the id value for the actor "Christian Bale"?

I have tried

/root/actors[actor='Christian Bale']/@id

It is giving nothing as the output.

1 Answers

This will work:

//actor[text()='Christian Bale']/@id

You can validate this here

Related