Example: Filtering and Numbering Nodes
The mapping illustrated in this example is available as PositionInFilteredSequence.mfd in the <Documents>\Altova\MapForce2023\MapForceExamples\ folder.
This mapping reads an XML file which contains contact data of several people, filters them, and writes them to a target XML file. The goal of the mapping is to filter from the source XML file only those people whose last name begins with letter "M" or a subsequent letter. Secondly, the extracted contacts must be numbered. The number is going to act as the unique identifier of each contact in the target XML file.
To achieve the goal above, the following component types were added to the mapping:
•A filter (see Filters and Conditions )
•A complex variable (see Adding Variables)
•A constant (To add a constant, select the menu command Insert | Constant).
The variable uses the same schema as the source component. If you right-click the variable and select Properties from the context menu, notice that the node BranchOffices/Office/Contact is selected as root node for this variable structure.
First, data of the source component is passed on to the filter. The filter passes onwards to the variable only those records that meet the filter condition. Namely, the filter is configured to get only those Contact nodes where the last name is equal or greater than M". To achieve this, the function greater compares each last item with the constant value "M".
The variable has the compute-when input connected to the root item of the source component (BranchOffices). At runtime, this causes the variable to be re-evaluated whenever a new item is read from the sequence in the source component. In this mapping, however, connecting or not connecting the compute-when item does not make a difference. The reason is that the variable is connected to the Contact source item (indirectly through the filter), and it would compute as many times as there are instances of Contact which meet the filter condition.
The position functions returns, for each iteration of the variable, the number of the current sequence. Only eight contacts meet the filter condition; therefore, if you preview the mapping and look at the output, notice how IDs 1 through 8 were written to the ID element of the target component.
In case you were wondering why the variable was necessary at all, it is because of the requirement to number all records. Had we connected the filter result directly to the target component, there would have been no way to number each occurrence of Contact. The purpose of the variable in this mapping is, therefore, to store each instance of Contact temporarily on the mapping, so that it can be numbered before it is written to the target.