1

Topic: The total of nonblank elements of recursions

It is used XSLT 1.0. Is XML:

<root>
<SUM_1> 10 <SUM_1>
<SUM_2> 7 <SUM_2>
<SUM_5> <SUM_5>
<SUM_6> 3 <SUM_6>
<SUM_11> 1 <SUM_11>
<SUM_15> 12 <SUM_15>
<root>

In total elements SUM _ <N> 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.

2

Re: The total of nonblank elements of recursions

adar7;
I apologize, XML the such:

<root>
<SUM_1> 10 </SUM_1>
<SUM_2> 7 </SUM_2>
<SUM_5> </SUM_5>
<SUM_6> 3 </SUM_6>
<SUM_11> 1 </SUM_11>
<SUM_15> 12 </SUM_15>
</root>

3

Re: The total of nonblank elements of recursions

<xsl:template match = "root">
<xsl:value-of select = "sum (*)"/>
</xsl:template>

4

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 _ <N>.

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

Result:

<root>
<eps name = "NAZN.TOTAL_SUM"> 0.00 </eps>
</root>

5

Re: The total of nonblank elements of recursions

adar7;
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 <SUM _ [N]>.

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

6

Re: The total of nonblank elements of recursions

adar7 wrote:

It would be desirable to select only elements of type SUM _ <N>.

<xsl:template match = "root">
<xsl:value-of select = "sum (* [starts-with (name (), ' SUM _ ')])"/>
</xsl:template>

7

Re: The total of nonblank elements of recursions

_Vasilisk _;

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

8

Re: The total of nonblank elements of recursions

adar7 wrote:

NEVER use the operator//It forces to view ALL XML