D. Caan
D. Caan

Reputation: 2019

Select specific value XPath

How can I select just the "fail" value using XPath?

<Properties>
  <Property Descriptor="100">1377349460.298</Property>
  <Property Descriptor="101">1</Property>
  <Property Descriptor="24000">fail</Property>
</Properties>
<Properties>
  <Property Descriptor="100">1377349462.298</Property>
  <Property Descriptor="101">1</Property>
  <Property Descriptor="24000">pass</Property>
</Properties>

I tried Property[@Value="fail"] but it doesn't work.

Upvotes: 2

Views: 1976

Answers (2)

MiMo
MiMo

Reputation: 11953

To select the Property element containing the string fail use:

Property[.='fail']

or

Property[text()='fail']

as Babai correctly suggest.

. means 'the current node', text() means 'the first text within the current node', so the expressions mean 'select an element called Property containing fail')

This assumes that the current node is right above the Property element, if you want to find Property in any level below the current node use:

//Property[.='fail']

In XPath a non-empty node-set is considered true when used in a test - so the above XPath is (in a sense) already a test if there are any Property nodes anywhere containing fail.

If you want to count how many nodes Property nodes at any level contain fail use:

count(//Property[.='fail'])

Upvotes: 3

Arup Rakshit
Arup Rakshit

Reputation: 118261

You could do this as below also :-

//Property[text()='fail']

Upvotes: 2

Related Questions