Jump to content

Summing Variables


avalli

Recommended Posts

Hello everybody,I'm trying to transform an XML file into a table using XSLT.I can calculate the value of a variable and get the value on each row of the table.How can I sum the value of the variable of each row and get the total amount for each column?As far as I know, I can't use the sum operator with a variablesum($variable)because the sum opearator works with Xpath expressions only.Any suggestions are appreciated.Thanks a lot,Alessandro

Link to comment
Share on other sites

If your XML contains each number in a separate cell, like:

<tr><td>2</td><td>3</td></tr>

Then, once you're over the <tr>, you can do "sum(td)".

Link to comment
Share on other sites

If your XML contains each number in a separate cell, like:
<tr><td>2</td><td>3</td></tr>

Then, once you're over the <tr>, you can do "sum(td)".

My xsl file looks like the one you suggested, but instead of numbers, I've got variables values in each <td><tr><td><xsl:value-of select="$var"/></td><td><xsl:value-of select="$var"/></td></tr>I'd like to get another <tr> with the sum of the <td> above.
Link to comment
Share on other sites

Here are my files (XML and below XSL).In the last <tr> of the XSL file, I can't calculate the sum of the corresponding <td>. :) ---------------------------------------------------------------<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="./ore.xsl" ?><!DOCTYPE ore [<!ELEMENT ore (entry+)><!ATTLIST ore prof CDATA #REQUIRED ultima_mod CDATA #REQUIRED><!ELEMENT entry (#PCDATA)><!ATTLIST entry cat (33|40|dovute|fondo|bo) #REQUIRED frontali (si|no) #REQUIRED aggio (si|no) #REQUIRED supp (si|no) #REQUIRED ud (si|no) #REQUIRED corso (si|no) #REQUIRED anno (09|10) #REQUIRED giorno CDATA #REQUIRED mese CDATA #REQUIRED sett (lun|mar|mer|gio|ven|sab) #REQUIRED ini_h CDATA #REQUIRED ini_m CDATA #REQUIRED fine_h CDATA #REQUIRED fine_m CDATA #REQUIRED>]><ore prof="Vallin" ultima_mod="28/03/2010"><entry cat="40" frontali="no" aggio="no" supp="no" ud="si" corso="no" anno="09" giorno="11" mese="12" sett="lun" ini_h="15" ini_m="00" fine_h="18" fine_m="30">udienze generali I quadrimestre</entry><entry cat="33" frontali="si" aggio="no" supp="no" ud="no" corso="no" anno="10" giorno="02" mese="03" sett="mar" ini_h="08" ini_m="00" fine_h="13" fine_m="00">viaggio d'istruzione a Monaco</entry><entry cat="bo" frontali="no" aggio="no" supp="no" ud="no" corso="no" anno="10" giorno="09" mese="01" sett="mar" ini_h="13" ini_m="30" fine_h="15" fine_m="10">programmazione progetto salute</entry><entry cat="33" frontali="si" aggio="no" supp="no" ud="no" corso="no" anno="10" giorno="03" mese="03" sett="mer" ini_h="08" ini_m="00" fine_h="13" fine_m="00">viaggio d'istruzione a Monaco</entry><entry cat="33" frontali="si" aggio="no" supp="no" ud="no" corso="no" anno="10" giorno="04" mese="03" sett="gio" ini_h="08" ini_m="00" fine_h="13" fine_m="00">viaggio d'istruzione a Monaco</entry><entry cat="40" frontali="no" aggio="si" supp="no" ud="no" corso="no" anno="09" giorno="26" mese="11" sett="mer" ini_h="14" ini_m="30" fine_h="16" fine_m="30">corso aggiornamento CLM</entry><entry cat="33" frontali="si" aggio="no" supp="no" ud="no" corso="si" anno="09" giorno="06" mese="09" sett="lun" ini_h="09" ini_m="00" fine_h="11" fine_m="00">corso recupero per debito A.S. 08/09 classi 4e</entry><entry cat="dovute" frontali="no" aggio="no" supp="no" ud="no" corso="no" anno="09" giorno="08" mese="09" sett="ven" ini_h="09" ini_m="30" fine_h="11" fine_m="45">collegio docenti</entry><entry cat="dovute" frontali="no" aggio="no" supp="no" ud="no" corso="no" anno="09" giorno="10" mese="09" sett="gio" data="10/09" ini_h="08" ini_m="15" fine_h="10" fine_m="20">riunione gruppo didattico inglese</entry><entry cat="bo" frontali="si" aggio="no" supp="no" ud="no" corso="no" anno="10" giorno="22" mese="01" sett="sab" ini_h="08" ini_m="00" fine_h="14" fine_m="00">gare di sci d'istituto</entry><entry cat="fondo" frontali="si" aggio="no" supp="no" ud="no" corso="si" anno="10" giorno="23" mese="01" sett="mar" ini_h="16" ini_m="00" fine_h="17" fine_m="30">corso recupero classi 3e</entry><entry cat="40" frontali="no" aggio="si" supp="no" ud="no" corso="no" anno="09" giorno="04" mese="12" sett="ven" ini_h="14" ini_m="30" fine_h="16" fine_m="30">corso aggiornamento CLM</entry><entry cat="fondo" frontali="si" aggio="no" supp="no" ud="no" corso="si" anno="10" giorno="23" mese="01" sett="sab" ini_h="14" ini_m="00" fine_h="15" fine_m="30">corso recupero classi 2e</entry><entry cat="40" frontali="no" aggio="si" supp="no" ud="no" corso="no" anno="09" giorno="05" mese="12" sett="lun" ini_h="14" ini_m="30" fine_h="16" fine_m="30">corso aggiornamento CLM</entry> <entry cat="40" frontali="si" aggio="no" supp="si" ud="no" corso="no" anno="09" giorno="15" mese="10" sett="sab" ini_h="08" ini_m="50" fine_h="09" fine_m="50">supplenza 3TC</entry><entry cat="40" frontali="si" aggio="no" supp="si" ud="no" corso="no" anno="10" giorno="12" mese="02" sett="gio" ini_h="10" ini_m="55" fine_h="11" fine_m="50">supplenza 2LA</entry></ore>--------------------------------------------------------------------<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><head><title>le mie ore</title></head><body><center><h2><p align="center"><tt>consuntivo ore prof. <xsl:value-of select="./ore/@prof"/></tt></p></h2></center><br /><center><table border="1" cellpadding="4"><!-- prima riga (con header colonne) --><tr> <th align="center" bgcolor="#AADD33"><font size="2"><tt>data</tt></font></th> <th align="center" bgcolor="#BBEE66"><font size="2"><tt>orario</tt></font></th> <th align="center" bgcolor="#EEEEFF"><font size="2"><tt>attività</tt></font></th> <th align="center" bgcolor="#BB9966"><font size="2"><tt>dovute</tt></font></th> <th align="center" bgcolor="#CC9933"><font size="2"><tt>40</tt></font></th> <th align="center" bgcolor="#DDAA66"><font size="2"><tt>33</tt></font></th> <th align="center" bgcolor="#EEBB99"><font size="2"><tt>fondo</tt></font></th></tr><xsl:for-each select="ore/entry"><xsl:sort select="@anno"/><xsl:sort select="@mese"/><xsl:sort select="@giorno"/><xsl:sort select="@ini_h"/><xsl:sort select="@ini_m"/><tr><!-- colonna data --><td align="center"><font size="2"><tt><xsl:value-of select="./@sett"/>. <xsl:value-of select="./@giorno"/>/<xsl:value-of select="./@mese"/></tt></font></td><!-- colonna orario --><xsl:choose> <xsl:when test="./@aggio='si'"> <td align="center" bgcolor="#AAAAFF"><font size="2"><tt><xsl:value-of select="./@ini_h"/>.<xsl:value-of select="./@ini_m"/>-<xsl:value-of select="./@fine_h"/>.<xsl:value-of select="./@fine_m"/></tt></font></td> </xsl:when> <xsl:when test="./@supp='si'"> <td align="center" bgcolor="#CCCCCC"><font size="2"><tt><xsl:value-of select="./@ini_h"/>.<xsl:value-of select="./@ini_m"/>-<xsl:value-of select="./@fine_h"/>.<xsl:value-of select="./@fine_m"/></tt></font></td> </xsl:when> <xsl:when test="./@ud='si'"> <td align="center" bgcolor="#FFCCFF"><font size="2"><tt><xsl:value-of select="./@ini_h"/>.<xsl:value-of select="./@ini_m"/>-<xsl:value-of select="./@fine_h"/>.<xsl:value-of select="./@fine_m"/></tt></font></td> </xsl:when> <xsl:when test="./@corso='si'"> <td align="center" bgcolor="#FFFF99"><font size="2"><tt><xsl:value-of select="./@ini_h"/>.<xsl:value-of select="./@ini_m"/>-<xsl:value-of select="./@fine_h"/>.<xsl:value-of select="./@fine_m"/></tt></font></td> </xsl:when> <xsl:otherwise> <td align="center" bgcolor="#FFFFFF"><font size="2"><tt><xsl:value-of select="./@ini_h"/>.<xsl:value-of select="./@ini_m"/>-<xsl:value-of select="./@fine_h"/>.<xsl:value-of select="./@fine_m"/></tt></font></td> </xsl:otherwise></xsl:choose><!-- attività --><xsl:choose> <xsl:when test="./@frontali='si'"> <td align="center" bgcolor="#FFFFFF"><font size="2" color="#9900FF"><tt><xsl:value-of select="."/></tt></font></td> </xsl:when> <xsl:otherwise> <td align="center" bgcolor="#FFFFFF"><font size="2"><tt><xsl:value-of select="."/></tt></font></td> </xsl:otherwise></xsl:choose><!-- variabili per calcolo tempo --><xsl:variable name="inizio_min"><xsl:value-of select="./@ini_h * 60 + ./@ini_m"/></xsl:variable><xsl:variable name="fine_min"><xsl:value-of select="./@fine_h * 60 + ./@fine_m"/></xsl:variable><xsl:variable name="durata_minuti"><xsl:value-of select="$fine_min - $inizio_min"/></xsl:variable><xsl:variable name="durata_ore"><xsl:value-of select="$durata_minuti div 60"/></xsl:variable><!-- atti dovuti --><xsl:choose> <xsl:when test="./@cat='dovute'"> <td align="center"><font size="2"><tt><xsl:value-of select='format-number($durata_ore, "##.000")'/></tt></font></td> </xsl:when> <xsl:otherwise><td></td></xsl:otherwise></xsl:choose><!-- 40 ore --><xsl:choose> <xsl:when test="./@cat='40'"> <td align="center"><font size="2"><tt><xsl:value-of select='format-number($durata_ore, "##.000")'/></tt></font></td> </xsl:when> <xsl:otherwise><td></td></xsl:otherwise></xsl:choose><!-- 33 ore --><xsl:choose> <xsl:when test="./@cat='33'"> <td align="center"><font size="2"><tt><xsl:value-of select='format-number($durata_ore, "##.000")'/></tt></font></td> </xsl:when> <xsl:otherwise><td></td></xsl:otherwise></xsl:choose><!-- fondo d'istituto --><xsl:choose> <xsl:when test="./@cat='fondo'"> <td align="center"><font size="2"><tt><xsl:value-of select='format-number($durata_ore, "##.000")'/></tt></font></td> </xsl:when> <xsl:otherwise><td></td></xsl:otherwise></xsl:choose></tr></xsl:for-each><!-- riga totali --><tr> <td align="center" bgcolor="#FFFFFF"></td> <td align="center" bgcolor="#FFFFFF"><img src="cogs.gif" border="0" height="26" width="32"></img></td> <td align="center" bgcolor="#EEEEFF"><font size="2"><tt>T O T A L E (in ore)</tt></font></td> <td align="center" bgcolor="#BB9966"><font size="2"><tt>?</tt></font></td> <td align="center" bgcolor="#CC9933"><font size="2"><tt>?</tt></font></td> <td align="center" bgcolor="#DDAA66"><font size="2"><tt>?</tt></font></td> <td align="center" bgcolor="#EEBB99"><font size="2"><tt>?</tt></font></td></tr></table></center></body></html></xsl:template></xsl:stylesheet>--------------------------------------------

