Way to determine XPath to retrieve data of a specific attribute

71 views Asked by At

I am working on an integration and the response I am getting in XML format. I need to parse it and get the attribute values using javascript. I am trying to get value from node <ab:specific_field> with <ab:field_reference>id is commodity or cosmetic. I am struggling to go trough nodes and extract <ab:Value> node value which is the value I need to extract.

I tried with //ab:Main_Data/ab:specific_field[1]/ab:Value, but no luck. Can anyone help me to write a correct XPath to extract value from <ab:value> node.

<ab:Response_Data>
<ab:MainData>
  <ab:reference>....</ab:refernce>
  <ab:information....<ab..info>
   <ab:specific_field>
     <ab:Field_Reference>
     <ab:ID type="WID">123</ab:ID>
     <ab:ID wd:parent_id="custom-API-Service" ab:parent_type="Integration_Document_Name" ab:type="Integration_Document_Field_Name">Commodity</ab:ID>
     </ab:Field_Reference>
     <ab:Value>Medicine</ab:Value>
   </ab:specific_field>
   <ab:specific_field>
<ab:Field_Reference>
     <ab:type="WID">1234</ab:ID>
     <ab:ID wd:parent_id="custom-API-Service" ab:parent_type="Integration_Document_Name" ab:type="Integration_Document_Field_Name">Cosmetic</ab:ID>
     </ab:Field_Reference>
     <ab:Value>Powder</ab:Value>
   </ab:specific_field>
</ab:MainData>
</ab:Response_Data>
2

There are 2 answers

0
rdonuk On

Try this

//ab:maindata/ab:specific_field[.//ab:id[normalize-space()='commodity' or normalize-space()='cosmetic']]/ab:value
0
Oktay On

Your XML has errors. If you have more than one ab:ID fields you can filter one of them with a property.

To get ab:ID nodes:

//ab:ID[@wd:parent_id="custom-API-Service"]

To get ab:ID text nodes:

//ab:ID[@wd:parent_id="custom-API-Service"]/text()