Example: Filtering Nodes

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

Home >  Designing Mappings > Filters and Conditions >

Example: Filtering Nodes

This example shows you how to filter nodes based on a true/false condition. A Filter: Nodes/Rows ( mff_ic_insert_filter ) component is used to achieve this goal.

 

The mapping described in this example is available at the following path: <Documents>\Altova\MapForce2019\MapForceExamples\MarketingExpenses.mfd.

mff_map_MarketingExpenses

As shown above, the mapping reads data from a source XML which contains an expense report ("ExpReport") and writes data to a target XML ("MarketingExpenses"). There are several other components between the target and source. The most relevant component is the expense-item filter ( ic-component_filter ), which represents the subject of this topic.

 

The goal of the mapping is to filter out only those expense items that belong to the Marketing department. To achieve this goal, a filter component has been added to the mapping. (To add a filter, click the Insert menu, and then click Filter: Nodes/Rows.)

 

To identify whether each expense item belongs to Marketing, this mapping looks at the value of the "expto" attribute in the source. This attribute has the value "Marketing" whenever the expense is a marketing expense. For example, in the code listing below, the first and third expense item belongs to Marketing, the second belongs to Development, and the fourth belongs to Sales:

 

...  

  <expense-item type="Meal" expto="Marketing">
    <Date>2003-01-01</Date>
    <expense>122.11</expense>
  </expense-item>
  <expense-item type="Lodging" expto="Development">
    <Date>2003-01-02</Date>
    <expense>122.12</expense>
  </expense-item>
  <expense-item type="Lodging" expto="Marketing">
    <Date>2003-01-02</Date>
    <expense>299.45</expense>
  </expense-item>
  <expense-item type="Entertainment" expto="Sales">
    <Date>2003-01-02</Date>
    <expense>13.22</expense>
  </expense-item>

...

XML input before the mapping is executed

On the mapping area, the node/row input of the filter is connected to the expense-item node in the source component. This ensures that the filter component gets the list of nodes that it must process.

 

To add the condition based on which filtering should occur, we have added the equal function from the MapForce core library (for more information, see Working with Functions). The equal function compares the value of the "type" attribute to a constant which has the value "Marketing". (To add a constant, click the Insert menu, and then click Constant.)

 

Since we need to filter only those items that satisfy the condition, we connected only the on-true output of the filter to the target component.

 

When you preview the mapping result, by clicking the Output tab, MapForce evaluates, for each expense-item node, the condition connected to the bool input of the filter. When the condition is true, the expense-item node is passed on to the target; otherwise, it is ignored. Consequently, only the expense items matching the criteria are displayed in the output:

 

...  

  <expense-item>
    <type>Meal</type>
    <Date>2003-01-01</Date>
    <expense>122.11</expense>
  </expense-item>
  <expense-item>
    <type>Lodging</type>
    <Date>2003-01-02</Date>
    <expense>299.45</expense>
  </expense-item>

...

XML output after the mapping is executed


© 2019 Altova GmbH