Altova MapForce 2025 Professional Edition

Example: Replacing Job Titles

Home Prev Top Next

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\MapForce2025\MapForceExamples\Tutorial\MFCompany.xml. It stores, among other data, information about company employees and their job titles, for example:

 

<Person>
  <First>Michelle</First>
  <Last>Butler</Last>
  <Title>Software Engineer</Title>
</Person>
<Person>
  <First>Lui</First>
  <Last>King</Last>
  <Title>Support Engineer</Title>
</Person>

<Person>
  <First>Steve</First>
  <Last>Meier</Last>
  <Title>Office Manager</Title>
</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 ic-new-schema 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 ic-conn-child Toggle auto-connect of children toolbar option first, in order to prevent unnecessary connections from being created automatically.

mf_lookup_06

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:

mf_lookup_07

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>
  <First>Vernon</First>
  <Last>Callaby</Last>
</Person>
<Person>
  <First>Frank</First>
  <Last>Further</Last>
</Person>

<Person>
  <First>Michelle</First>
  <Last>Butler</Last>

  <Title>Code Magician</Title>
</Person>

 

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:

mf_lookup_09

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:

mf_lookup_08

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>
  <First>Vernon</First>
  <Last>Callaby</Last>
  <Title>N/A</Title>
</Person>
<Person>
  <First>Frank</First>
  <Last>Further</Last>
  <Title>N/A</Title>
</Person>
<Person>
  <First>Michelle</First>
  <Last>Butler</Last>
  <Title>Code Magician</Title>
</Person>

 

This concludes the Value-Map example. By applying the logic above, you can now achieve the desired result in other mappings.

© 2018-2024 Altova GmbH