The ForEach mediator requires an XPath/JSONPath expression and a sequence (inline or referred). It splits the message into a number of different messages derived from the original message by finding matching elements for the XPath/JSONPath expression specified. Based on the matching elements, new messages are created for each iteration and processed sequentially. The processing is carried out based on a specified sequence. The behaviour of ForEach mediator is similar to a generic loop. After mediation, the sub-messages are merged back to their original parent element in the original message sequentially.
The ForEach mediator creates the following properties during mediation.
|FOREACH_ORIGINAL_MESSAGE||This contains the original envelop of the messages split by the ForEach mediator.|
|FOREACH_COUNTER||This contains the count of the messages processed. The message count increases during each iteration.|
Iterate Mediator is quite similar to the ForEach mediator. You can use complex XPath expressions to conditionally select elements to iterate over in both mediators. Following are the main difference between ForEach and Iterate mediators:
- Use the ForEach mediator only for message transformations. If you need to make back-end calls from each iteration, then use the iterate mediator.
- ForEach supports modifying the original payload. You can use Iterate for situations where you send the split messages to a target and collect them by an Aggregate in a different flow
- You need to always accompany an Iterate with an Aggregate mediator. ForEach loops over the sub-messages and merges them back to the same parent element of the message.
- In Iterate you need to send the split messages to an endpoint to continue the message flow. However, ForEach does not allow using Call, Send and Callout mediators in the sequence.
- ForEach does not split the message flow, unlike Iterate Mediator. It guarantees to execute in the same thread until all iterations are complete.
When you use ForEach mediator, you can only loop through segments of the message and do changes to a particular segment. For example, you can change the payload using payload factory mediator. But you cannot send the split message out to a service. Once you exit from the ForEach loop, it automatically aggregates the split segments. This replaces the ForEach function of the complex XSLT mediators using a ForEach mediator and a Payload Factory mediator. However, to implement the split-aggregate pattern, you still need to use Iterate mediator.
<foreach expression="xpath|jsonpath" [sequence="sequence_ref"] [id="foreach_id"] > <sequence> (mediator)+ </sequence>? </foreach>
The parameters available to configure the ForEach mediator are as follows.
|ForEach ID||If a value is entered for this parameter, it will be used as the prefix for the
The XPath/JSONPath expression with which different messages are derived by splitting the parent message. This expression should have matching elements based on which the splitting is carried out.
You can click NameSpaces to add namespaces when you are providing an expression. Then the Namespace Editor panel would appear where you can provide any number of namespace prefixes and URLs used in the XPath expression.
The mediation sequence that should be applied to the messages derived from the parent message. ForEach mediator is used only for transformations, thereby, you should not include Call , Send and Callout mediators, which are used to invoke endpoints, in t his sequence.
You can select one of the following options.
In this configuration, the
"json-eval($.getQuote.request)" JSONPath expression evaluates the split messages to be derived from the
parent message. Then the split messages pass through a sequence which
includes a Log mediator with the log level set to
<foreach id="foreach_1" expression="//m0:getQuote/m0:request" xmlns:m0="http://services.samples"> <sequence> <log level="full"/> </sequence> </foreach>
<foreach id="foreach_1" expression="json-eval($.getQuote.request)"> <sequence> <log level="full"/> </sequence> </foreach>