IBM Support

Foreach iteration counter in an XML Map

Question & Answer


Question

I am using the foreach transformation in an XML map to copy data from one array type to another. How do I access and use the iteration counter?

Answer

The foreach iteration counter can be read using the position() XPath function. To use it in your XML map create a custom XPath transform.

For example, say you want to insert the string "This is element number 1" in the first array element processed by the foreach, "This is element number 2" in the second array element and so on.

In the map inside the foreach transform, click on any field on the left side and drag it to the target field (a string type in this case) on the right. A Move transform is created. Click the triangle beside Move and navigate to Custom Transforms>Custom XPath. This changes the Move into a Custom XPath. In the properties view select the General tab and type



concat('This is element number ',position())

When you run this map on the server the target field of each element in the output array will contain the string described above.

[{"Product":{"code":"SSQQFK","label":"WebSphere Integration Developer"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Product Usage","Platform":[{"code":"PF016","label":"Linux"},{"code":"PF033","label":"Windows"}],"Version":"7.0.0.5;7.0.0.4;7.0.0.3;7.0.0.2;7.0.0.1;7.0","Edition":"All Editions","Line of Business":{"code":"LOB45","label":"Automation"}},{"Product":{"code":"SSTLXK","label":"IBM Integration Designer"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Product Usage","Platform":[{"code":"PF033","label":"Windows"},{"code":"PF016","label":"Linux"}],"Version":"7.5;7.5.0.1;7.5.1;7.5.1.1;8.0;8.0.1;8.0.1.1;8.0.1.2;8.5;8.5.0.1","Edition":"Advanced","Line of Business":{"code":"LOB45","label":"Automation"}}]

Document Information

Modified date:
15 June 2018

UID

swg21450489