Altova MapForce 2023 Basic Edition

Example: Returning a Value Conditionally

Home Prev Top Next

This example shows you how to return a simple value from a component, based on a true/false condition. An If-Else Condition ( mff_ic_if_else ) is used to achieve the goal. Note that If-Else Conditions should not be confused with filter components. If-Else Conditions are only suitable when you need to process simple values conditionally (string, integer, etc.). If you need to filter complex values such as nodes, use a filter instead (see Example: Filtering Nodes).


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


This mapping reads data from a source XML which contains temperature data ("Temperatures") and writes data to a target XML which conforms to the same schema. There are several other components between the target and source, one of them being the if-else condition (highlighted in red), which is also the subject of this topic.


The goal of the mapping is to add short description to each temperature record in the target. Specifically, if temperature is above 20 degrees Celsius, the description should be "high". If the temperature is below 5 degrees Celsius, the description should be "low". For all other cases, no description should be written.


To achieve this goal, conditional processing is required; therefore, an If-Else Condition has been added to the mapping. (To add an If-Else Condition, click the Insert menu, and then click If-Else Condition.) In this mapping, the If-Else Condition has been extended (with the help of the gui_add_function_parameter button) to accept two conditions: bool1 and bool2.


The conditions themselves are supplied by the greater and less functions, which have been added from the MapForce core library, see also Add a Function to the Mapping. These functions evaluate the values provided by two input components, called "upper" and "lower". (To add an input component, click the Insert menu, and then click Insert Input. For more information about input components, see Supplying Parameters to the Mapping.)


The greater and less functions return either true or false. The function result determines what is written to the target instance. Namely, if the value of the "temp" attribute in the source is greater than 20, the constant value "high" is passed to the if-else condition. If the value of the "temp" attribute in the source is less than 5, the constant value "low" is passed on to the if-else condition. The otherwise input is not connected. Therefore, if none of the above conditions is met, nothing is passed to the result output connector.


Finally, the result output connector supplies this value (once for each temperature record) to the "desc" attribute in the target.


When you are ready to preview the mapping result, click the Output tab. Notice that the resulting XML output now includes the "desc" attribute, whenever the temperature is either greater than 20 or lower than 5.


  <data temp="-3.6" month="2006-01" desc="low"/>
  <data temp="-0.7" month="2006-02" desc="low"/>
  <data temp="7.5" month="2006-03"/>
  <data temp="12.4" month="2006-04"/>
  <data temp="16.2" month="2006-05"/>
  <data temp="19" month="2006-06"/>
  <data temp="22.7" month="2006-07" desc="high"/>
  <data temp="23.2" month="2006-08" desc="high"/>

XML output after the mapping is executed

© 2016-2022 Altova GmbH