fn:sum

In the Graphical Data Mapping editor, you can use the fn:sum transform to set the value of an output element to a numeric type that is the result of the sum of all the values in a sequence. You can also use the fn:sum transform to set the value of an output element to the sum of durations in a sequence.

Overview

You can use the XPath 2.0 function fn:sum(arg1, arg2) to sum numeric or duration values in a sequence.

The fn:sum transform is the representation of the fn:sum XPath function in the Graphical Data Mapping editor.

You can have any number of inputs to the fn:sum transform. These inputs can be used to define the arguments of the fn:sum function.

Inputs to the transform

You can connect one or more inputs to the fn:sum transform. These inputs are used to define the arguments of the fn:sum function.

Arguments of the XPath function

You define the two arguments to the fn:sum function in the General tab of the fn:sum transform properties view.
  • The first argument (arg) contains any number of numeric and untyped values, or any number of duration values.
  • The second argument (zero) contains the default value returned by fn:sum when the sequence is empty. You can set this value to the integer 0, a duration of zero seconds, or any other atomic value.
This figure shows the fn:sum arguments.
You must consider the following points when you define the arguments to the fn:sum function:
  • Untyped values are cast to a double type element.
  • You can edit the sequence specified for arg and then add literal values, cast untyped values, and add more values that are the result of arithmetic operations.
  • If arg is an empty sequence, and zero is set, then fn:sum returns the value specified by zero.
  • When you sum durations, you must ensure that all the elements in the sequence have the same type, that is, all values are of type xs:yearMonthDuration or all values are of type xs:dayTimeDuration.
Note: If an input is not of a numeric type such as xs:int, the transform will fail at run time. Cast the input element to a numeric type. For more information, see Cast type (xs:type).

Cardinality

The Cardinality property determines the elements (also known as indexes) in the repeating input element that are processed by the fn:sum transform.

You can configure the Input array indexes section to select specific instances of the input array. For more information, see Selecting the indexes of input array elements.

Define when the transform is applied at run time

You can use any of the input elements to the fn:sum transform to define a conditional expression that defines the condition under which the transform is applied. If the condition evaluates to true, the transform is applied.

For more information, see Configuring the properties of a transform.

Example: Calculate the value of a numeric output element

In the following figure, the fn:sum transform has four inputs and the fn:sum function has two arguments.

The input E is of type string and is cast to an integer type.

The second argument zero is set to the value of multiplying two input elements.

This figure shows the fn:sum transform.

The fn:sum transform returns 11147 for the following input message:


<?xml version="1.0" encoding="UTF-8"?>
<NewElement>
  <A>10</A>
  <C>2</C> 
  <C>10</C> 
  <D>1000</D>
  <E>120</E>
</NewElement>