Please enable JavaScript to view this site.

Altova MapForce 2021 Enterprise Edition

Some mapping components have an optional parent-context item. With the help of this item you can influence the mapping context in which that component should operate and consequently change the mapping output. The components that have an optional parent-context are: aggregate functions, variables, and Join components.

 

fn-count

 

For a demo of how changing the parent context is useful, open the following mapping: <Documents>\Altova\MapForce2021\MapForceExamples\Tutorial\ParentContext.mfd.

mf_semantics_11

In the source XML of the mapping above, there is a single Company node which contains two Office nodes. Each Office node contains multiple Department nodes, and each Department contains multiple Person nodes. If you open the 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

 

The mapping counts all people in all departments. For this purpose, it uses the count function from the core library. If you click the Output tab to preview the mapping, you will notice that it produces a single value, 21, which corresponds to the total number of people in the source XML file.

 

The mapping works as follows:

 

As usual, the mapping execution starts from the top node of the target component (rows, in this example). There is no incoming connection to rows. As a result, an implicit mapping context is established between Company (top item of the source component) and rows (top item of the target component).

The function's result is a single value, because there is only one company in the source file.

To populate the col1 target item, MapForce executes the count function in the implicit parent context mentioned above, so it will count all Person nodes from all offices and from all departments.

 

The parent-context argument of the function lets you change the mapping context. This enables you, for example, to count the number of people in each department. To do this, draw two more connections as shown below:

mf_semantics_12

In the mapping above, connection A changes the parent context of the count function to Department. As a result, the function will count the number of people in each department. Very importantly, the function will now return a sequence of results instead of a single result, because multiple departments exist in the source. This is the reason why connection B exists: for each item in the resulting sequence it creates a new row in the target file. The mapping output has now changed accordingly (notice the numbers corresponds exactly to the count of people in each department):

 

<rows>
  <row>
      <col1>3</col1>
  </row>
  <row>
      <col1>2</col1>
  </row>
  <row>
      <col1>6</col1>
  </row>
  <row>
      <col1>4</col1>
  </row>
  <row>
      <col1>2</col1>
  </row>
  <row>
      <col1>1</col1>
  </row>
  <row>
      <col1>3</col1>
  </row>
</rows>

 

Given that the current mapping creates a row for each department, you can optionally copy the office name and the department name as well into the target file, by drawing connections C and D:

mf_semantics_13

This way, the output will display not only the count of people but also the corresponding office and department name.

 

If you would like to count the number of people in each office, connect the parent context of count function to the Office item in the source.

mf_semantics_14

With the connections shown above, the count function returns one result for each office. There are two offices in the source file, so the function will now return two sequences. Consequently, there will be two rows in the output, where each row is the number of people in that office:

 

<rows>
  <row>
      <col1>15</col1>
      <col2>Nanonull, Inc.</col2>
  </row>
  <row>
      <col1>6</col1>
      <col2>Nanonull Partners, Inc.</col2>
  </row>
</rows>

© 2020 Altova GmbH