Please enable JavaScript to view this site.

Altova MapForce 2020 Professional Edition

Tutorials

Convert XML to New Schema

Scroll Home Prev Top Next More

This tutorial shows you how to convert data between two XML files, while helping you learn the basics of the MapForce development environment. Both XML files store a list of books, but their elements are named and organized in a slightly different way (that is, the two files have different schemas).

Tutorial_Convert_XML_to_New_Schema

Abstract model of the data transformation

The code listing below shows sample data from the file that will be used as data source (for the sake of simplicity, the XML and the namespace declarations are omitted).

 

<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>

books.xml

This is how data should look in the target (destination) file:

 

<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>

library.xml

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

 

To achieve the required data transformation, let's take the following steps.

 

Step 1: Select XSLT2 as transformation language

You can do this in one of the following ways:

Click the XSLT2 ( toolbar_xslt2 ) toolbar button.

On the Output menu, click XSLT 2.0.

 

Step 2: Add the source XML file to the mapping

The source XML file for this mapping is located at the following path: <Documents>\Altova\MapForce2020\MapForceExamples\Tutorial\books.xml. You can add it to the mapping in one of the following ways:

Click the Insert XML Schema/File ( ic-new-schema ) toolbar button.

On the Insert menu, click XML Schema/File.

Drag the XML file from Windows Explorer into the mapping area.

 

Now that the file has been added to the mapping area, you can see its structure at a glance. In MapForce, this structure is known as a mapping component, or simply component. You can expand elements in the component either by clicking the collapse ( gui_collapse ) and expand icons ( gui_expand ), or by pressing the + and - keys on the numeric keypad.

tut-01-BAS

Mapping component

To move the component inside the mapping pane, click the component header and drag the mouse to a new position. To resize the component, drag the corner of the component ic-bestfit. You can also double-click the corner so that MapForce adjusts the size automatically.

 

The top level node component_filerepresents the file name; in this particular case, its title displays the name of the XML instance file. The XML elements in the structure are represented by the component_element icon, while XML attributes are represented by the component_attribute icon.

 

The small triangles displayed on both sides of the component represent data inputs (if they are on the left side) or outputs (when they are on the right side). In MapForce, they are called input connectors and output connectors, respectively.

 

Step 3: Add the target XML schema to the mapping

To generate the target XML, we will use an existing XML schema file. In a real-life scenario, this file may have been provided to you by a third party, or you can create it yourself with a tool such as XMLSpy. If you don't have a schema file for your XML data, MapForce prompts you to generate it whenever you add to the mapping an XML file without an accompanying schema or schema reference.

 

For this particular example, we are using an existing schema file available at: <Documents>\Altova\MapForce2020\MapForceExamples\Tutorial\library.xsd. To add it to the mapping, follow the same steps as with the source XML file (that is, click the Insert XML Schema/File ( ic-new-schema ) toolbar button). Click Skip when prompted by MapForce to supply an instance file.

dlg_xml_supply_instance

At this stage, the mapping design looks as follows:

tut-01-02-BAS

 

Step 4: Make the connections

For each <book> in the source XML file, we want to create a new <publication> in the target XML file. We will therefore create a mapping connection between the <book> element in the source component and the <publication> element in the target component. To create the mapping connection, click the output connector (the small triangle) to the right of the <book> element and drag it to the input connector of the <publication> element in the target.

 

When you do this, MapForce may automatically connect all elements which are children of <book> in the source file to elements having the same name in the target file; therefore, four connections are being created simultaneously. This behavior is called "Auto Connect Matching Children" and it can be disabled and customized if necessary.

tut-01-03-BAS

You can enable or disable the "Auto Connect Matching Children" behavior in one of the following ways:

 

Click the Toggle auto connect of children ( ic-conn-child ) toolbar button.

On the Connection menu, click Auto Connect Matching Children.

 

