Sona Shetty
Sona Shetty

Reputation: 1047

Xpath of previous sibling

I am trying to retrieve the text of <value> node from a huge xml having structure as show below. Is there anyway to write a generic xpath query if the text of node <name> is know to user?

<ReferencesServiceQualificationItem> 
  <ItemInvolvesProduct> 
    <ID>CPI000123456854</ID>  
    <DescribedBy> 
      <value>Yes</value>  
      <Characteristic> 
        <name>POTS Interconnect Match</name> 
      </Characteristic> 
    </DescribedBy>  
    <DescribedBy> 
      <value>null</value>  
      <Characteristic> 
        <name>Remediation Date</name> 
      </Characteristic> 
    </DescribedBy> 
  </ItemInvolvesProduct> 
</ReferencesServiceQualificationItem>

Upvotes: 2

Views: 753

Answers (2)

Valckrie
Valckrie

Reputation: 23

Assuming you want the preceeding "DescribedBy" node and you have the text of the current node:

//DescribedBy[Characteristic/name = 'Remediation Date']/preceding-sibling::DescribedBy[1]/value

Upvotes: 0

har07
har07

Reputation: 89285

This is one possible XPath, given that user know the name is, for example, 'POTS Interconnect Match' :

//DescribedBy[Characteristic/name='POTS Interconnect Match']/value

Basically the above XPath search for DescribedBy element where Characteristic/name child equals "POTS Interconnect Match" and then return the corresponding value element.

xpathtester demo

output :

<value>Yes</value>

Upvotes: 1

Related Questions