Altova MapForce 2025 Enterprise Edition

Example: Exception When Node Does Not Exist

Home Prev Top Next

This example illustrates how to throw an exception when a node in the source XML schema does not exist. For the sake of simplicity, this example uses the same XML schema both as source and target component.

To add the source schema to the mapping:

1.On the Insert menu, click XML Schema/File, and browse for <Documents>\Altova\MapForce2025\MapForceExamples\BookList.xsd.

2.When prompted to provide an instance file, click Skip.

3.When prompted to select a schema root element, select BookList as root element.

 

To add the target schema, follow the same steps. Then, using the corresponding commands from Insert menu (or the corresponding toolbar buttons), add the following:

 

A Filter: Nodes/Rows component (see also Filters and Conditions)

A constant with the text "No year defined!"

An exception

 

Finally, drag the exists function from the Libraries window into the mapping area, and make the connections as illustrated below.

mf_exception_example_1

According to the XML schema, all attributes of the Book element are optional, except the book title. Therefore, the "Year" attribute may or may not exist in a valid XML instance. The goal of the mapping is to process successfully an XML instance where the "Year" attribute exists for each book. Otherwise, the mapping must throw an exception.

To test the successful execution of the mapping:

1.Double-click the header of the source component and, next to Input XML file, browse for the following file: <Documents>\Altova\MapForce2025\MapForceExamples\BookList.xml.

2.Click the Output button to run the mapping.

To test the exception:

1.Create, in the same directory, a copy of the BookList.xml file called BookListInvalid.xml.

2.Modify it so as to remove the "Year" attribute from a Book element.

3.Double-click the header of the source component, and, next to Input XML file, browse for the BookListInvalid.xml file.

4.Click the Output button to run the mapping.

 

Let's now have a closer look at how the mapping works.

 

Connection A ensures that a book in the target instance is created for each book in the source instance. Connections B, C, D, E ensure that the "Title", "Year", "Price", and "Author" are copied from the source to the target, for each book.

 

Connection F triggers the exists function to check for the existence of the "Year" attribute. Connection G passes the function result (true or false) to the filter. If the result is true, the "Year" attribute exists, and the book is passed on to the filter, and subsequently to the target through connection H.

 

Notice that the filter was not connected directly to the Year output of the source component. Had we done so, the filter would filter the Year by its own existence, which is not meaningful, and the exception would never be thrown.

 

Connection I is there because the exception must be connected either to an on-false or on-true output of a filter, according to the rules. Finally, connection K passes the custom error text from the constant to the exception component.

© 2018-2024 Altova GmbH