extract variables policy to extract all values using xpath

sjm2000
Participant V

Hi.

Any idea how to use extract variables policy to extract all values of "amountofpay"

using xpath?

I tried my favourite --> //amountofpay , but returning only first value.

Below sample, contains only 2 "return" tags .

The output is dynamic and the number of "return" tags can vary each time.


<Envelope>
   <Body>
      <GetPayResponse>
         <return>
            <amountofpay>2.00</amountofpay>
            <modeofpayment>CC</modeofpayment>
         </return>
         <return>
           <amountofpay>35.50</amountofpay>
            <modeofpayment>HQ</modeofpayment>
         </return>
 	</GetPayResponse>
</Body>
</Envelope>


All help is appreciated.

Best Regards

Sujith Mathew

1 3 1,324
3 REPLIES 3

You must extract the values into a nodeset.

    <XMLPayload stopPayloadProcessing="false">
        <Namespaces/>
        <Variable name="amounts" type="nodeset">
            <XPath>/Envelope/Body/GetPayResponse/return/amountofpay</XPath>
        </Variable>
    </XMLPayload>

...but then you have a nodeset in the variable. You need to then parse THAT to extract the number of things extracted.

You can use JS for that purpose if you like.

Something like this:

function extractValues(s) {
  var results = [];
  var re = new RegExp('<[a-zA-Z_]+>([^<]+)</[a-zA-Z_]+>', 'g');
  var match;
  while (match = re.exec(s)) {
    results.push(match[1]);
  }
  return results;
}


var amounts = context.getVariable('amounts');
var values = extractValues(amounts);

Thank you very much I will try it out when I reach office

Before that just wanted to know is there any feature in the extract variables apigee policy itself- without having the last JavaScript policy ?

I would love to know.

I am using both on premise versions of 16.05 and 16.01.

The above is not working, i changed match == re.exec(s)

and still i can't bring the "values_var"

to display as JSON variable using assign message policy which is my ultimate objective 🙂

function extractValues(s) {
  var results = [];
  var re = new RegExp('<[a-zA-Z_]+>([^<]+)</[a-zA-Z_]+>', 'g');
  var match;
  while (match == re.exec(s)) {
    results.push(match[1]);
  }
  return results;
}
var amounts = context.getVariable('amounts');
var values_var = extractValues(amounts);

Below is my assign message for reference 🙂 . All help is appreciated.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage name="create-modified-to-json" enabled="true">
    <Set>
        <Headers/>
        <Payload contentType="application/json" variablePrefix="@" variableSuffix="#">
        "@values_var#"
            </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="response"/>
    <DisplayName>create-modified-to-json</DisplayName>
</AssignMessage>

Note: @Dino , just to clarify why i am not using the xml to json wizard, it's a long story, in brief, this api will be one of the critical system in the our organization and i don't want any chances with a top-down approach

Best Regards

Sujith Mathew