Hi Aditya,
You can use XSLT mediator [1] for this purpose. You could pass your
current response
from AggregateMediator to an XSLT mediator and build the required response.
I have tried with the attached XSLT and received below output.
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<jsonObject>
<id>1</id>
<make>TAHB</make>
<children>
<kid>1</kid>
</children>
<children>
<kid>2</kid>
</children>
<name>Home</name>
<area>5000sqft</area>
<children>
<id>2</id>
<make>Tonda</make>
<name>Car</name>
<model>Tmaze</model>
</children>
<children>
<id>3</id>
<make>Tamsung</make>
<name>Mobile</name>
<model>S8</model>
</children>
</jsonObject>
</soapenv:Body>
</soapenv:Envelope>
[1] https://docs.wso2.com/display/ESB500/XSLT+Mediator
Thanks,
Himasha
On Thu, Jul 20, 2017 at 11:23 PM, Sudharma Subasinghe <sudhar...@wso2.com>
wrote:
> HI Aditya,
>
> I tried the attached configs in the mail thread and could get expected
> output as in above. Sorry for missing the point of backend service.
>
> The xpath *get-property('firstResponse')/children* works for me and it
> will add elements to *children* element as in your config. Hope you are
> trying with a new ESB version.
>
> *EnrichMediator} - Invalid Target object to be enrich *error can be happened
> for xpath *get-property('firstResponse')/children, *if there is no
> 'firstResponse' is formed before 'aggrementResponse'. The aggregated result
> from the backend service can be as following when aggregating results. So
> there will be not be 'firstResponse' before 'aggrementResponse and that
> error will be occured. Same can be happened with
>
> *xpath="$ctx:firstResponse".*
> <?xml version='1.0' encoding='utf-8'?>
> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/
> ">
> <soapenv:Body>
> <Information>
> <jsonObject>
> <id>3</id>
> <make>Tamsung</make>
> <name>Mobile</name>
> <model>S8</model>
> </jsonObject>
> <jsonObject>
> <id>1</id>
> <make>TAHB</make>
> <children>
> <kid>1</kid>
> </children>
> <children>
> <kid>2</kid>
> </children>
> <name>Home</name>
> <area>5000sqft</area>
> </jsonObject>
> <jsonObject>
> <id>2</id>
> <make>Tonda</make>
> <name>Car</name>
> <model>Tmaze</model>
> </jsonObject>
> </Information>
> </soapenv:Body>
> </soapenv:Envelope>
>
> Assuming that your backend gives mentioned output by you, Can you try by
> adding PayloadFactory mediator to your main.xml as in below.
>
> <default>
> -----------------------------------------------------------
> <payloadFactory media-type="xml">
> <format>
> <jsonObject xmlns="">
> <id>$1</id>
> <children xmlns="">
> <id>$1</id>
> <make>$2</make>
> <name>$3</name>
> <model>$4</model>
> </children>
> </jsonObject>
> </format>
> <args>
> <arg evaluator="xml" expression="//jsonObject/id"/>
> <arg evaluator="xml" expression="//jsonObject/make"/>
> <arg evaluator="xml" expression="//jsonObject/name"/>
> <arg evaluator="xml" expression="//jsonObject/
> model"/>
> </args>
> </payloadFactory>
> <property name="aggrementResponse"
> expression="//jsonObject/children"
> scope="default"
> type="OM"/>
> <enrich>
> <source xpath="get-property('
> aggrementResponse')"/>
> <target action="sibling"
> xpath="get-property('firstResponse')/children"/>
> </enrich>
> </default>
> --------------------------------------------------------------------
> <enrich>
> <source clone="true" xpath="get-property('
> firstResponse')"/>
> <target type="body"/>
> </enrich>
> ------------------------------------------------------------------------
> <property name="messageType" value="application/json" scope="axis2"/>
>
> This worked for me.
>
>
> Thanks
> Sudharma
>
> On Thu, Jul 20, 2017 at 6:46 PM, aditya shivankar <
> shivankar.adit...@gmail.com> wrote:
>
>> Thanks Sudharma for reply,
>>
>>
>> As I mentioned* "modifyAgRes.xml" *is a mock of backend service. I
>> cannot change the backend service. Whatever modifications I have to do is
>> only to main xml* "**addChild.xml**".*
>> Plus I think we cannot use payloadFactory this way , as number of
>> elements coming in input array are not fixed.It can be any number.
>>
>> One more thing, Enrich mediator is behaving weirdly. For editing the
>> property of type "OM",
>> suppose property like this
>>
>> <property expression="//jsonObject" name="firstResponse" scope="default"
>> type="OM"/>
>>
>> use of below xpath sometimes works and sometimes not
>>
>> *xpath="$ctx:firstResponse"*
>>
>> but mostly (intermitently) it gives below error
>>
>> *EnrichMediator} - Invalid Target object to be enrich. *
>>
>> I am getting the similar error , even for the xpath you have suggested.
>>
>> * xpath="get-property('firstResponse')/children*
>>
>> With Regards,
>> Aditya
>>
>>
>>
>>
>> On Thu, Jul 20, 2017 at 3:31 PM, Sudharma Subasinghe <sudhar...@wso2.com>
>> wrote:
>>
>>> Hi,
>>>
>>> Try using PayloadFactory mediator as below. Change your configs as
>>> following.
>>>
>>> *modifyAgRes.xml*
>>>
>>> <case regex="2">
>>> <payloadFactory media-type="xml">
>>> <format>
>>> <jsonObject xmlns="">
>>> <id>$1</id>
>>> <children xmlns="">
>>> <id>$1</id>
>>> <make>$2</make>
>>> <name>Car</name>
>>> <model>Tmaze</model>
>>> </children>
>>> </jsonObject>
>>> </format>
>>> <args>
>>> <arg evaluator="xml" expression="//jsonObject/id"/>
>>> <arg evaluator="xml" expression="//jsonObject/make"
>>> />
>>> </args>
>>> </payloadFactory>
>>> </case>
>>> <case regex="3">
>>> <payloadFactory media-type="xml">
>>> <format>
>>> <jsonObject xmlns="">
>>> <id>$1</id>
>>> <children xmlns="">
>>> <id>$1</id>
>>> <make>$2</make>
>>> <name>Mobile</name>
>>> <model>S8</model>
>>> </children>
>>> </jsonObject>
>>> </format>
>>> <args>
>>> <arg evaluator="xml" expression="//jsonObject/id"/>
>>> <arg evaluator="xml" expression="//jsonObject/make"
>>> />
>>> </args>
>>> </payloadFactory>
>>> </case>
>>> ----------------------------------------------------------
>>> <!--enrich>
>>> <source clone="true" xpath="//jsonObject"/>
>>> <target type="body"/>
>>> </enrich-->
>>>
>>>
>>>
>>> *addChild.xml*<property name="aggrementResponse"
>>> expression="//jsonObject/children"
>>> scope="default"
>>> type="OM"/>
>>>
>>> <enrich>
>>> <source xpath="get-property('aggrementResponse')"/>
>>> <target action="sibling" xpath="get-property('firstResp
>>> onse')/children"/>
>>> </enrich>
>>>
>>> Thanks
>>> Sudharma
>>>
>>> On Thu, Jul 20, 2017 at 2:03 AM, aditya shivankar <
>>> shivankar.adit...@gmail.com> wrote:
>>>
>>>> Adding responses as elements ,to an array in json response
>>>>
>>>> Input request :
>>>>
>>>> [
>>>> {
>>>> "id" : "1",
>>>> "make" : "TAHB",
>>>> "children":[
>>>> {
>>>> "kid":"1"
>>>> },
>>>> {
>>>> "kid":"2"
>>>> }
>>>> ]
>>>> },
>>>> {
>>>> "id" : "2",
>>>> "make" : "Tonda"
>>>> },
>>>> {
>>>> "id" : "3",
>>>> "make" : "Tamsung"
>>>> }
>>>> ]
>>>>
>>>>
>>>> I am using iterate, to send each element of above array as request to a
>>>> backend service( In myactual project this is not so simple service i.e.
>>>> backend service reponse is very complex json with lots of arrays and
>>>> sub-arrays(child arrays)in it . for better understanding of issue I kept it
>>>> like this ).
>>>>
>>>> The responses of the backend service are aggregated in one soap xml by
>>>> AggregateMediator.
>>>>
>>>> Below is response from AggregateMediator
>>>>
>>>> <?xml version='1.0' encoding='utf-8'?>
>>>> <soapenv:Envelope xmlns:soapenv="http://schemas.
>>>> xmlsoap.org/soap/envelope/">
>>>> <soapenv:Body>
>>>> <Information>
>>>> <jsonObject>
>>>> <id>1</id>
>>>> <make>TAHB</make>
>>>> <children>
>>>> <kid>1</kid>
>>>> </children>
>>>> <children>
>>>> <kid>2</kid>
>>>> </children>
>>>> <name>Home</name>
>>>> <area>5000sqft</area>
>>>> </jsonObject>
>>>> <jsonObject>
>>>> <id>2</id>
>>>> <make>Tonda</make>
>>>> <name>Car</name>
>>>> <model>Tmaze</model>
>>>> </jsonObject>
>>>> <jsonObject>
>>>> <id>3</id>
>>>> <make>Tamsung</make>
>>>> <name>Mobile</name>
>>>> <model>S8</model>
>>>> </jsonObject>
>>>> </Information>
>>>> </soapenv:Body>
>>>> </soapenv:Envelope>
>>>>
>>>> Requirement is to add all the responses from the backend service except
>>>> the first one, to the child array of the first response of the backend
>>>> service.
>>>> I do not know how to do this ? not even how to add new elements to a
>>>> array in wso2.
>>>>
>>>> My Current Output :
>>>>
>>>> {
>>>> "id": 1,
>>>> "make": "TAHB",
>>>> "children": [{
>>>> "kid": 1
>>>> },
>>>> {
>>>> "kid": 2
>>>> }],
>>>> "name": "Home",
>>>> "area": "5000sqft",
>>>> "jsonObject": [{
>>>> "id": 2,
>>>> "make": "Tonda",
>>>> "name": "Car",
>>>> "model": "Tmaze"
>>>> },
>>>> {
>>>> "id": 3,
>>>> "make": "Tamsung",
>>>> "name": "Mobile",
>>>> "model": "S8"
>>>> }]
>>>> }
>>>>
>>>>
>>>> Desired Output :
>>>>
>>>> {
>>>> "id": 1,
>>>> "make": "TAHB",
>>>> "children": [
>>>> {
>>>> "kid": 1
>>>> },
>>>> {
>>>> "kid": 2
>>>> },
>>>> {
>>>> "id": 2,
>>>> "make": "Tonda",
>>>> "name": "Car",
>>>> "model": "Tmaze"
>>>> },
>>>> {
>>>> "id": 3,
>>>> "make": "Tamsung",
>>>> "name": "Mobile",
>>>> "model": "S8"
>>>> }
>>>> ],
>>>> "name": "Home",
>>>> "area": "5000sqft",
>>>> }
>>>>
>>>> Please find main xml and backendService xml attached. It contains what
>>>> I tried.
>>>> main xml : addChild.xml
>>>> backendService xml : modifyAgrRes.xml
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Dev mailing list
>>>> Dev@wso2.org
>>>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>>>
>>>>
>>>
>>>
>>> --
>>> Sudharma Subasinghe,
>>> Software Engineer,
>>> WSO2 Inc.
>>> Email: sudhar...@wso2.com <hasi...@wso2.com>
>>> Mobile : +94 710 565 157 <%2B94%20718%20210%20200>
>>>
>>
>>
>
>
> --
> Sudharma Subasinghe,
> Software Engineer,
> WSO2 Inc.
> Email: sudhar...@wso2.com <hasi...@wso2.com>
> Mobile : +94 710 565 157 <%2B94%20718%20210%20200>
>
> _______________________________________________
> Dev mailing list
> Dev@wso2.org
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>
--
Himasha Guruge
*Software Engineer*
WS*O2* *Inc.*
Mobile: +94 777459299
himas...@wso2.com
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<jsonObject>
<xsl:apply-templates />
</jsonObject>
</xsl:template>
<xsl:template match="Information">
<xsl:for-each select="jsonObject">
<xsl:if test="id=1">
<xsl:copy-of select="*" />
</xsl:if>
<xsl:if test="id>1">
<children>
<xsl:copy-of select="*" />
</children>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev