#### Topic: The total of nonblank elements of recursions

It is used XSLT 1.0. Is XML:

``````&lt;root&gt;
&lt;SUM_1&gt; 10 &lt;SUM_1&gt;
&lt;SUM_2&gt; 7 &lt;SUM_2&gt;
&lt;SUM_5&gt; &lt;SUM_5&gt;
&lt;SUM_6&gt; 3 &lt;SUM_6&gt;
&lt;SUM_11&gt; 1 &lt;SUM_11&gt;
&lt;SUM_15&gt; 12 &lt;SUM_15&gt;
&lt;root&gt;``````

In total elements SUM _ &lt;N&gt; can be 15. There can be everything, there can be some, some can be empty.
It is necessary to count the total of all elements. I know that the task c the help of a recursion dares, but the further advancements on this subject of result did not give. Help, please.

#### Re: The total of nonblank elements of recursions

I apologize, XML the such:

``````&lt;root&gt;
&lt;SUM_1&gt; 10 &lt;/SUM_1&gt;
&lt;SUM_2&gt; 7 &lt;/SUM_2&gt;
&lt;SUM_5&gt; &lt;/SUM_5&gt;
&lt;SUM_6&gt; 3 &lt;/SUM_6&gt;
&lt;SUM_11&gt; 1 &lt;/SUM_11&gt;
&lt;SUM_15&gt; 12 &lt;/SUM_15&gt;
&lt;/root&gt;``````

#### Re: The total of nonblank elements of recursions

``````&lt;xsl:template match = "root"&gt;
&lt;xsl:value-of select = "sum (*)"/&gt;
&lt;/xsl:template&gt;``````

#### Re: The total of nonblank elements of recursions

_Vasilisk _;
Thanks, but real L contain other elements where too there are numbers. It would be desirable to select only elements of type SUM _ &lt;N&gt;.

``````&lt;xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"&gt;
&lt;xsl:output method = "xml" version = "1.0" encoding = "utf-8" omit-xml-declaration = "yes" indent = "yes"/&gt;
&lt;xsl:template match = "/"&gt;
&lt;root&gt;
&lt;xsl:element name = "eps"&gt;
&lt;xsl:attribute name = "name"&gt; NAZN.TOTAL_SUM &lt;/xsl:attribute&gt;
&lt;xsl:variable name = "total_sum"&gt;
&lt;xsl:call-template name = "sumParam"&gt;
&lt;xsl:with-param name = "count_service" select = "15"/&gt;
&lt;/xsl:call-template&gt;
&lt;/xsl:variable&gt;
&lt;xsl:call-template name = "format_number"&gt;
&lt;xsl:with-param name = "number" select = "\$ total_sum"/&gt;
&lt;/xsl:call-template&gt;
&lt;/xsl:element&gt;
&lt;/root&gt;
&lt;/xsl:template&gt;
&lt;xsl:template name = "format_number"&gt;
&lt;xsl:param name = "number"/&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test = "not (normalize-space (\$number)) or string (\$number) = ' NaN '"&gt;
&lt;xsl:value-of select = "' 0.00 '"/&gt;
&lt;/xsl:when&gt;
&lt;xsl:otherwise&gt;
&lt;xsl:value-of select = "format-number (translate (\$number, ', ', '. '), ' 0.00 ')"/&gt;
&lt;/xsl:otherwise&gt;
&lt;/xsl:choose&gt;
&lt;/xsl:template&gt;
&lt;xsl:template name = "sumParam"&gt;
&lt;xsl:param name = "count_service"/&gt;
&lt;xsl:param name = "pAccum" select = "0"/&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test = "number (\$count_service)&gt; 0"&gt;
&lt;xsl:call-template name = "sumParam"&gt;
&lt;xsl:with-param name = "count_service" select = "\$ count_service - 1"/&gt;
&lt;xsl:with-param name = "pAccum" select = "\$ pAccum + number (concat ('/SUM _ ', \$ count_service))"/&gt;
&lt;/xsl:call-template&gt;
&lt;/xsl:when&gt;
&lt;xsl:otherwise&gt;
&lt;xsl:value-of select = "\$ pAccum"/&gt;
&lt;/xsl:otherwise&gt;
&lt;/xsl:choose&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;``````

Result:

``````&lt;root&gt;
&lt;eps name = "NAZN.TOTAL_SUM"&gt; 0.00 &lt;/eps&gt;
&lt;/root&gt;``````

#### Re: The total of nonblank elements of recursions

In general collected the decision, it can be useful to whom, but ugly with duplication of the code and without a dynamic call of tags &lt;SUM _ [N]&gt;.

``````&lt;xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"&gt;
&lt;xsl:output method = "xml" version = "1.0" encoding = "utf-8" omit-xml-declaration = "yes" indent = "yes"/&gt;
&lt;xsl:template match = "/"&gt;
&lt;root&gt;
&lt;xsl:element name = "eps"&gt;
&lt;xsl:attribute name = "name"&gt; NAZN.TOTAL_SUM &lt;/xsl:attribute&gt;
&lt;xsl:variable name = "total_sum"&gt;
&lt;xsl:call-template name = "sumParam"&gt;
&lt;xsl:with-param name = "count_service" select = "15"/&gt;
&lt;/xsl:call-template&gt;
&lt;/xsl:variable&gt;
&lt;xsl:call-template name = "format_number"&gt;
&lt;xsl:with-param name = "number" select = "\$ total_sum"/&gt;
&lt;/xsl:call-template&gt;
&lt;/xsl:element&gt;
&lt;/root&gt;
&lt;/xsl:template&gt;
&lt;xsl:template name = "format_number"&gt;
&lt;xsl:param name = "number"/&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test = "not (normalize-space (\$number)) or string (\$number) = ' NaN '"&gt;
&lt;xsl:value-of select = "' 0.00 '"/&gt;
&lt;/xsl:when&gt;
&lt;xsl:otherwise&gt;
&lt;xsl:value-of select = "format-number (translate (\$number, ', ', '. '), ' 0.00 ')"/&gt;
&lt;/xsl:otherwise&gt;
&lt;/xsl:choose&gt;
&lt;/xsl:template&gt;
&lt;xsl:template name = "sumParam"&gt;
&lt;xsl:param name = "count_service"/&gt;
&lt;xsl:param name = "pAccum" select = "0"/&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test = "number (\$count_service) &gt; 0"&gt;
&lt;xsl:variable name = "value"&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test = "\$ count_service = 1"&gt; &lt;xsl:value-of select = "//SUM_1"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 2"&gt; &lt;xsl:value-of select = "//SUM_2"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 3"&gt; &lt;xsl:value-of select = "//SUM_3"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 4"&gt; &lt;xsl:value-of select = "//SUM_4"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 5"&gt; &lt;xsl:value-of select = "//SUM_5"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 6"&gt; &lt;xsl:value-of select = "//SUM_6"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 7"&gt; &lt;xsl:value-of select = "//SUM_7"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 8"&gt; &lt;xsl:value-of select = "//SUM_8"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 9"&gt; &lt;xsl:value-of select = "//SUM_9"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 10"&gt; &lt;xsl:value-of select = "//SUM_10"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 11"&gt; &lt;xsl:value-of select = "//SUM_11"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 12"&gt; &lt;xsl:value-of select = "//SUM_12"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 13"&gt; &lt;xsl:value-of select = "//SUM_13"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 14"&gt; &lt;xsl:value-of select = "//SUM_14"/&gt; &lt;/xsl:when&gt;
&lt;xsl:when test = "\$ count_service = 15"&gt; &lt;xsl:value-of select = "//SUM_15"/&gt; &lt;/xsl:when&gt;
&lt;/xsl:choose&gt;
&lt;/xsl:variable&gt;
&lt;xsl:variable name = "value_norm"&gt;
&lt;xsl:choose&gt;
&lt;xsl:when test = "not (normalize-space (\$value)) or string (\$value) = ' NaN '"&gt;
&lt;xsl:value-of select = "' 0.00 '"/&gt;
&lt;/xsl:when&gt;
&lt;xsl:otherwise&gt;
&lt;xsl:value-of select = "format-number (translate (\$value, ', ', '. '), ' 0.00 ')"/&gt;
&lt;/xsl:otherwise&gt;
&lt;/xsl:choose&gt;
&lt;/xsl:variable&gt;
&lt;xsl:variable name = "cnt"&gt; &lt;xsl:value-of select = "concat ('//SUM _ ', \$ count_service)"/&gt; &lt;/xsl:variable&gt;
&lt;xsl:variable name = "cnt2"&gt; &lt;xsl:value-of select = "\$ cnt"/&gt; &lt;/xsl:variable&gt;
&lt;xsl:call-template name = "sumParam"&gt;
&lt;xsl:with-param name = "count_service" select = "\$ count_service - 1"/&gt;
&lt;xsl:with-param name = "pAccum" select = "\$ pAccum + \$ value_norm"/&gt;
&lt;/xsl:call-template&gt;
&lt;/xsl:when&gt;
&lt;xsl:otherwise&gt;
&lt;xsl:value-of select = "\$ pAccum"/&gt;
&lt;/xsl:otherwise&gt;
&lt;/xsl:choose&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;``````

#### Re: The total of nonblank elements of recursions

It would be desirable to select only elements of type SUM _ &lt;N&gt;.

``````&lt;xsl:template match = "root"&gt;
&lt;xsl:value-of select = "sum (* [starts-with (name (), ' SUM _ ')])"/&gt;
&lt;/xsl:template&gt;``````

#### Re: The total of nonblank elements of recursions

_Vasilisk _;

``````&lt;xsl:template match = "root"&gt;
&lt;xsl:value-of select = "format-number (sum (//* [starts-with (name (), ' SUM _ ')]), ' 0.00 ')"/&gt;
&lt;/xsl:template&gt;``````