Example: Replacing Job Titles
This example shows you how to replace values of specific elements in an XML file with the help of Value-Map components (that is, using a predefined look-up table).
The XML file required for this example is available at the following path: <Documents>\Altova\MapForce2023\MapForceExamples\Tutorial\MFCompany.xml. It stores, among other data, information about company employees and their job titles, for example:
<Person> <Person> |
Let's assume that you need to replace some of the job titles in the XML file above. Specifically, the title "Software Engineer" must be replaced with "Code Magician". Also, the title "Support Engineer" must be replaced with "Support Magician". All the other job titles must remain unchanged.
To achieve the goal, add the XML file to the mapping area, by clicking the Insert XML Schema/File toolbar button or by running the Insert | XML Schema/File menu command. Next, copy-paste the XML component on the mapping and create the connections as shown below. Note that you might need to turn off the Toggle auto-connect of children toolbar option first, in order to prevent unnecessary connections from being created automatically.
The mapping created so far simply copies the Person elements to the target XML file, without making any changes to the First, Last, and Title elements.
To replace the required job titles, let's add a Value-Map component. Right-click the connection between the two Title elements, and select Insert Value-Map from the context menu. Set up the Value-Map properties as shown below:
According to the setup above, each occurrence of "Software Engineer" will be replaced with "Code Magician", and each occurrence of "Support Engineer" will be replaced with "Support Magician". Notice that the Otherwise condition was not specified yet. For this reason, the Value-Map returns an empty node whenever the job title is other than "Software Engineer" and "Support Engineer". Consequently, if you click the Output pane and preview the mapping, some of the Person elements will have a missing a Title, for example:
<Person> <Person> <Title>Code Magician</Title> |
As stated before, empty nodes cause missing entries in the generated output; therefore, in the XML fragment above, only Michelle Butler had the title replaced, because her title was present in the look-up table. The configuration created so far still does not fulfill the original requirement. The correct setup is as follows:
With the configuration above, the following happens at mapping run time:
•Each occurrence of "Software Engineer" will be replaced with "Code Magician"
•Each occurrence of "Support Engineer" will be replaced with "Support Magician"
•If the original title is not found in the look-up table, the Value-Map will return it unchanged.
For illustrative purposes only, we can also change all the job titles other than "Software Engineer" and "Support Engineer" to a custom value, for example "N/A". To achieve this, set the Value-Map properties as shown below:
When you preview the mapping this time, each job title is present in the output, but those that were not matched have the "N/A" value, for example:
<Person> |
This concludes the Value-Map example. By applying the logic above, you can now achieve the desired result in other mappings.