简体   繁体   中英

Way to determine XPath to retrieve data of a specific attribute

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>

Try this

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM