In the previous tutorial, Map Multiple Sources to One Target, you have seen how to map data from multiple source schemas to a single target schema. You have also created a file called merged_library.xml, which stores book records from two sources. Now let's assume that someone from another department has asked you to provide a subset of this XML file. Specifically, you must deliver an XML file that includes only the books published after 1900.
For convenience, you can modify the existing MultipleSourcesToOneTarget.mfd mapping so that, whenever required, you can generate both the complete XML library, and the filtered library.
Abstract model of the data transformation
In the diagram above, the data is first merged from two different schemas (books.xsd and library.xsd) into a single XML file called merged_library.xml. Secondly, the data is transformed using a filtering function and passed further to the next component, which creates an XML file called filtered_library.xml. The "intermediate" component acts both as data target and source. In MapForce, this technique is known as "chaining mappings", which is also the subject of this tutorial.
Our goal is to make it possible to generate at any time both the merged_library.xml and the filtered_library.xml. To achieve the goal, let's take the following steps.
Step 1: Prepare the mapping design file
This tutorial uses as starting point the MultipleSourcesToOneTarget.mfd mapping from the <Documents>\Altova\MapForce2019\MapForceExamples\Tutorial\ folder. You have already designed this mapping in the Map Multiple Sources to One Target tutorial. To begin, open the MultipleSourcesToOneTarget.mfd file in MapForce, and save it with a new name.
Make sure to save the new mapping in the <Documents>\Altova\MapForce2019\MapForceExamples\Tutorial\ folder, because it references several files from it.
MultipleSourcesToOneTarget.mfd (MapForce Basic Edition)
Step 2: Add and configure the second target component
To add the second target component, click the Insert XML Schema/File ( ) toolbar button, and open the library.xsd file located in the <Documents>\Altova\MapForce2019\MapForceExamples\Tutorial\ folder. Click Skip when prompted to supply a sample instance file. The mapping now looks as follows:
As shown above, the mapping now has two source components: books and library, and two target components. To distinguish between the target components, we will rename the second one to filtered_library, and also set the name of the XML file that should be generated by it. To do this, double-click the header of the right-most component and edit the component settings as follows:
Notice that the new name of the component is filtered_library, and the output XML file is named filtered_library.xml.
Step 3: Make the connections
Create a connection from the item publication in the merged_library to the item publication in the filtered_library. When you do this, a notification message is displayed.
Click OK. Notice that new buttons are now available in the upper-right corner of both target components: Preview ( ) and Pass-through ( ). These buttons will be used and explained in the following steps.
Step 4: Filter data
To filter data before supplying it to the filtered_library, we will use a Filter component. To add a filter component, right-click the connection between merged_library and filtered_library, and select Insert Filter: Nodes/Rows from the context menu.
The filter component has now been added to the mapping.
As shown above, the bool input connector is highlighted in orange, which suggests that an input is required. If you move the mouse over the connector, you can see that an input of type xs:boolean is required. Note that, for tips to be displayed, the Show tips ( ) toolbar button must be enabled.
The filter component requires a condition that returns either true or false. When the Boolean condition returns true, data of the current publication sequence will be copied over to the target. When the condition returns false, data will not be copied.
In this tutorial, the required condition is to filter all books which were published after 1900. To create the condition, do the following:
|1.||Add a constant of numeric type having the value "1900" (On the Insert menu, click Constant). Choose Number as type.|
|2.||In the Libraries window, locate the function greater and drag it to the mapping pane.|
|3.||Make the mapping connections to and from the function greater as shown below. By doing this, you are instructing MapForce: "When publish_year is greater than 1900, copy the current publication source item to the publication target item".|
Step 5: Preview and save the output of each target component
You are now ready to preview and save the output of both target components. When multiple target components exist in the same mapping, you can choose which one to preview by clicking the Preview ( ) button. When the Preview button is in a pressed state ( ), it indicates that that specific component is currently enabled for preview (and this particular component will generate the output in the Preview pane). Only one component at a time can have the preview enabled.
Therefore, when you want to view and save the output of the merged_library (that is, the "intermediate") component, do the following:
|1.||Click the Preview button ( ) on the merged_library component.|
|2.||Click the Output button at the bottom of the mapping pane.|
|3.||On the Output menu, click Save Output File if you want to save the output to a file.|
When you want to view and save the output of the filtered_library component :
|1.||Click the Pass-through button ( ) on the merged_library component.|
|2.||Click the Preview button ( ) on the filtered_library component.|
|3.||Click the Output button at the bottom of the mapping pane.|
|4.||On the Output menu, click Save Output File if you want to save the output to a file.|
Notice the Pass-through ( ) button—clicking or not clicking it makes a big difference in any mapping which has multiple target components, including this one. When this button is in a pressed state ( ), MapForce lets data pass through the intermediate component, so that you can preview the result of the entire mapping.
Release the button ( ) if you want to preview only the portion of the mapping between the merged_library and the filtered_library. In the latter case, an error will be generated. This behavior is expected, because the intermediate component does not have a valid input XML file from which it should read data. To solve the problem, double-click the header of the component and edit so as to supply a valid input XML file, as shown below:
You have now finished designing a mapping which has multiple target components, and you can view and save the output of each target, which was the intended goal of this tutorial. For further information about working with pass-through components, see Chained mappings / pass-through components.
© 2019 Altova GmbH