Go through occurrences

In the previous chapter, you saw an example that compounded the components of an address into a single string. This was accomplished using a basic loop and a path containing the bracket operator to retrieve a single occurrence of a node.

While this method works fine, it is very costly in terms of system resources. This is because the bracket operator is handled individually by the integration engine. As a result, when the integration engine comes to pick up the n th occurrence of a node, it does not remember that it just read the preceding (n‑1) th occurrence. Therefore, it restarts the search from the first. When processing large numbers of occurrences, this requires to a great number of operations.

The loop on statement instructs the integration engine to go through all existing occurrences of a specified node and perform some task for each one. You can translate the previous example using the more efficient loop on:

1   %address := "";
2   loop on \\address\lines
3   {
4       %address := %address + this + "\n";
5   };
6   %address := %address + \\address\zip + " "
7                        + \\address\city;
8   %address

In this example, line 2 tells the integration engine to go through all occurrences of \\address\lines node, one by one. For each occurrence, the set of statements contained between the curly brackets { } is evaluated.

In this set of statements, you can use the reserved word this to refer to the current occurrence: this is invoked in line 4 to append the contents of the current line to the address variable. The this keyword always starts a path to a node and cannot be filtered using either the brackets operator or where. However, paths starting with this can address any node in the Business Document: you can use the parent operator .., and single and double backslash connectors to move all around the node tree.

As with loops using loop and while instructions, you can invoke the next and break instructions inside a loop on statement. They operate by either jumping directly to the next occurrence or interrupting the loop. To ignore blank lines, you change the previous expression to:

1   %address := "";
2   loop on \\address\lines
3   {
4       if (length(this) < 1) then next;
5       %address := %address + this + "\n";
6   };
7   %address := %address + \\address\zip + " "
8                        + \\address\city;
9   %address

Another way to handle blank lines is to filter the lines directly in the loop on instruction argument. This works because the loop on instruction has the capacity to go through any set of occurrences of nodes addressed by any legal path. This means you can use paths that include where clauses or bracket operators, or that address from none to any occurrences. The previous example could look similar to this:

1   %address := "";
2   loop on \\address\lines[where length(item) <> 0]
3   {
4       %address := %address + this + "\n";
5   };
6   %address := %address + \\address\zip + " "
7                        + \\address\city;
8   %address

The set of statements bound to the loop on instruction is repeated only for each lines node that is not blank (length is not zero).

You can also use ambiguous paths. All the occurrences of all the addressed nodes are covered.

In EDIFACT/Invoice documents, segments appear in several segment groups (references, documentary requirements, currencies…). As a result, several nodes with the name DTM appear in the prebuilt EDIFACT Business Documents provided in the integration engine. If you want to perform an operation on all these segments, you can use loop on:

loop on \\DTM
{
   …
}

This statement first evaluates the enclosed set of statements for the DTM segments in the header of the EDIFACT document, then for the DTM segments of segment group 1, then for those of segment group 3… until the very last DTM segment of the document (segment group 51) has been covered.

In the set of statements bound to this loop on instruction, you can use this as a reference to the current DTM. Since all DTM segments have the same structure, a path similar to this\C507\D2005 always addresses something, whatever segment group the DTM segment is included in. However, a path similar to this\..\QTY cannot be used because only segment groups 1, 33 and 49 have both DTM and QTY segments.

When looping with ambiguous paths, the only requirement is to address nodes that exist. Even if each DTM segment has different structures, looping would be possible, although only paths to nodes that are common to all the DTMs would be legal.

The loop on statement can go through any set of occurrences. You can not only use a path to a single node or to several nodes (ambiguous paths), but also specify more multiple paths. Imagine you want to go through all CTA and COM segments within a Business Document. To do this you enter the following:

loop on \\CTA, \\COM
{
   …
}

This code goes through all occurrences of all CTA segments and then all occurrences of COM segments. In the repeated set of statements, this refers first to CTA nodes and then to COM nodes.

Similar to standard loops and while loops, loop on statements can include another loop inside the set of statements they repeat. In such a situation, the keyword this always refers to the inner loop.

Related Links