Use of XPath function in Message Template

I am trying to form Json payload by reading xml payload in response. For this I am using AssignMessage policy with xpath function in Template.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-FormResponse">
    <DisplayName>AM-FormResponse</DisplayName>
    <Properties/>
    <AssignVariable>
        <Name>xpath</Name>
        <value>//return/code/text()</value>
    </AssignVariable>
    <AssignVariable>
        <Name>extracted_code</Name>
        <Template>{xpath(xpath,response)}</Template>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

While saving the proxy I am getting "Unknown string function : xpath" error.

I also tried using this function directly in Set Payload but there instead of value I am getting xpath in response.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-FormResponse">
    <DisplayName>AM-FormResponse</DisplayName>
    <Properties/>
    <Set>
        <Payload contentType="application/json">{
              "code": "{xpath('//return/code/text()',response.content)}"
            }
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

Output of this policy -

{
  "code": "{xpath('//return/code/text()',response.content)}"
}

I am trying this Apigee SaaS. How can I correct this?

1 12 561
12 REPLIES 12

Not applicable

use extract variable policy to fetch the variables and then frame the json payload using assign message policy.

or

you can use xml-to-json policy and then format your json payload using assign message or javascript policy.

Thanks Ajitav, I am able to form the message using EV and AM policies, just wanted to use xpath functions so that this can be done only using AM policy.

I would try this :

<AssignMessage name="AM-FormResponse">
  <AssignVariable>
    <Name>my_path</Name>
    <Value>//return/code/text()</Value>
  </AssignVariable>

    <Set>
        <Payload contentType="application/json">{
              "code": "{xpath(my_xpath,response.content)}"
            }
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

Thanks Dino for answering.

I tried this also but getting same error - "The revision is deployed, but traffic cannot flow. Unknown string function : xpath"

That means you are on an older version of Apigee, which does not include the xpath function. If you upgrade, you'll get it. What version are you using?

We are using Apigee SaaS.

@Dino - Is this function not supported in Apigee Saas?

It is being rolled out now. It is available in evaluation orgs for SaaS.

Hi @dchiesa1 sorry to restart this thread, not sure if there was one already existing if yes I can repost it. 
We are using Apigee X (1-11-0-apigee-7).

Use case - We are using a service callout to invoke a REST API. I am looking to capture the response field using Assign Message and was trying to use the jsonpath function but while deploying the proxy am getting a deployment error as - "reported error entities.UnknownStringFunction: "Unknown string function : jsonpath" ".
In the Assign Message I have this as below:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-CaptureStatusCode">
<DisplayName>AM-CaptureStatusCode</DisplayName>
<Copy source="avScanResponse">
<Headers/>
<QueryParams/>
<FormParams/>
<Payload>true</Payload>
<Verb/>
<StatusCode/>
<ReasonPhrase/>
<Path/>
</Copy>
<AssignTo>avScanResponse</AssignTo>
<AssignVariable>
<Name>status_code</Name>
<Value/>
<Ref>avScanResponse.status.code</Ref>
</AssignVariable>
<AssignVariable>
<Name>rsp_status</Name>
<Value/>
<Template>{jsonpath($.avScanResponse,status)}</Template>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

 

I was able to capture the status field using Extract Variables, but checking out option if it can be achieved through the Assign Message.

Sample Response from Service Callout:

 

    "status": "SUCCESS",
    "description": "Text Edited",
    "inputFilePath": null,
    "uploadedFilePath": null

 

Please let me know if anything is missing from my side will update accordingly.

Thanks and Regards,

Debjit

ya, the string functions are case-sensitive. you want jsonPath, with an uppercase P.

Thank you @dchiesa1 it worked. It also proved I need a new set of spectacles. 

😉

I'm glad you got it sorted out!