469,338 Members | 8,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

Subtotals & Grand Total

XML
Expand|Select|Wrap|Line Numbers
  1. <nodeAA>AA</nodeAA>
  2.     <nodeBB>BB</nodeBB>
  3.         <nodeCC>
  4.             <From>12/05</From>
  5.             <To>11/06</To>
  6.             <Months>12</Months>
  7.             <Amount>10.00</Amount>
  8.             CC
  9.         </nodeCC>
  10.     </nodeBB>
  11.     <nodeDD>DD</nodeDD>
  12.         <nodeEE>
  13.             <From>12/06</From>
  14.             <To>11/07</To>
  15.             <Months>12</Months>
  16.             <Amount>20.00</Amount>
  17.             EE
  18.         </nodeEE>
  19.     </nodeDD>
  20. </nodeAA>
Desired Output
12/05 - 11/06 (12) 0010.00 = 0120.00
12/06 - 11/07 (12) 0020.00 = 0240.00

Grand Total: 0360.00
Nov 28 '08 #1
3 5532
Dormilich
8,651 Expert Mod 8TB
that seems not that difficult. for the number format you should read about the format-number() function. if you run into problems, we're around to assist you.

regards
Nov 28 '08 #2
jkmyoung
2,057 Expert 2GB
Agreed, most of it is easy, up to the grand total part.
For this, there are 2 recommended methods:
1. Recursive template, adding up the product from each part, pass sum as parameter or result
2. Create a temporary variable with the products. Then sum up using the sum function and the node-set function if necessary.

1. Pros: does not require extension functions.
Cons: uses more memory. Can break if there are too many nodes to calculate.

2. Pros: Requires only single pass.
Cons: Requires use of XSLT 2.0, or the node-set extension function, which differs across processors.

quick example of 1:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template name="addProducts">
  2.   <xsl:variable name="currentProduct" select="(*/Months) * (*/Amount)"/>
  3.   <xsl:choose>
  4.     <xsl:when test="not(following-sibling::*)">
  5.       <xsl:value-of select="$currentProduct"/>
  6.    </xsl:when>
  7.    <xsl:otherwise>
  8.       <xsl:variable name="$nextProductSums">
  9.          <xsl:for-each select="following-sibling::*[1]">
  10.             <xsl:call-template name="addProducts"/>
  11.          </xsl:for-each>
  12.        </xsl:variable>
  13.       <xsl:value-of select="$nextProductSums + $currentProduct"/>
  14.   </xsl:otherwise>
  15.   </xsl:choose>
  16. </xsl:template>
  17.  
Quick example of 2:
Expand|Select|Wrap|Line Numbers
  1. <xsl:variable name="products">
  2.    <xsl:for-each select="*">
  3.        <node>
  4.          <xsl:value-of select="(*/Months) * (*/Amount)"/>
  5.        </node>
  6.   </xsl:for-each>
  7. </xsl:variable>
  8.  
  9. <xsl:value-of select="sum($products/node)"/>
  10.  
Dec 1 '08 #3
* Note * I had to remove the $'s at lines 5 and 8 of example 1. I'm forced to use xml version 1.0

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <xsl:stylesheet
  3.     version="1.0"
  4.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  5.  
  6.     <xsl:template match="/">
  7.         <html>
  8.             <body>
  9.                 <head>
  10.                     <title>PHUS Transformation</title>
  11.                 </head>
  12.                 <h1>PHUS Transformation</h1>
  13.                 <!-- <xsl:apply-templates select="PHUS/pBICYEAR"/> -->
  14.                 <xsl:apply-templates select="PHUS"/>
  15.             </body>
  16.         </html>
  17.     </xsl:template>
  18.  
  19.     <xsl:template match="pBICYEAR">
  20.  
  21.             <!-- pBICYEAR attribute: pBicYearData, child node of pBICYEAR -->
  22.             <xsl:for-each select="@pBicYearData">
  23.  
  24.                 <!-- Tax Year Headers -->
  25.                 <font size="6"><b><xsl:value-of select="." /></b></font><br/>
  26.  
  27.             </xsl:for-each>
  28.  
  29.                 <!-- pTRANS node: child node of pBICYEAR -->
  30.                 <xsl:for-each select="pTRANS">
  31.                     <font color="brick"><xsl:value-of select="@TransData" /></font><br/>
  32.  
  33.                     <!-- Event Data -->
  34.                     <xsl:for-each select="pEVNT">
  35.                         <font color="red">
  36.                             <xsl:value-of select="EventID" />*
  37.                             <xsl:value-of select="Description" />*
  38.                             <xsl:value-of select="From" />*-
  39.                             <xsl:value-of select="To" />*
  40.                             (<xsl:value-of select="ElapsedMonths" />)*
  41.                             <xsl:value-of select="Amount" /> *
  42.  
  43.                             <xsl:value-of select = "format-number(ElapsedMonths * Amount, '#######.00')"/>
  44.  
  45.                         </font><br/>
  46.                     </xsl:for-each><br/>
  47.  
  48.                 </xsl:for-each>
  49.  
  50.     </xsl:template>
  51.  
  52.     <xsl:template name="addProducts">
  53.       <xsl:variable name="currentProduct" select="(*/ElapsedMonths) * (*/Amount)"/>
  54.       <xsl:choose> 
  55.        <xsl:when test="not(following-sibling::*)"> 
  56.          <xsl:value-of select="currentProduct"/> 
  57.       </xsl:when> 
  58.        <xsl:otherwise> 
  59.          <xsl:variable name="nextProductSums"> 
  60.             <xsl:for-each select="following-sibling::*[1]"> 
  61.                <xsl:call-template name="addProducts"/> 
  62.             </xsl:for-each> 
  63.           </xsl:variable> 
  64.          <xsl:value-of select="$nextProductSums + $currentProduct"/> 
  65.       </xsl:otherwise> 
  66.       </xsl:choose> 
  67.     </xsl:template>   
  68.  
  69. </xsl:stylesheet>
  70.  
  71. <!-- This template is placed here for reference -->
  72. <!-- <xsl:template name="addProducts"> 
  73.   <xsl:variable name="currentProduct" select="(*/Months) * (*/Amount)"/> 
  74.   <xsl:choose> 
  75.     <xsl:when test="not(following-sibling::*)"> 
  76.       <xsl:value-of select="$currentProduct"/> 
  77.    </xsl:when> 
  78.    <xsl:otherwise> 
  79.       <xsl:variable name="$nextProductSums"> 
  80.          <xsl:for-each select="following-sibling::*[1]"> 
  81.             <xsl:call-template name="addProducts"/> 
  82.          </xsl:for-each> 
  83.        </xsl:variable> 
  84.       <xsl:value-of select="$nextProductSums + $currentProduct"/> 
  85.   </xsl:otherwise> 
  86.   </xsl:choose> 
  87. </xsl:template> -->
Dec 1 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Andy V | last post: by
2 posts views Thread by Richard Grene | last post: by
4 posts views Thread by Jerry LeVan | last post: by
3 posts views Thread by Greg | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.