This example shows you how to return a simple value from a component, based on a true/false condition. An If-Else Condition ( ) 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\MapForce2019\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 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 (for more information, see Working with Functions). 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.
XML output after the mapping is executed
© 2019 Altova GmbH