Overriding the Mapping Context

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

Home >  Designing Mappings > Mapping Rules and Strategies >

Overriding the Mapping Context

In some mappings, in order to achieve the desired mapping output, it may be necessary to override the mapping context. For this reason, some components provide an optional parent-context item in their structure which enables you to influence the mapping context if so required. Examples of such components are aggregate functions and variables.

fn-count

An aggregate function with optional parent-context

To understand why the mapping context is important, let's add to the mapping an XML file that contains nested nodes with multiple levels. On the Insert menu, click XML Schema/File, and browse for the file: <Documents>\Altova\MapForce2019\MapForceExamples\Altova_Hierarchical.xml.

mf_parent-context

Altova_Hierarchical.xml

Importantly, in the XML file above, the Office parent node contains multiple Department nodes, and each Department contains multiple Person nodes. If you open the actual XML file in an XML editor, you can see that the distribution of people by office and department is as follows:

 

Office

Department

Number of people

Nanonull, Inc.

Administration

3

Marketing

2

Engineering

6

IT & Technical Support

4

Nanonull Partners, Inc.

Administration

2

Marketing

1

IT & Technical Support

3

 

Now let's assume that your mapping should count all people in all departments. To achieve this requirement, you can add the count function from core | aggregate functions and map data as follows:

mf_parent-context1

If you preview the mapping at this stage, the output is 21, which corresponds to the total number of people in all departments. Notice that the count function includes an optional parent-context item, which so far has not been connected. As a result, the parent context of the count function is the default root node of the source component (which, in this case, is the Altova item). This means that all the persons, from all departments, are considered for the scope of the count function. This is the way the mapping context works by default, as outlined in Mapping Rules and Strategies, and this is sufficient in most mapping scenarios.

 

However, it is possible to override the default mapping context if necessary. To do this, add a connection from the Office node to the parent-context item as shown below.

mf_parent-context2

By changing the mapping as shown above, you are instructing the mapping to iterate over people records in the context of each office. Therefore, if you preview the mapping now, the output will be 15*. This is exactly the number of people in the first office, "Nanonull, Inc.". The explanation is that this time the people nodes were counted twice (once for each office). The count of people in each office was 15 and 6, respectively. However, only the first result was returned (because the function cannot return a sequence of values, only a simple value).

 

* Assuming that the target language of the mapping is other than XSLT 1.0.

 

You can further modify the mapping so as to change the mapping context to Department, as shown below. This time the people records would be counted in the context of each department (that is, 7 times, which corresponds to the total number of departments). Again, only the first of the results is returned, so the mapping output is 3, which corresponds to the number of people in the first department of the first office.

mf_parent-context3

While this mapping is not doing much yet, its point is to illustrate how the parent-context item influences the output of the mapping. Having this in mind, you can override the parent-context in other mappings, such as those that contain variables. See also Example: Grouping and Subgrouping Records.


© 2019 Altova GmbH