0

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.

adomyaty
  • 11
  • 7

1 Answers1

1

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>
Kyle Pittman
  • 2,858
  • 1
  • 30
  • 38
  • Thanks Kyle. Using your expression I get "ERROR - Failed to evaluate XPath expression: Extra illegal tokens: '@', 'id'" when using that link to test it and pasting my XML there. Not sure why, is it the spaces that's doing it? – adomyaty Jun 06 '14 at 01:12
  • Seems like it's nto a good xml the way it's being generated. After pressing fix and reissuing the expression you mentioned now I'm getting "ERROR - Seem like XML is not well formed:Element type "node" must be followed by either attribute specifications, ">" or "/>"." – adomyaty Jun 06 '14 at 01:15
  • Copy/paste the //nodes/node[@name='00_QATEST']//@id part into the "XPath" field, and copy paste the XML I put in the edit into the XML field, then hit Test. You should get a result of "1234abc" – Kyle Pittman Jun 06 '14 at 01:32
  • Here try this (This resmbles more the actual data im referring minus some confidential data replaced) – adomyaty Jun 06 '14 at 01:41
  • Using the above with the expression you mentioned results in ERROR - Seem like XML is not well formed:Element type "node" must be followed by either attribute specifications, ">" or "/>". – adomyaty Jun 06 '14 at 01:42
  • There are some spacing issues and other issues with the xml you pasted. If I clean it up and change my XPath to //nodes/node[@name='Demo']//@id the result I get is 'acf-123-asd', as you'd expect it to be. So I think you need to inspect your source XML more closely. – Kyle Pittman Jun 06 '14 at 01:55
  • 1
    No problem, be sure to mark your question as answered if I helped you any. Cheers :) – Kyle Pittman Jun 09 '14 at 17:34