'ESB WSO2 Adding a property into a XML file using Enrich, Iterate and Filter mediator

I want to sent a Postman request with a xml file like this to my api:

<data>
    <user>
        <name>Andrei</name>
        <adress>Tudor Vladimirescu, nr2</adress>
        <cnp>123456789</cnp>
        <age>22</age>
    </user>
    <user>
        <name>Ana</name>
        <adress>Str. Pacurari, nr1</adress>
        <cnp>123456789</cnp>
        <age>26</age>
    </user>
    <user>
        <name>Andreea</name>
        <adress>Tudor Vladimirescu, nr1</adress>
        <cnp>123456789</cnp>
        <age>20</age>
    </user>
</data>

And I want to add a <valid>Yes/No</valid> tag to each <user> based on the age. For example: if the age is above 23, the value of the valid tag should be Yes. I want to send the modified message to another API. I know that I should use Enrich, Iterate and Filter mediators but I really need some help because I'm a beginner. Thank you!



Solution 1:[1]

I have used Iterate mediator as you need backend call and filter mediator used to set age is valid or not for each element and finally used enrich mediator to achieve your usecase.

<!-- iteration of incoming user data -->
            <iterate expression="//data/user" id="iterateuser" sequential="true">
                <target>
                    <sequence>
                        <property expression="//age/text()" name="age" scope="default" type="STRING"/>
                         <log level="custom">
                <property name="Inside Iterator" value="is called*****"/>
                <property expression="get-property('age')" name="age"/>
            </log>
             <!-- fiter to find valid age  or not-->
                        <filter xpath="get-property('age')>='23'">
                            <then>
                                <property name="valid" scope="default" type="STRING" value="Yes"/>
                            </then>
                            <else>
                                <property name="valid" scope="default" type="STRING" value="No"/>
                            </else>
                        </filter>
                         <!-- enrich to add valid tag to existing payload-->
                        <enrich>
                            <source clone="true" type="inline">
                                <valid xmlns=""/>
                            </source>
                            <target action="child" xmlns:ns="http://www.wso2.org/types" xpath="//user"/>
                        </enrich>
                        <!-- enrich to add valid property value with validtag-->
                        <enrich>
                            <source clone="true" property="valid" type="property"/>
                            <target xpath="//valid"/>
                        </enrich>
                        <log level="full"/>
                        <!-- make an external endpoint call here -->
                    </sequence>
                </target>
            </iterate>

Input:

<data>
    <user>
        <name>Andrei</name>
        <adress>Tudor Vladimirescu, nr2</adress>
        <cnp>123456789</cnp>
        <age>22</age>
    </user>
    <user>
        <name>Ana</name>
        <adress>Str. Pacurari, nr1</adress>
        <cnp>123456789</cnp>
        <age>26</age>
    </user>
    <user>
        <name>Andreea</name>
        <adress>Tudor Vladimirescu, nr1</adress>
        <cnp>123456789</cnp>
        <age>20</age>
    </user>
</data>

Log for iteration:

[2022-03-15 12:27:23,032]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 22
[2022-03-15 12:27:23,032]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:8101fdcc-0a29-4bea-a8f1-b8ad80f0c6ff, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
        <name>Andrei</name>
        <adress>Tudor Vladimirescu, nr2</adress>
        <cnp>123456789</cnp>
        <age>22</age>
    <valid>No</valid></user></soapenv:Body></soapenv:Envelope>
[2022-03-15 12:27:23,032]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 26
[2022-03-15 12:27:23,040]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:b01f8b29-9fe5-43e6-a0ed-0a61d395fd1a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
        <name>Ana</name>
        <adress>Str. Pacurari, nr1</adress>
        <cnp>123456789</cnp>
        <age>26</age>
    <valid>Yes</valid></user></soapenv:Body></soapenv:Envelope>
[2022-03-15 12:27:23,053]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - Inside Iterator = is called*****, age = 20
[2022-03-15 12:27:23,053]  INFO {org.apache.synapse.mediators.builtin.LogMediator} - To: /enrichapi, MessageID: urn:uuid:1feeb723-8a72-475a-b014-cda2e7b7a90a, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><user>
        <name>Andreea</name>
        <adress>Tudor Vladimirescu, nr1</adress>
        <cnp>123456789</cnp>
        <age>20</age>
    <valid>No</valid></user></soapenv:Body></soapenv:Envelope>

If you noticed above log <valid>No</valid> or <valid>Yes</valid> is added to payload.

Post this, you can make external endpoint call inside iterator.

Ref: Enrich Mediator

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Justin