Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WSO2 ESB foreach function

In WSO2 ESB Proxy service, how can i iterate based on integer value from some webservice response, just like "foreach":

For example such response message:

<Response>
   <noOfcustomers>10</noOfCustomers>
</Response>

I need to iterate 10 times (based on the number of customers)

Is this possible? How can i achieve this?

Thanks for your help!

like image 439
user2400243 Avatar asked Nov 03 '22 21:11

user2400243


1 Answers

I've not found a clean way to do this, but here's a messy solution.

First you need an XSLT transformation.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsl xsi">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:param name="iterations"/>
    <xsl:template name="for.loop">
        <xsl:param name="i"/>
        <xsl:param name="count"/>
        <!--begin_: Line_by_Line_Output -->
        <xsl:if test="$i &lt;= $count">
            <iteration>
                <xsl:value-of select="$i"/>
            </iteration>
        </xsl:if>
        <!--begin_: RepeatTheLoopUntilFinished-->
        <xsl:if test="$i &lt;= $count">
            <xsl:call-template name="for.loop">
                <xsl:with-param name="i">
                    <xsl:value-of select="$i + 1"/>
                </xsl:with-param>
                <xsl:with-param name="count">
                    <xsl:value-of select="$count"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <xsl:template match="/">
        <iterations>
            <xsl:call-template name="for.loop">
                <xsl:with-param name="i">1</xsl:with-param>
                <xsl:with-param name="count"><xsl:value-of select="$iterations"/></xsl:with-param>
            </xsl:call-template>
        </iterations>
    </xsl:template>
</xsl:stylesheet>

Then you use the transformation in your sequence like this:

<inSequence>
    <xslt key="conf:/repository/test/iterations.xslt">
        <property name="iterations" expression="//noOfcustomers"/>
    </xslt>
    <iterate expression="//iterations/iteration" sequential="true">
        <target>
          <sequence>

          </sequence>
        </target>
    </iterate>
</inSequence>

The sequence in the iterate mediator will run for each element in "iterations". The drawback to this approach is that you are replacing the message body with the iteration XML, so you have to use the enrich meditor before the transformation to save the original message to a property if you wish to reuse it.

like image 91
Chris Avatar answered Nov 14 '22 23:11

Chris