Example: Converting a TRADACOMS Invoice to XML
This example provides instructions on how to create a mapping design that converts data from a TRADACOMS invoice file to XML format. The mapping design file created in this example is also available at the following path: <Documents>\Altova\MapForce2022\MapForceExamples\TRADACOMS_Invoice.mfd.
The mapping accomplishes the following goals:
1.Convert a source TRADACOMS file (INVFIL.edi) to an XML file which is valid against an existing schema (Order.xsd). Both the source TRADACOMS file and the schema file (Order.xsd) are available in the MapForce Examples folder (<Documents>\Altova\MapForce2022\MapForceExamples\).
2.The date in the TRADACOMS file has decimal format so it must be converted to xs:dateTime format in the target XML file.
3.For each line item in the purchase order, the tax amount must be calculated as a decimal value, rounded to three decimal places (thousandths). For the scope of this example, we will assume that the tax is calculated according to the following formula:
Tax = round(LEXC * VATP * 10) * 0.001
Where round rounds the value to the nearest integer.
To accomplish the goals, take the following steps:
1.Add the source and target components to the mapping area and draw the mapping connections between them.
2.Use date functions to convert the date to the required format.
3.Use math functions to calculate the tax value.
Step 1: Add the components to the mapping area
To add the source TRADACOMS component:
1.On the Insert menu, click EDI.
2.Click the TRADACOMS collection folder, and, under Message Types, select the INVOICE FILE.
3.When prompted to supply a sample file, browse for the INVFIL.edi available in the MapForce Examples folder (<Documents>\Altova\MapForce2022\MapForceExamples\).
4.When prompted to enter the component settings, leave the default values as is, and click OK.
To add the target XML component:
1.On the Insert menu, click XML Schema/File.
2.When prompted to supply a sample file, click Skip. (Because this is a target component, the file will be generated, so there is no need to browse for an existing one.)
3.When prompted to select a root element, select "Order".
In this example, the data will be mapped from the source TRADACOMS file to the target XML file as follows:
* Source data will be processed with functions before being written to the target
** These fields enable iteration through a group of fields of the same type
Using the table above as reference, you can now draw mapping connections from all of the above items in the source component to their destination in the target component. For now, you can omit those fields that require calculated values (that is, the ones marked with * in the table); these will be handled in the next steps. For general information about drawing connections in MapForce, see Working with Connections.
|Tip:||To search for a field by its name inside the MapForce component, press Ctrl + F.|
Step 2: Add the date conversion functions
To convert the value of the IVDT item from a decimal value to xs:dateTime type, the mapping uses the format-number function from the core | conversion functions library. For a worked example, see Preserving Leading Zeros During Conversion.
The role of the second function, parse-date, is to convert the YYMMDD string value returned by the format-number to xs:dateTime type. It has two input arguments: (a) the value to be formatted and (b) the format mask. The format mask essentially instructs MapForce to treat the year, month, and date as having a width of exactly two characters each.
To add the functions above to the mapping, drag them from the Libraries window to the mapping area. To supply an argument to a function, insert a constant (the menu command is Insert | Constant). For general information about working with functions in MapForce, see Functions.
Step 3: Add the tax calculation functions
The functions used to calculate tax according to the formula are multiply and round, available in the core | math functions library. The multiply function is extendable (that is, it can take a variable number of arguments). In this example, it takes LEXC, VATP, and the integer 10 as arguments. The first two values are connected from the source TRADACOMS component, while the integer is supplied by a constant. The result of the multiply function is then rounded to the nearest integer using the round function. Finally, the result of the round function is multiplied by 0.001 by means of a second multiply function. This result is then connected to the destination item of the XML component (/Order/LineItems/LineItem/Article/Tax).
To preview the output of the mapping design, click the Output tab in the lower part of the mapping window.
Step 4 (optional): Style the mapping output with StyleVision
Optionally, you can link the target XML component to a StyleVision Power StyleSheet (.sps) file. (For more information about StyleVision, see https://www.altova.com/stylevision.) This would enable you to generate the mapping output as HTML, RTF, PDF, or 2007+ file (provided that StyleVision is installed and the prerequisites required by each format are in place, see Styling Mapping Output with StyleVision). To take this optional step, double-click the header of the XML component, and, next to "StyleVision Power StyleSheet file", browse for the Order.sps file available in the MapForce Examples folder. Back on the mapping area, you can now click the HTML, PDF, RTF, and Word 2007+ tabs to view the mapping output in the corresponding format.