unable to combine two responses in xml payload in APIGEE mashup

Hi All,

I’m facing issues in trying to combine 2 XML responses into using APIGEE mashup. Below is my configuration:

XML Payload 1:

<root>
<parent>
<fname>A</fname>
</parent>
<parent>
<fname>B</fname>
</parent>
<parent>
<fname>C</fname>
</parent>
</root>

Callout Service 1:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="callout_service_1">
    <DisplayName>callout_service_1</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>calloutResponse_1</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>https://localhost:8080/getService1</URL>
    </HTTPTargetConnection>
</ServiceCallout>

Extract Variables 1:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="extract_service_1">
    <DisplayName>extract_service_1</DisplayName>
    <Properties/>
    <Source clearPayload="false">calloutResponse_1</Source>
    <VariablePrefix>detailResponse1</VariablePrefix>
    <XMLPayload stopPayloadProcessing="false">
        <Variable name="parent" type="nodeset">
            <XPath>//root/parent</XPath>
        </Variable>
    </XMLPayload>
</ExtractVariables>

XML Payload 2:

<root>
<parent>
<fname>D</fname>
</parent>
<parent>
<fname>E</fname>
</parent>
<parent>
<fname>F</fname>
</parent>
</root>

Callout Service 2:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="callout_service_2">
    <DisplayName>callout_service_2</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>calloutResponse_2</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>https://localhost:8080/getService2</URL>
    </HTTPTargetConnection>
</ServiceCallout>

Extract Variables 2:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="extract_service_2">
    <DisplayName>extract_service_2</DisplayName>
    <Properties/>
    <Source clearPayload="false">calloutResponse_2</Source>
    <VariablePrefix>detailResponse1</VariablePrefix>
    <XMLPayload stopPayloadProcessing="false">
        <Variable name="parent" type="nodeset">
            <XPath>//root/parent</XPath>
        </Variable>
    </XMLPayload>
</ExtractVariables>

I’m trying to achieve the below output:

<root>
<parent>
<fname>A</fname>
</parent>
<parent>
<fname>B</fname>
</parent>
<parent>
<fname>C</fname>
</parent>
<parent>
<fname>D</fname>
</parent>
<parent>
<fname>E</fname>
</parent>
<parent>
<fname>F</fname>
</parent>
</root>

When I hit the URL and trace the request, the value of the variable parent from Extract Variables 1 is getting overridden by Extract Variables 2 as the key is same in both the cases.

I’ve tried using property but unable to get the response assign to it. I’ve already raised a ticket on it ref: Facing problem in assigning response from xml payload to variablePrefix.

I tried combining the response using javascript using “+” operator but got an error as below:

This page contains the following errors:
error on line 7 at column 9: Extra content at the end of the document
Below is a rendering of the page up to the first error.

An invalid XML is formed.

Please help me out with this.

I’ve tried using property but unable to get the response assign to it.

Be careful. It could be that the response is being assigned but the Trace does not reflect that. Check your assumption. I believe there is an outstanding bug on this - Trace does not display results of ExtractVariables when there is a VariablePrefix.

Also I noticed your VariablePrefix is the same in both ExtractVariables policies. So that’s a problem. if you want to not stomp your variables, use different prefixes, or different names.

Even without the VariablePrefix, you can assign to a different variable name. Why use “parent” in both cases? Why not use “parent2” or anything OTHER than parent in the 2nd ExtractVariables?

    <!-- <VariablePrefix>detailResponse1</VariablePrefix> -->
    <XMLPayload stopPayloadProcessing="false">
        <Variable name="parent2" type="nodeset">
            <XPath>//root/parent</XPath>
        </Variable>
    </XMLPayload> 

But, if your real goal is to mashup 2 xml documents, you don’t need ExtractVariables at all. You can use XSL. Here is a question and answer from last week on the topic.

You may be thinking: “XSL!!!?! I don’t want XSL!” And I’m sorry about that. But you cannot effectively combine XML with “+” in Javascript. That won’t work, without a lot of pain and anguish. XSL is the right tool for the job, here.

@Dino: Thank you for your reply. I’ll check the reference URL shared by you.