adomyaty
adomyaty

Reputation: 11

Issue with Xpath expression to extract an attribute when another attribute is a match

my first question here!

So I'm using the following expression: //nodes/node[@name='00_QATEST']

and the XML is like this:

xml version 1.0
    <nodes>
        <node id = "1234abc" name="00_QATEST"/>
        <node id = "12345abcd" name="00_QATEST2"/>

When I use the above expression it will return the whole block. I need to get just the id value. Now if I do put id instead of node in the above expression I get nothing returned.

Upvotes: 0

Views: 143

Answers (1)

Kyle Pittman
Kyle Pittman

Reputation: 3077

Your XPath is selecting the entire node because you've only selected //nodes/node... That is, you're selecting the entire node, and not just fetching the ID.

Instead, try adding //@id to select only the ID of the node with the correct name.

//nodes/node[@name='00_QATEST']//@id

Also you might like to check out: http://www.xpathtester.com/xpath

Edit: I tested your xml with the following code:

<nodes>
    <node id = "1234abc" name="00_QATEST"/>
    <node id = "12345abcd" name="00_QATEST2"/>
</nodes>

Upvotes: 1

Related Questions