This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

XSLT: SUM function or "+" operator usage problems to display subtotal/total




Hi all,

I had a transform problems which  is related to some arithmetic operation
and displaying total/subtotal problems.

For example I have a XML like this

<Report >
   <Header>
        <ReportID>01</ReportID>
        <Title>Trade Report</Title>
   </Header>
   <Trade>
     <Record>
          <Ref>0001<Ref>
          <User>User1</User>
          </Item>
               <Product>ABC</Product>
               <Price>100.00</Price>
          <Item>
          </Item>
               <Product>CDE</Product>
               <Price>200.00</Price>
          <Item>
     </Record>
     <Record>
          <Ref>0002<Ref>
          <User>User2</User>
          </Item>
               <Product>ABC</Product>
               <Price>100.00</Price>
          <Item>
          </Item>
               <Product>CDE</Product>
               <Price>200.00</Price>
          <Item>
     </Record>
      <Trade>
</Report>


Output:

ID : 01      Trade Report

Product                                                 Price
---------------------------------------------------------
Sales : User 1

ABC                                           100.00
CDE                                           200.00
                                         ------------------
Subtotal                                    300.00


Sales : User 2

ABC                                           100.00
CDE                                           200.00
                                         ------------------
Subtotal                                    300.00
                                         -------------------
Total                                          600.00


my XML:


<xsl:variable name="rptid" select="Report/Header/ReportID/text()">
<xsl:variable name="title" select="Report/Header/Title/text()">
<xsl:variable name="subtotal">0</xsl:variable>
<xsl:variable name="total">0</xsl:variable>
( ... Is this something wrong here??? ..)

<xsl:template match="Report">
     <xsl:apply-template select="*[not(self::Header)]"/>
</xsl:template>

<xsl:template match="Trade">
     (.. Print Report Title...)

     <xsl:apply-templates select="Record"/>

     (....How to Print each trade total using total variable ????...)
     (... How to Reset total variable to 0 ???..)
</xsl:template>

<xsl:template match="Record">
     <xsl:for-each select="item">
          (.... Print each line ..)

          (... increment the total, subtotal by value of <Price>)

          <xsl:variable name="subtotal">
               <xsl:value-of select="$subtotal+number(Price)">
          </xsl:variable>
          <xsl:variable name="total">
               <xsl:value-of select="$total+number(Price)">
          </xsl:variable>
          (..... however, this does not work, would someone point me to the
right way of sum up in for-each loop ??...)
          (..... if possible, please show me sample code ??...)
     </xsl:for-each>

     (.... How to print out subtotal here and reset the subtotal to 0
???... )
</xsl:template>

Thank you very much in advance.

Albert Tsun




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]