Reusing an existing loop expression - 7.3

Talend Data Mapper Functions Reference Guide

Version
7.3
Language
English
Product
Talend Big Data Platform
Talend Data Fabric
Talend Data Management Platform
Talend Data Services Platform
Talend MDM Platform
Talend Real-Time Big Data Platform
Module
Talend Studio
Content
Design and Development > Designing Jobs
Last publication date
2023-01-05

Use a LoopReference function to reuse a loop expression previously defined in another 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 customer information. Each customer element contains the customer's name and their state. You want to create an XML output in which the customers are grouped by state. You can use the following XML sample as input:
<customers>
	<customer>
		<lastName>Harrison</lastName>
		<firstName>Emily</firstName>
		<state>New Jersey</state>
	</customer>
	<customer>
		<lastName>Reagan</lastName>
		<firstName>James</firstName>
		<state>New Jersey</state>
	</customer>
	<customer>
		<lastName>Smith</lastName>
		<firstName>Karen</firstName>
		<state>Texas</state>
	</customer>
</customers>
You can use the following XML sample to create your output structure:
<root>
	<customers>
		<state/>
		<customer>
			<lastName/>
			<firstName/>
		</customer>
		<customer>
			<lastName/>
			<firstName/>
		</customer>
	</customers>
	<customers>
		<state/>
		<customer>
			<lastName/>
			<firstName/>
		</customer>
		<customer>
			<lastName/>
			<firstName/>
		</customer>
	</customers>
</root>

Procedure

  1. Right-click the output customers element and click Split Loop to create a separate loop for each state value.
  2. Click the first customers loop in the output structure and drag and drop a FixedLoop function in its Loop tab.
    This is used to indicate that the output should only produce one iteration of this loop.
  3. Drag and drop a Constant function on the first output state element, then double-click the function, enter New Jersey in the Value field and click OK.
  4. Drag and drop the input customer element on the first output customer element.
    A SimpleLoop function is automatically added.
  5. Drag and drop an Equal function on the Filter argument of the SimpleLoop.
  6. Drag and drop the input state element on the First Value argument and the first output state on the Second Value.
  7. Click the customers[2] element and drag and drop a FixedLoop function in its Loop tab.
  8. Drag and drop a Constant function on the second output state element and set its value to Texas.
  9. Click the second output customer loop and drag and drop a LoopReference function in its Loop tab.
  10. Drag and drop the first output customer loop on the Map Element argument.
    This allows you to specify that the loop used in this element should be the same as the one defined in the first customer element. Unlike the LoopCopy function, the LoopReference does not apply the filters or sort keys defined in the referenced loop.
  11. Add an Equal function to the Filter argument and set the input state as First Value and the second output state as Second Value.
  12. Drag and drop the input customer element on the second output customer to map the child elements.

Results

Your map is configured and should look like this:
You can use the Test Run feature to see the result. In this example, the following output is returned:
<root>
  <customers>
    <state>New Jersey</state>
    <customer>
      <lastName>Harrison</lastName>
      <firstName>Emily</firstName>
    </customer>
    <customer>
      <lastName>Reagan</lastName>
      <firstName>James</firstName>
    </customer>
  </customers>
  <customers>
    <state>Texas</state>
    <customer>
      <lastName>Smith</lastName>
      <firstName>Karen</firstName>
    </customer>
  </customers>
</root>