Search code examples
xmlpdfxsltxsl-foapache-fop

Addind values over loop in FOP with XSL and XML


I have this XML and XSL example:

XML:

<mastercount>
  <sourceval>
    <attm>50</attm>
    <fh>6500</fh>
    <id>1</id>
  </sourceval>
  <sourceval>
    <attm>15</attm>
    <fh>2300</fh>
    <id>2</id>
  </sourceval>
  <sourceval>
    <attm>4</attm>
    <fh>280</fh>
    <id>3</id>
  </sourceval>
  <sourceval>
    <attm>20</attm>
    <fh>2700</fh>
    <id>4</id>
  </sourceval>
</mastercount>

XSL:

<xsl:variable name="var_idx">
  <xsl:value-of select="position()" />
</xsl:variable>
<xsl:variable name="var_sum_attm" />
<xsl:variable name="var_sum_fh" />
<fo:table-row>
    <xsl:for-each select="/mastercount">
        <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
            <fo:block>
                <xsl:value-of select="sourceval[position()=$var_idx]/attm" />               
            </fo:block>     
        </fo:table-cell>

        <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
            <fo:block>
                <xsl:value-of select="sourceval[position()=$var_idx]/fh" />
            </fo:block>
        </fo:table-cell>
    </xsl:for-each>
</fo:table-row>

This part of the source works great and I get

         attm  fh   attm    fh
 Val2     50 6500    0    6500 
 Val1     15 2300    0    2300 
 Val3    280    0  280       0 
 Val4     20 2700    0    2700 

(I skipped the name part above in the source)

However I now need a sum of those 2 fields:

         attm  fh   attm    fh
 Val2     50 6500    0    6500 
 Val1     15 2300    0    2300 
 Val3    280    0  280       0 
 Val4     20 2700    0    2700
        sum   sum  sum     sum

How can I get this to work?

Any ideas?

Thanks for your help. TheVagabond

UPDATE:

Thanks to RT72 here is an answer:

<xsl:for-each select="/mastercount">
    <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
      <fo:block>
        <xsl:value-of select="sum(sourceval/attm)"/>                                            
      </fo:block>                                       
    </fo:table-cell>
    <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right">
      <fo:block>
        <xsl:value-of select="sum(sourceval/fh)" />
      </fo:block>
    </fo:table-cell>
</xsl:for-each>

Solution

  • The following should work for you:

     <xsl:variable name="var_sum_attm" select="sum(/mastercount/sourceval/attm)"/>
     <xsl:variable name="var_sum_fh" select="sum(/mastercount/sourceval/fh)"/>
    

    Updated after OP edited question

    try this

    <xsl:for-each select="/mastercount/sourceval">
    <fo:table-row>                              
        <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
            <fo:block>
                <xsl:value-of select="attm" />
            </fo:block>
        </fo:table-cell>
        <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
            <fo:block>
                <xsl:value-of select="fh" />
             </fo:block>
         </fo:table-cell>
    </fo:table-row>
    

    <fo:table-row>                              
    <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
        <fo:block>
             <xsl:value-of select="sum(/mastercount/sourceval/attm)" />
        </fo:block>
    </fo:table-cell>
    <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
        <fo:block>
            <xsl:value-of select="sum(/mastercount/sourceval/fh)" />
         </fo:block>
     </fo:table-cell>
    </fo:table-row>