Notice that some of the input connectors on the target component have been highlighted by MapForce in orange, which indicates that these items are mandatory. To ensure the validity of the target XML file, provide values for the mandatory items as follows:

 

Connect the <category> element in the source with the <genre> element in the target

Connect the <year> element in the source with the <publish_year> element in the target

 

Finally, you need to supply a value to the <last_updated> element. If you move the mouse over its input connector, you can see that the element is of type xs:dateTime. Note that, for tips to be displayed, the Show tips ( ic-show-lib-info ) toolbar button must be enabled.

tut-01-05-BAS

You can also make the data type of each item visible at all times, by clicking the Show Data Types ( ic-show-types ) toolbar button.

 

You can get the current date and time (that is, the xs:dateTime value) by means of a date and time XSLT function. To find the XSLT function to the mapping, start typing "date" in the text box located in the lower part of the Libraries window. Alternatively, double-click an empty area on the mapping and start typing "current-date".

tut-01-06-BAS

As shown above, if you move the mouse over the "result" part of the function, you can see its description. For tips to be displayed, make sure that the Show tips ( ic-show-lib-info ) toolbar button is enabled.

 

To add the function to the mapping, drag the function into the mapping pane, and connect its output to the input of the <last_updated> element.

tut-01-07-BAS

You have now created a MapForce mapping design (or simply a "mapping") which converts data from the books.xml instance file (having the books.xsd schema) to the new library.xml file (having the library.xsd schema). If you double-click the header of each component, you can view these and other settings in the Component Settings dialog box, as shown below.

tut-01-10-BAS

Component settings for the source

tut-01-11-BAS

Component settings for the target

 

Step 5: Validate and save the mapping

Validating a mapping is an optional step that enables you to see and correct potential mapping errors and warnings before you run the mapping. To check whether the mapping is valid, do one of the following:

 

On the File menu, click Validate Mapping.

Click the Validate ( ic-val-mapping ) toolbar button.

 

The Messages window displays the validation results:

gui_messages_window

Messages window

At this point, you might also want to save the mapping to a file. To save the mapping, do one of the following:

 

On the File menu, click Save.

Click the Save ( toolbar_btn_save ) toolbar button.

 

For your convenience, the mapping created in this tutorial is available at the following path: <Documents>\Altova\MapForce2020\MapForceExamples\Tutorial\\BooksToLibrary.mfd. Therefore, from this point onwards, you can either continue with the mapping file you created, or with the BooksToLibrary.mfd file.

 

Step 6: Preview the mapping result

You can preview the result of the mapping directly in MapForce. To do this, click the Output button located in the lower part of the mapping pane. MapForce runs the transformation and displays the result of the mapping in the Output pane.

tut-01-08-BAS

Output pane

You can now see the result of the transformation in MapForce.

 

By default, the files displayed for preview in the Output pane are not written to the disk. Instead, MapForce creates temporary files. To save the file displayed in the Output pane to the disk, select the menu command Output | Save Output File, or click the Save generated output ( ic-save-sel-string ) toolbar button.

 

To configure MapForce to write the output directly to final files instead of temporary, go to Tools | Options | General, and then select the Write directly to final output files check box. Note that enabling this option is not recommended while you follow this tutorial, because you may unintentionally overwrite the original tutorial files.

 

You can also preview the generated XSLT code that performs the transformation. To preview the code, click the XSLT2 button located in the lower area of the mapping pane.

tut-01-09-BAS

XSLT2 pane

To generate and save the XSLT2 code to a file, select the menu item File | Generate Code in | XSLT 2.0. When prompted, select a folder where the generated code must be saved. After code generation completes, the destination folder includes the following two files:

 

1.An XSLT transformation file, named after the target schema (in this example, MappingMaptolibrary.xslt).

2.A DoTransform.bat file. The DoTransform.bat file enables you to run the XSLT transformation in RaptorXML Server (for more information, see https://www.altova.com/raptorxml.html ).

© 2019 Altova GmbH