Example: Summing Node Values

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Functions > Importing Custom XSLT 1.0 or 2.0 Functions >

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\MapForce2019\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 aggregate function of the core library. This function is available in the Libraries window (see also Working with Functions).
By importing a custom XSLT stylesheet into MapForce.

 

Solution 1: Using the "sum" aggregate function

To use the sum aggregate 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 image 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".

 

To import the XSLT stylesheet into MapForce:

 

1.Select XSLT as transformation language. For more information, see Selecting a Transformation Language.
2.In the Libraries window, click Add/Remove Libraries.
3.On the Options dialog box, click the Libraries tab.
4.Click Add and browse for <Documents>\Altova\MapForce2019\MapForceExamples\Tutorial\Summing-nodes.xslt.
5.Drag the Total function from the newly created "Summing-nodes" library 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>


© 2019 Altova GmbH