The ForEach mediator provides a general loop construct as a mediator. It allows a message to be processed iteratively while maintaining the original message context. The ForEach mediator split the message into a number of different messages derived from the parent message, by finding matching elements for the XPath expression that is specified. New messages will be created for all each of the matching elements and processed in sequence within the main flow using specified sequence. However, Call, CallOut or Send mediators are not allowed in the sequence. The original message will be modified by replacing the split elements with the elements that go through the loop construct.
Figure1: ForEach Mediator
ForEach Mediator can be configured with the following options.Both these properties are defined in default (synapse) scope. Defining ForEach ID is optional. If not defined the properties will be defined without the prefix. However, in a nested foreach scenario it is best to define the ForEach ID to avoid properties getting overwritten.