xsl-list
[Top] [All Lists]

Re: simple summation question

2002-12-31 01:27:40
The solution provided by Brian is O(N^2), a solution with linear
complexity is produced using the "scanl" template of the FXSL library:

stylesheet (testScanlt.xsl):
----------------------------
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:myAdd="f:myAdd"
xmlns:myParam="f:myParam"

  <xsl:import href="scanlDVC.xsl"/>
  <!-- to be applied on numList.xml -->
  
  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <myParam:myParam>0</myParam:myParam>
  
  <xsl:template match="/">
   
    <xsl:variable name="vFun" select="document('')/*/myAdd:*[1]"/>
    <xsl:variable name="vZero" select="document('')/*/myParam:*[1]"/>

    
    <xsl:call-template name="scanl">
      <xsl:with-param name="pFun" select="$vFun"/>
      <xsl:with-param name="pQ0" select="$vZero" />
      <xsl:with-param name="pList" select="/*/entity"/>
      <xsl:with-param name="pElName" select="'subTotal'"/>
    </xsl:call-template>
  </xsl:template>
    
  <myAdd:myAdd/>
  <xsl:template match="myAdd:*">
    <xsl:param name="pArg1" select="0"/>
    <xsl:param name="pArg2" select="0"/>
  
    <xsl:value-of select="$pArg1 + sum($pArg2/row/someValue)"/>
  </xsl:template>
  
</xsl:stylesheet>


When this is applied on your source xml:
---------------------------------------------
<report> 
  <entity>  
    <row>   
      <someValue>5</someValue>  
    </row>  
    <row>   
      <someValue>6</someValue>  
    </row>  
    <row>   
      <someValue>7</someValue>  
    </row> 
  </entity> 
  <entity>  
    <row>   
      <someValue>8</someValue>  
    </row>  
    <row>   
      <someValue>9</someValue>  
    </row>  
    <row>   
      <someValue>10</someValue>  
    </row> 
  </entity>
</report>


the result is:
-------------
<subTotal>0</subTotal>
<subTotal>18</subTotal>
<subTotal>45</subTotal>



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL



"Lee, Insoo" <Insoo(_dot_)Lee(_at_)gs(_dot_)com> wrote in message
news:3D0E38416579D411803C00D0B7AF6B21152120F2(_at_)gsny47e(_dot_)ny(_dot_)fw(_dot_)gs(_dot_)com(_dot_)(_dot_)(_dot_)

  Hello - a quick simple question, how would get a subTotal for
someValue by
entity as shown below?
  I can get a grand total by using
sum(/report/entity/row/someValue)..

  Thanks

   <report>
 <entity>
  <row>
   <someValue>5</someValue>
  </row>
  <row>
   <someValue>6</someValue>
  </row>
  <row>
   <someValue>7</someValue>
  </row>
 </entity>

 <entity>
  <row>
   <someValue>8</someValue>
  </row>
  <row>
   <someValue>9</someValue>
  </row>
  <row>
   <someValue>10</someValue>
  </row>
 </entity>
</report>



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


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

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



<Prev in Thread] Current Thread [Next in Thread>