Please enable JavaScript to view this site.

Altova MapForce 2020 Enterprise Edition

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\MapForce2020\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.

mf_map_summing-nodes1

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"?>
<Input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="input.xsd">
  <Products>
    <Product>
        <Name>ProductA</Name>
        <Amount>10</Amount>
        <Price>5</Price>
    </Product>
    <Product>
        <Name>ProductB</Name>
        <Amount>5</Amount>
        <Price>20</Price>
    </Product>
  </Products>
</Input>

 

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"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 
  <xsl:template match="*">
    <xsl:for-each select=".">
    <xsl:call-template name="Total">
        <xsl:with-param name="string" select="."/>
    </xsl:call-template>
    </xsl:for-each>
  </xsl:template>
     
  <xsl:template name="Total">
  <xsl:param name="string"/>
    <xsl:value-of select="sum($string/Product/Price)"/>
  </xsl:template>
</xsl:stylesheet>

 

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 transformation language. You are now ready to import the custom function, as follows:

 

1.Click the Add/Remove Libraries button at the base of the Libraries window. The Manage Libraries window opens.

mf_manage_libraries_window_empty

2.Do one of the following:

 

To import functions as a local library (in the scope of the current document only), click the Add button under the current mapping name.

To import functions as a global library (at program level), click the Add button adjacent to Global Library Imports.

 

Note: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\MapForce2020\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.

mf_map_summing-nodes1a

4.Drag the Total function from the Libraries into the mapping, and create the mapping connections as shown below.

mf_map_summing-nodes2

To preview the mapping result, click the Output tab. The sum of the two Price fields is now displayed in the Total field.

 

<?xml version="1.0" encoding="UTF-8"?>
<Output xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="output.xsd">
  <Total>25</Total>
  <Product>
    <Name>ProductA</Name>
    <Amount>10</Amount>
    <Price>5</Price>
  </Product>
  <Product>
    <Name>ProductB</Name>
    <Amount>5</Amount>
    <Price>20</Price>
  </Product>
</Output>

© 2020 Altova GmbH