Selecting Values in XPath depending on values at same level

3.1k views Asked by At

I've got this partial XML

 <Events>
   <Properties>
     <Property Descriptor="1">VALUE1</Property>
     <Property Descriptor="2">FOO</Property>
     <Property Descriptor="3">BAR</Property>
     </Properties>
   <Properties>
     <Property Descriptor="1">VALUE2</Property>
     <Property Descriptor="2">NO</Property>
     <Property Descriptor="3">NOTHINGHERE</Property>
   </Properties>
 </Events>

Hi can I query the first occurrence of Property Descriptor="3" when Property Descriptor="2" = "FOO"? Result here should be BAR

I tried //Properties/Property[@Descriptor="3"][..//Property[@Descriptor="2"] = "FOO"] but it's now working.

1

There are 1 answers

0
Mathias Müller On BEST ANSWER

can I query the first occurrence of Property Descriptor="3" when Property Descriptor="2" = "FOO"?

/Events/Properties[Property[@Descriptor = '2'] = 'FOO']/Property[@Descriptor = '3'][1]

And in more detail:

/Events/Properties                    Look for a Properties element
[Property                             but only if it has a Property element
[@Descriptor = '2'] = 'FOO']          but only if its Descriptor attribute equals 2 and
                                      its textual content is "FOO"
/Property[@Descriptor = '3']          for that Properties element look for a child element
                                      called Property where the Descriptor attribute equals 3
[1]                                   thereof return the first occurrence