Skip to main content Skip to complementary content

Grouping loop iterations

Use a SimpleLoop function with a Distinct Child Element property to group loop iterations based on the value of a child element.

Before you begin

You have created a map with an input and an output structure. You can use the samples provided below.

About this task

In this example, you have an XML file containing information about orders. It contains several item elements, some of which belong to the same order. The goal is to create an XML file with a single entry per order containing the order ID, shipping information and list of items. You can use the following XML sample as input:
<orders>
  <item>
    <itemId>1268168</itemId>
    <orderId>45632</orderId>
    <customerName>John Smith</customerName>
    <shipping>
      <street>South Highway</street>
      <city>San Diego</city>
      <state>California</state>
    </shipping>
  </item>
  <item>
    <itemId>1684157</itemId>
    <orderId>45632</orderId>
    <customerName>John Smith</customerName>
    <shipping>
      <street>South Highway</street>
      <city>San Diego</city>
      <state>California</state>
    </shipping>
  </item>
  <item>
    <itemId>1168468</itemId>
    <orderId>46987</orderId>
    <customerName>Jane Harrison</customerName>
    <shipping>
      <street>Bailard Avenue</street>
      <city>Los Angeles</city>
      <state>California</state>
    </shipping>
  </item>
  <item>
    <itemId>1544417</itemId>
    <orderId>46987</orderId>
    <customerName>Jane Harrison</customerName>
    <shipping>
      <street>Bailard Avenue</street>
      <city>Los Angeles</city>
      <state>California</state>
    </shipping>
  </item>
  <item>
    <itemId>1168468</itemId>
    <orderId>47949</orderId>
    <customerName>Jack Monroe</customerName>
    <shipping>
      <street>Steele Lane</street>
      <city>Las Vegas</city>
      <state>Nevada</state>
    </shipping>
  </item>
  <item>
    <itemId>1388147</itemId>
    <orderId>47949</orderId>
    <customerName>Jack Monroe</customerName>
    <shipping>
      <street>Steele Lane</street>
      <city>Las Vegas</city>
      <state>Nevada</state>
    </shipping>
  </item>
</orders>
You can use the following XML sample to create your output structure:
<orders>
	<order>
		<orderId/>
		<customerName/>
		<shipping>
			<street/>
			<city/>
			<state/>
		</shipping>
		<items>
			<itemId/>
			<itemId/>
		</items>
	</order>
	<order>
		<orderId/>
		<customerName/>
		<shipping>
			<street/>
			<city/>
			<state/>
		</shipping>
		<items>
			<itemId/>
			<itemId/>
		</items>
	</order>
</orders>

Procedure

  1. Drag and drop the input item element on the output order element to map their child elements.
    A SimpleLoop function is automatically added. If you use the Test Run option at this step, the output contains one order element for each iteration of the input item element.
  2. Double-click the SimpleLoop function to open its properties.
  3. Select orderId in the Distinct Child Element drop-down list and click OK.
    This option allows you to specify that each orderId value should appear only once in the output.
  4. Drag and drop the input itemId element on the corresponding output.
  5. Open the Loop tab for the output itemId element and drag and drop a SimpleLoop function.
  6. Drag and drop the input item element in the Input Map Element argument.
  7. Drag and drop an Equal function on the Filter argument.
  8. Drag and drop the input orderId element on the First Value argument and the output orderId on Second Value.
    This expression allows you to return all itemId elements associated with the current iteration of the orderId element.

Results

Your map in now configured. You can use the Test Run feature to see the result. In this example, the following output is returned:
<orders>
  <order>
    <orderId>45632</orderId>
    <customerName>John Smith</customerName>
    <shipping>
      <street>South Highway</street>
      <city>San Diego</city>
      <state>California</state>
    </shipping>
    <items>
      <itemId>1268168</itemId>
      <itemId>1684157</itemId>
    </items>
  </order>
  <order>
    <orderId>46987</orderId>
    <customerName>Jane Harrison</customerName>
    <shipping>
      <street>Bailard Avenue</street>
      <city>Los Angeles</city>
      <state>California</state>
    </shipping>
    <items>
      <itemId>1168468</itemId>
      <itemId>1544417</itemId>
    </items>
  </order>
  <order>
    <orderId>47949</orderId>
    <customerName>Jack Monroe</customerName>
    <shipping>
      <street>Steele Lane</street>
      <city>Las Vegas</city>
      <state>Nevada</state>
    </shipping>
    <items>
      <itemId>1168468</itemId>
      <itemId>1388147</itemId>
    </items>
  </order>
</orders>

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!