user2400243
user2400243

Reputation: 51

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!

Upvotes: 5

Views: 3086

Answers (3)

maheeka
maheeka

Reputation: 2093

This is supported with ForEach mediator from ESB 4.9 onwards

Upvotes: 0

Chris
Chris

Reputation: 163

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.

Upvotes: 2

Ratha
Ratha

Reputation: 9692

You can iterate based on xpath. But currently we don't have counter support. What is your actual usecase?

Upvotes: 0

Related Questions