Please enable JavaScript to view this site.

Altova MapForce 2020 Professional Edition

Designing Mappings > Using Variables

Example: Grouping and Subgrouping Records

Scroll Home Prev Top Next More

The mapping illustrated in this example is available as DividePersonsByDepartmentIntoGroups.mfd in the <Documents>\Altova\MapForce2020\MapForceExamples\ folder.

 

This mapping processes an XML file that contains employee records of a fictitious company. The company has two offices: "Nanonull, Inc." and "Nanonull Partners, Inc". Each office has several departments (for example, "IT", "Marketing", and so on), and each department has one or more employees. The goal of the mapping is to create groups of maximum three people from each department, regardless of the office. The size of each group is three by default; however, it should be easy to change if necessary. Each group must be saved as a separate XML file, with the name having the format "<Department Name>_GroupN" (for example, Marketing_Group1.xml, Marketing_Group2.xml, and so on).

mf_map_DividePersons

DividePersonsByDepartmentIntoGroups.mfd

As illustrated above, in order to achieve the mapping goal, a complex variable was added to the mapping, and a few other component types (primarily functions). The variable has the same structure as a Department item in the source XML. If you right-click the variable in order to view its properties, you will notice that it uses the same XML schema as the source component, and has Department as root element. Importantly, the variable has two nested parent-context items, which ensure that the variable is computed first in the context of each department, and then in the context of each group within each department (see also Changing the Context and Scope of Variables).

 

Initially, the mapping iterates through all departments in order to obtain the name of each department (this will be subsequently required to create the file name corresponding to each group). This is achieved by connecting the group-by function to the Department source item, and by supplying the department name as grouping key.

 

Next, within the context of each department, a second grouping takes place. Namely, the mapping calls the group-into-blocks function in order to create the required groups of people. The size of each group is supplied by a simple input component which has a default value of "3". The default value is supplied by a constant. In this example, in order to change the size of each group, one can easily modify the constant value as required. However, the "size" input component can also be modified so that, if the mapping is run by generated code or with MapForce Server, the size of each group could be conveniently supplied as a parameter to the mapping. For more information, see Supplying Parameters to the Mapping.

 

Next, the value of the variable is supplied to the target PersonList XML component. The file name for each created group was computed by concatenating the following parts, with the help of the concat function:

 

1.The name of each department

2.The string "_Group"

3.The number of the group in the current sequence (for example, "1" if this is the first group for this department)

4.The string ".xml"

 

The result of this concatenation is stored in the Name item of the variable, and then supplied as a dynamic file name to the target component. This causes a new file name to be created for each received value. In this example, the variable computes eight groups in total, so eight output files are created when the mapping runs, as required. For more information about this technique, see Processing Multiple Input or Output Files Dynamically.

© 2019 Altova GmbH