This documentation is for WSO2 Enterprise Service Bus version 5.0.0. For the latest ESB, view the latest WSO2 Enterprise Integrator documentation.

All docs This doc
Skip to end of metadata
Go to start of metadata


This sample demonstrates how the ForEach mediator can be used to transform a payload.


  • To invoke the main sequence using curl, a request file should be created as shown below. This file is named  stockQuoteReq.xml in this example.

    <soap:Envelope xmlns:soap="" xmlns:m0="http://services.samples" xmlns:xsd="http://services.samples/xsd">
  • See Prerequisites to Start the ESB Samples for other prerequisites.

Building the sample

The XML configuration for this sample is as follows.

<sequence xmlns="" name="main">
  	<foreach xmlns:ns="http://org.apache.synapse/xsd" xmlns:m0="http://services.samples" expression="//m0:getQuote/m0:request">
           	<payloadFactory media-type="xml">
                 	<arg expression="//m0:request/m0:symbol" evaluator="xml"/>
  	<log level="full"/>

This configuration file synapse_sample_18.xml is available in the <ESB_HOME>/repository/samples directory.

To build the sample, start the ESB with the sample 18 configuration. For instructions on starting a sample ESB configuration, see Starting the ESB with a sample configuration.
The operation log keeps running until the server starts, which usually takes several seconds. Wait until the server has fully booted up and displays a message similar to "WSO2 Carbon started in n seconds."

Executing the sample

Invoke the main sequence using the following command. This command should be issued from the same location in which the  stockQuoteReq.xml request file you created is saved.

curl -d @stockQuoteReq.xml -H "Content-Type: application/soap+xml;charset=UTF-8" "http://localhost:8280/"

Analyzing the output

The ForEach mediator splits the message to four different elements based on the evaluation of the //m0:getQuote/m0:request expression. The PayloadFactory mediator produces a result as shown in the following console log.

<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:m0="http://services.samples" xmlns:soap="" xmlns:xsd="http://services.samples/xsd">

 <m0:request><m0:symbol>IBM</m0:symbol></m0:request> in the request has been changed to <m0:checkPriceRequest><m0:code>IBM</m0:code></m0:checkPriceRequest>.

The expression can be changed to perform a transformation or similar selectively. For example if the expression is given as expression = "//m0:getQuote/m0:request[1]", only the first element will be transformed and the other elements will be left unchanged.

For more information on XPath expressions, see XPath Syntax by

  • No labels