Sorting with Variables

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Designing Mappings > Sorting Data >

Sorting with Variables

In some cases, it may be necessary to add intermediate variables to the mapping in order to achieve the desired result. This example illustrates how to extract records from an XML file, and sort them, with the help of intermediate variables. The example is accompanied by a mapping sample located at the following path: <Documents>\Altova\MapForce2019\MapForceExamples\Altova_Hierarchical_Sort.mfd.

sort-altHier1

Altova_Hierarchical_Sort.mfd

This mapping reads data from a source XML file called Altova_Hierarchical.xml and writes it to a target XML file. As shown above, the source XML contains information about a fictitious company. The company is divided into offices. Offices are sub-divided into departments, and departments are further divided into people.

 

The target XML component, PersonList, contains a list of Person records. The Details item is meant to store information about the office and department where the person belongs.

 

The aim is to extract all persons from the source XML and sort them alphabetically by last name. Also, the office and department name where each person belongs must be written to the Details item.

 

To achieve this goal, this example makes use of the following component types:

 

1.The concat function. In this mapping, this function returns a string in the format Office(Department). It takes as input the office name, the department name, and two constants which supply the start and end brackets. See also Working with Functions.
2.An intermediate variable. The role of the variable is to bring all data relevant to a person into the same mapping context. The variable causes the mapping to look up the department and office of each person, in the context of each person. To put it differently, the variable "remembers" the office and department name to which a person belongs. Without the variable, the context would be incorrect, and the mapping would produce unwanted output (for more information about how a mapping is executed, see Mapping Rules and Strategies). Notice that the variable replicates the structure of the target XML file (it uses the same XML schema). This makes it possible to connect the sort result to the target, through a Copy-All connection. See also Using Variables and Copy-All Connections.
3.A Sort component, which performs the actual sorting. Notice that the key input of the Sort component is connected to the Last item of the variable, which sorts all person records by their last name.

© 2019 Altova GmbH