Link to comment
Share on other sites

I'm really not following.What forms a "row" from the source XML? What values from it? Can't you possibly isolate such nodes into a context?Consider outputting a cleaner XHTML. Then, one solution would be to send the output as XML with another PI to a stylesheet, that would then be able to get all cells from the (now proper) row.

Link to comment
Share on other sites

I'm really not following.What forms a "row" from the source XML? What values from it? Can't you possibly isolate such nodes into a context?Consider outputting a cleaner XHTML. Then, one solution would be to send the output as XML with another PI to a stylesheet, that would then be able to get all cells from the (now proper) row.
The values in the rows on the right of the output table are the result of a variable ($durata_ore).You can't find those values in the XML file.I would like to get the total sum of each column and put the total amount at the bottow row of the table.I can't use the sum() operator in the XSL file because the sum() operator only accepts nodes.
Link to comment
Share on other sites

In that case, like I said, consider outputting without the total, but add a PI pointing to this or (better yet) another stylesheet that would then be able to get the total based on the output from the first source.

Link to comment
Share on other sites

I disagree. With a little imagination and work, you can do what you want. The data is in the XML, therefore we can get it out. I modified your XSLT just enough to total one column. You can do the rest.

<!-- riga totali --><xsl:variable name="total_inizio_min" select="sum(//entry[@cat='dovute']/@ini_h) * 60 + sum(//entry[@cat='dovute']/@ini_m)" /><xsl:variable name="total_fine_min"   select="sum(//entry[@cat='dovute']/@fine_h) * 60 + sum(//entry[@cat='dovute']/@fine_m)"/><xsl:variable name="total_durata_minuti" select="$total_fine_min - $total_inizio_min"/><xsl:variable name="total_durata_ore" select="format-number($total_durata_minuti div 60,'##.000')"/><tr><td align="center" bgcolor="#FFFFFF"></td><td align="center" bgcolor="#FFFFFF"><img src="cogs.gif" border="0" height="26" width="32"></img></td><td align="center" bgcolor="#EEEEFF"><font size="2"><tt>T O T A L E (in ore)</tt></font></td><td align="center" bgcolor="#BB9966"><font size="2"><tt><xsl:value-of select="$total_durata_ore" /></tt></font></td>

Link to comment
Share on other sites

I disagree. With a little imagination and work, you can do what you want. The data is in the XML, therefore we can get it out.
You are right. And it works!Thanks a lot for your help. I definitely need to study (and practice) some more.Alessandro
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...