The Loop action (see screenshot below) iterates over a sequence of items that you define using the For Each setting. Within the loop you can then define a set of actions to perform during every iteration. For example, in the screenshot below, for each iteration, an Append to Node action is carried out. Also inside the loop, a Break Loop action is specified if a certain condition is fulfilled.
The sequence over which the loop iterates is defined by the XPath expressions of the For Each setting. The key points to note are given below.
•For Each: Can be a sequence named in the XPath expression (for example: 1 to 7) or one obtained from an XML tree (for example: $XML1/Products/Product selects a sequence of all Product elements in the $XML1 tree; see screenshot above). If the loop does not contain a Break Loop action, then the loop is exited when all iterations have been completed.
•Loop variable: The loop variable is the variable that holds the item of the sequence that is currently being iterated over. The loop variable is identified by a name, which you enter by first double-clicking after the $ sign and then typing the name. In the screenshot above, the loop variable is named MyLoop. It is referenced like any other XPath variable, that is, with a $ sign before its name ($MyLoop). The variable will be in scope within the loop; this means that you cannot reference the variable in an XPath expression that is outside the loop. In the screenshot above, the loop variable is referenced in the New Node setting of the Append to Node action. This is a valid reference since the Append to Node action has been created within the loop; the variable is therefore in scope at this point. The $MyLoop variable shown in the screenshot above will contain the Product node that is currently being iterated over.
|Note:||If the loop variable uses nodes from a page source tree, then this tree is locked and cannot be modified by the actions inside the Loop action. In our example above, the tree $XML1 will be locked while the loop is being processed. The modifications are happening in another page source tree ($XML2): the new nodes that are added with the Append to Node action are added to $XML2. If you want to modify the tree being iterated over, you can do this as follows: Instead of iterating directly over the nodes of the tree, iterate over a number sequence that is tied to the node sequence in the tree. For example, instead of iterating over the sequence of Product nodes in the screenshot example above, we can iterate over a range of numbers that is tied to the node sequence. The XPath expression of the For Each setting can be changed from $XML1/Products/Product to for $i in 1 to count($XML1/Products/Product) return $i. It is a sequence of numbers that is now being iterated over. (The current Product node within the loop can be accessed with the XPath expression: $XML1/Products/Product[$i]).|
The actions defined in the screenshot above duplicates the first five Product elements in another tree. When the page is loaded, the Loop action iterates over the $XML1/Products/Product elements. During each iteration, the current Product node is stored in the MyLoop variable. This Product node (in the $MyLoop variable) is then added as the last child of the $XML2/Products node. The loop continues till the fifth Product element has been copied from $XML1/Products to $XML2/Products. See screenshot below.
The loop is exited either when all iterations have been completed or a condition is set under which a Break Loop action is executed (see next topic).
MobileTogether provides a range of XPath extension functions that have been specifically created for use in MobileTogether designs. Some functions can be particularly useful with specific actions. For example, mt-available-languages() returns the languages in which the solution is available and could, for example, be used with the Message Box action. If a function is especially relevant to this action, it is listed below. For a full list of extension functions and their descriptions, see the topic MobileTogether Extension Functions.