Arman Freitas
Arman Freitas

Reputation: 23

Xpath looking for an attribute

So I have the following XML:

<root>
    <people>

        <person id="a01">
            <name>Jack</name>
        </person>

        <person id="a02">
            <name>George</name>
        </person>


    </people>

    <groups>
        <group groupId="g01">
            <member refId="a01" />
            <member refId="a02" />
        </group>
    </groups>
</root>

And I was looking for the name of all the name of the persons in the group with the groupId g01. What I was trying to do was :

//name[@id=//group[@groupId='g01']/member/@refId]/text()

But apparently it doesn't work.

Cant I use an expression when looking for an attribute? How can I solve my problem then?

Im getting really confused because when there is an xpath problem its always involving idrefs and I just cant understand how to solve them if I cant store any variables in xpath.

Upvotes: 2

Views: 68

Answers (1)

Andersson
Andersson

Reputation: 52685

I'm not sure I correctly understood your problem, so let me know if below solution doesn't work as expected

//people/person[@id=//group[@groupId="g01"]/member/@refId]/name/text()

This should allow you to get name if the value of @id of parent person node is in list of @refId values from member nodes that are children of group with @groupId="g01"

Upvotes: 3

Related Questions