tags:

views:

56

answers:

3

I want to use XSLT to calculate the summation value of amount

the input is:

<FileHeader>
    <Item amount="500" />                  
    <Item amount="600" />                  
    <Item amount="400" />                  
    <Item amount="700" />                  
    <Item amount="100" />                  
    <Item amount="900" />                  
    <Item amount="1000" />                 
    <Item amount="200" />                  
    <Item amount="700" />                  
</FileHeader>

The output should be:

<Result>
  <FileSummary TotalAmount="5100">
</Result>

Thanks,

+3  A: 
<Result>
    <FileSummary TotalAmount="{sum(/FileHeader/Item/@amount)}" />
</Result>

Tested. Fixed typo. This should work.

Matt Ellen
it worked thanks :) just with typo error: "Item" instead of "Iteam"
ala
no problem :) I found the information here: http://msdn.microsoft.com/en-us/library/ms256160.aspx
Matt Ellen
+1  A: 

Here's an example of how this can be done:
XSLT: Sum of products from multiple nodes

Dana
+1  A: 

Try this :

<Result>
  <FileSummary>
      <xsl:attribute name="TotalAmount">
     <xsl:value-of select="sum(//FileHeader/Item/@amount)" />
      </xsl:attribute>
  </FileSummary>
</Result>
Canavar
Note that the `//` operator should be avoided as long as possible. Its performance is bad, especially as the input documents get larger.
Tomalak