sazr
sazr

Reputation: 25938

XPath Query to find Specific Element Fails

I am attempting to parse some XML data using XPath queries in C#. But my query is not successfully finding the element I am looking for (it finds nothing).

Whats wrong with my XPath query? Is my syntax for following-sibling incorrect or something? How can I edit my XPath to find the correct value element?

<attributes>
  <real>
    <name>cover rl</name>
    <value>89.87414122</value>
  </real>
  <real>
    <name>pit depth</name>
    <value>2.35620671</value> <!-- This is the value I need -->
  </real>
<attributes>

My XPath query that fails:

ns:attributes/real/name[text() = 'pit depth']/following-sibling::value

Upvotes: 1

Views: 120

Answers (1)

kjhughes
kjhughes

Reputation: 111736

You're close. Mostly get rid of the spurious ns: namespace prefix. Also note that your sample input XML should end with a closing </attributes> element rather than another opening <attributes> element

So, this XPath:

/attributes/real/name[. = 'pit depth']/following-sibling::value

Will yield:

<value>2.35620671</value>

per your request.

If you only want the contents of the value element:

/attributes/real/name[. = 'pit depth']/following-sibling::value/text()

Will yield:

2.35620671

Upvotes: 1

Related Questions