Please enable JavaScript to view this site.

Altova MapForce 2022 Professional Edition

Tutorials

One Source to One Target

Scroll Home Prev Top Next More

This tutorial describes how to create a mapping for one of the most basic scenarios. Our goal is to take the data from XML file A with XML schema A assigned to it and put this data into XML file B with XML schema B assigned to it. Thus, our target file will have the same data as in the source but with a different schema (structure). Thanks to the transformation code, you will be able to see how the structure of the source file has changed. Note the mapping could be carried out only between the structures, but we will not be able to preview the result of the transformation in the Output pane. Therefore, for illustration purposes, we use an XML file called Books.xml (see code listing below). The broad outline of our method will be as follows:

 

1.Since we are using two data structures, we will create two components (Source and Target) in our mapping design.

2.Then we need to map nodes by connecting a source node to the desired target node. It is these connections that constitute the mapping and determine what source node maps to what target node.

3.Since the transformation of one document into another is carried out by using a suitable transformation language, such as XSLT, we select a transformation language.

4.We use MapForce’s built-in transformation engines to transform the source XML document (Books.xml) into the required target document. This target document will be an XML document that is valid according to the target XSD (Library.xsd).

5.Finally, we can save the output XML file.

 

The image below illustrates an abstract model of the data transformation used in this tutorial:

MF_Tut1_SchemaToSchema

The abstract model above shows two steps of the mapping process. In the first step, the structure of Books.xsd is mapped to a new structure called Library.xsd. The mapping is carried out by means of a transformation language. The choice of a transformation language depends on your MapForce edition. In our case, XSLT2 is chosen as a transformation language. In the second step, the content of Books.xml, which has Books.xsd assigned to it, is mapped to the target file (BooksOutput.xml) and based on a new schema (Library.xsd). The code listing below shows sample data from Books.xml that will be used as a data source.

 

<books>
  <book id="1">
     <author>Mark Twain</author>
     <title>The Adventures of Tom Sawyer</title>
     <category>Fiction</category>
     <year>1876</year>
  </book>
  <book id="2">
     <author>Franz Kafka</author>
     <title>The Metamorphosis</title>
     <category>Fiction</category>
     <year>1912</year>
  </book>  
</books>

 

This is how we want our data to look in the target file called BooksOutput.xml:

 

<library>
  <last_updated>2015-06-02T16:26:55+02:00</last_updated>
  <publication>
     <id>1</id>
     <author>Mark Twain</author>
     <title>The Adventures of Tom Sawyer</title>
     <genre>Fiction</genre>
     <publish_year>1876</publish_year>
  </publication>
  <publication>
     <id>2</id>
     <author>Franz Kafka</author>
     <title>The Metamorphosis</title>
     <genre>Fiction</genre>
     <publish_year>1912</publish_year>
  </publication>
</library>

 

Some element names in the source and target XML are not the same. Our goal is to populate the elements <author>, <title>, <genre> and <publish_year> of the target file with the content of the equivalent elements in the source file (<author>, <title>, <category>, <year>). The attribute id in the source file must be mapped to the <id> element in the target file. Finally, we must populate the <last_updated> element of the target file with the date and time indicating when the file was last updated.

 

To carry out the required data transformation, take the steps described in the subsections below.

 

© 2015-2021 Altova GmbH