This documentation is for WSO2 Enterprise Service Bus version 4.9.0 . View documentation for the latest release.

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

Introduction

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

Prerequisites

  • 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="http://www.w3.org/2003/05/soap-envelope" xmlns:m0="http://services.samples" xmlns:xsd="http://services.samples/xsd">
    	<soap:Header/>
    	<soap:Body>
        	<m0:getQuote>
            	<m0:request><m0:symbol>IBM</m0:symbol></m0:request>
            	<m0:request><m0:symbol>WSO2</m0:symbol></m0:request>
            	<m0:request><m0:symbol>MSFT</m0:symbol></m0:request>
        	</m0:getQuote>
    	</soap:Body>
    </soap:Envelope>
  • See Prerequisites to Start the ESB Samples for other prerequisites.

Building the sample

The XML configuration for this sample is as follows.

<definitions>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="main">
   <in>
  	<foreach xmlns:ns="http://org.apache.synapse/xsd" xmlns:m0="http://services.samples" expression="//m0:getQuote/m0:request">
        	<sequence>
           	<payloadFactory media-type="xml">
              	<format>
                 	<m0:checkPriceRequest>
                    	<m0:code>$1</m0:code>
                 	</m0:checkPriceRequest>
              	</format>
              	<args>
                 	<arg expression="//m0:request/m0:symbol" evaluator="xml"/>
              	</args>
           	</payloadFactory>
        	</sequence>
  	</foreach>
  	<log level="full"/>
   </in>
   <out/>
</sequence>
</definitions>

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="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://services.samples/xsd">
    <soap:Body>
   	 <m0:getQuote>
   		 <m0:checkPriceRequest><m0:code>IBM</m0:code></m0:checkPriceRequest>
   		 <m0:checkPriceRequest><m0:code>WSO2</m0:code></m0:checkPriceRequest>
   		 <m0:checkPriceRequest><m0:code>MSFT</m0:code></m0:checkPriceRequest>
   	 </m0:getQuote>
    </soap:Body>
</soap:Envelope>

 <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 w3schools.com.

  • No labels