# Designing For Each Nodes

Before you can add the logic that causes the iteration over a sequence of XML nodes in your business process, your project must contain an XML Schema or MFL file that defines the repeating XML or MFL element over which you want your business process to iterate. To learn how to import an XML Schema or MFL file into your project, see Importing Files into the Schemas Project.

After importing an XML Schema or MFL file into your project, you can complete the design of the For Each node. It includes the following tasks:

- To Select a Repeating XML or MFL Element Over Which to Iterate
- To Add Activities to the For Each Node

To Select a Repeating XML or MFL Element Over Which to Iterate

The For Each node only iterates over repeating elements. The node builder allows you to select a repeating node from the variable you created in the preceding section.

1. In the Design View, double-click the For Each node to invoke its node builder.
2. Click Select Variable to select a variable that you have already created in your project or create a new variable to use in your decision node:

1. If you want to use a variable that is already created, select the variable that contains the XML or typed non-XML on which you want to build the condition.

For example, if we import an XML Schema (QuoteRequest.xsd) into our project, and create a business process variable (requestXML) of type quoteRequest (based on the QuoteRequest.xsd schema), the requestXML variable is available in the drop-down list of business process variables:

Note: To learn about creating business process variables and importing schemas to your project, see Business Process Variables and Data Types and Importing Files into the Schemas Project.

Go to step 3.

2. If you want to create a new variable, select Create new variable... from the drop-down list.

The Create Variable dialog box opens.

3. Enter a name for your new variable in the Variable Name field.
4. Select the XML or nonXML option, depending on if your variable is based on a XML document or MFL file and select the appropriate variable type in the displayed list of type options.
5. Click OK.

The Create Variable dialog box closes and your new variable is displayed in the Select Node pane.

3. Continuing with our example, a representation of the XML in the requestXML variable is displayed in the Select Node panel.

Note the following characteristics of the QuoteRequest.xsd schema as displayed in the preceding figure:

- The elements and attributes of the XML Schema are represented as nodes. Note that the Schema in the example (QuoteRequest.xsd) specifies a root element named quoteRequest.
- Child elements include: customerName, shipAddress, and widgetRequest.
- The shipAddress element specifies the following attributes: street, city, state, zip.
- The widgetRequest element is a repeating element.

There can be one or more occurrences of the widgetRequest element in an associated XML document; this is represented by in the GUI representation of the schema. The widgetRequest element, in turn, contains two elements: widgetId and quantity.

Note: In the example in the preceding figure, the repeating XML element (one or more occurrences) is represented by in the GUI representation of the schema. A repeating XML element that specifies zero or more occurrences is represented by .

4. Select a repeating element in the Select Node field.

The Repeating Element and Iteration Variable fields are populated with data:

- Repeating Element—Contains an XPath expression, which when applied against the XML document associated with the XML variable, returns the set of repeating XML elements. Building on our example, if you select the repeating element widgetRequest in the Select Node panel, the Repeating Element field is populated with the following expression: $requestXML/ns0:widgetRequest. This expression returns all the widgetRequest elements in an XML document.
- Iteration Variable—Contains the name of an iteration variable. An iteration variable is generated to hold the current element being processed in the For Each loop at run time. In our example, the iteration variable is named iter_forEach1, by default. You can change the name by entering a new name in the Iteration Variable field.

5. Click the X in the top right-hand corner to close the node builder and return to the Design View.

In the Design View, note that the icon in your For Each node displays the following graphics:

- is a visual reminder that the iteration variable you defined on this node is based on an XML element.
- is a visual reminder that the iteration variable you defined on this node is based on a MFL or typed non-XML element.

6. To save your work, select File —> Save.

To Add Activities to the For Each Node

You must define the activity or set of activities that are performed for each item in the list you created in the preceding step (To Select a Repeating XML or MFL Element Over Which to Iterate). Each iteration of the For Each loop executes the activity or activities you specify in a node (or nodes) in the loop.

1. In the Palette, click a node that represents the logic you want to add to the business process.
2. Drag and drop the node from the Palette onto the business process in the Design View, placing it on the business process within the For Each loop.

As you drag a node onto the For Each loop, a target appears on the loop, representing a valid location in the For Each loop where you can place the node. As you drag the node near the valid location, the target is activated and the cursor changes to an arrow . You can release the mouse button and the node snaps to the For Each loop.

3. On the node (or nodes) you add to the For Each loop, create the activities appropriate for your business process's business logic.

To learn about designing For Each loops and how data is assigned to variables within the loops, see Looping Through Items in a List in Tutorial: Building Your First Business Process.

Related Topics

Creating Looping Logic

Grouping Nodes in Your Business Process

Handling Exceptions

Adding Message Paths

Adding Timeout Paths