Example: Summing Node Values
This example shows you how to process multiple nodes of an XML document and have the result mapped as a single value to a target XML document. Specifically, the goal of the mapping is to calculate the price of all products in a source XML file and write it as a single value to an output XML file. The files used in this example are available in the <Documents>\Altova\MapForce2024\MapForceExamples\Tutorial\ folder:
•Summing-nodes.mfd — the mapping file
•input.xml — the source XML file
•input.xsd — the source XML schema
•output.xsd — the target XML schema
•Summing-nodes.xslt — A custom XSLT stylesheet containing a named template to sum the individual nodes.
There are two different ways to achieve the goal of the mapping:
•By using the sum function. This MapForce built-in function is available in the Libraries window.
•By importing a custom XSLT stylesheet into MapForce.
Solution 1: Using the "sum" aggregate function
To use the sum function in the mapping, drag it from the Libraries window into the mapping. Note that the functions available in the Libraries window depend on the XSLT language version you selected (XSLT 1 or XSLT 2). Next, create the mapping connections as shown below.
For more information about aggregate functions of the core library, see also core | aggregate functions.
Solution 2: Using a custom XSLT Stylesheet
As mentioned above, the aim of the example is to sum the Price fields of products in the source XML file, in this case products A and B.
<?xml version="1.0" encoding="UTF-8"?> |
The code listing below shows a custom XSLT stylesheet which uses the named template "Total" and a single parameter string. The template works through the XML input file and sums all the values obtained by the XPath expression /Product/Price.
<?xml version="1.0" encoding="UTF-8"?> |
Note: | To sum the nodes in XSLT 2.0, change the stylesheet declaration to version="2.0". |
Before importing the XSLT stylesheet into MapForce, select XSLT 1.0 as a transformation language. You are now ready to import the custom function, as follows:
1.Click the Add/Remove Libraries button at the bottom of the Libraries window. The Manage Libraries window opens (see screenshot below).
2.To import functions as a local library (in the scope of the current document only), click Add under the current mapping name. To import functions as a global library (at program level), click Add next to Global Library Imports. When you import a library locally, you can set the path of the library file to be relative to the mapping file. With globally imported libraries, the path of the imported library is always absolute.
3.Browse for <Documents>\Altova\MapForce2024\MapForceExamples\Tutorial\Summing-nodes.xslt, and click Open. A message box appears informing you that a new library has been added, and the new library appears in the Libraries window.
4.Drag the Total function from the Libraries into the mapping, and create the mapping connections as shown below.
To preview the mapping result, click the Output pane. The sum of the two Price fields is now displayed in the Total field.
<?xml version="1.0" encoding="UTF-8"?> |