Re: [xsl] Re: Keeping a running total?

Subject: Re: [xsl] Re: Keeping a running total?
From: Steve <subsume@xxxxxxxxx>
Date: Mon, 17 Jul 2006 17:01:18 -0400
You share my confusion. Is there some obvious difference between my
interpretation of the factory template application?

This morning, I was able to call the template in such a way as to
achieve recursion (except it kept going beyond a perfectly
well-presented result set). Now, but of course, I can't figure out how
I did that.

I was so sure I was going to have this beast tackled today, too. =( 5 o'clock.

-Steve

On 7/17/06, didoss@xxxxxxxxxxx <didoss@xxxxxxxxxxx> wrote:
Steve -

Maybe I'm confused, but it looks like you are not keeping a running total between services,...I read the template as saying that A should be able to handle 1920.

From previous discussions, I would think that means that A can handle a total of 1920, and therefore by service 4, should only be able to handle (1920 - 191.25 - 532.25 - 32) more, and then B and C kick in. So, by service 5, everybody is already maxed out, and there is no one to handle it.

What I see below leads me to believe that it resets the counter with each service, and that's why A is able to handle more in service 5.


Dianne


 -------------- Original message ----------------------
From: Steve <subsume@xxxxxxxxx>
> Your solution is wonderful, however, I have one problem while applying it.
>
> In item 4 both the A and B thresholds are reached. It spills over as
> expected to C but then A becomes its Quota, 1920.
>
> Exhaustive, sorry.
>
> XML-------------
>
> <Records>
>       <Record>
>               <service>1</service>
>               <hours>191.25</hours>
>       </Record>
>       <Record>
>               <service>2</service>
>               <hours>532.25</hours>
>       </Record>
>       <Record>
>               <service>3</service>
>               <hours>32</hours>
>       </Record>
>       <Record>
>               <service>4</service>
>               <hours>2858.5</hours>
>       </Record>
>       <Record>
>               <service>5</service>
>               <hours>244.3</hours>
>       </Record>
> <!--  etc -->
> Output-----------
>
> <out service="1" Total="191.25" A="191.25" B="0" C="0" Other="0" />
> <out service="2" Total="532.25" A="532.25" B="0" C="0" Other="0"  />
> <out service="3" Total="32" A="32" partB="0" C="0" Other="0"  />
> <out service="4" Total="2858.5" A="1920" B="350" C="588.5" Other="0"  />
> <out service="5" Total="244.3" A="244.3" B="0" C="0" Other="0" />
>              (etc....)
> XSL--------------
>
> <xsl:template match="/" >
>       <xsl:apply-templates select="$services//Record">
>               <xsl:with-param name="leftA" select="1920" />
>               <xsl:with-param name="leftB" select="350" />
>               <xsl:with-param name="leftC" select="878" />
>       </xsl:apply-templates>
> </xsl:template>
>
> <!--
> ***When I replace the above $services//Record with $services/Records
> (the parent) all output looks like <out service='x' total='T' A='0'
> B='0' C='0' Other='T' />
> -->
>
>
> <xsl:template match="Record">
>       <!-- running totals of hours already used   -->
>       <xsl:param name="used">0</xsl:param>
>       <xsl:param name="usedA">0</xsl:param>
>       <xsl:param name="usedB">0</xsl:param>
>       <xsl:param name="usedC">0</xsl:param>
>       <!-- running totals of quota not used   -->
>       <xsl:param name="leftA">0</xsl:param>
>       <xsl:param name="leftB">0</xsl:param>
>       <xsl:param name="leftC">0</xsl:param>
>       <xsl:choose>
>               <!-- Capacity used up, output used and move on   -->
>               <xsl:when test="(hours = $used)">
>                       <out service="{service}" Total = "{hours}" A="{$usedA}"
> partB="{$usedB}" C="{$usedC}" Other="{$used - ($usedA + $usedB +
> $usedC)}" />
>                       <xsl:apply-templates
> select="following-sibling::Record[0]">
>                               <xsl:with-param name="leftA" select="$leftA" />
>                               <xsl:with-param name="leftB" select="$leftB" />
>                               <xsl:with-param name="leftC" select="$leftC" />
>                       </xsl:apply-templates>
>               </xsl:when>
>               <xsl:when test="not($leftA = 0)">
>                       <xsl:choose>
>                               <xsl:when test="($leftA &gt;= (hours - $used))">
>                                       <xsl:apply-templates select=".">
>                                               <xsl:with-param name="used"
> select="hours" />
>                                               <xsl:with-param name="usedA"
> select="($usedA + hours - $used)" />
>                                               <xsl:with-param name="leftA"
> select="($leftA - (hours - $used))" />
>                                               <xsl:with-param name="leftB"
> select="$leftB" />
>                                               <xsl:with-param name="leftC"
> select="$leftC" />
>                                       </xsl:apply-templates>
>                               </xsl:when>
>                               <xsl:otherwise>
>                                       <xsl:apply-templates select=".">
>                                               <xsl:with-param name="used"
> select="($used + $leftA)" />
>                                               <xsl:with-param name="usedA"
> select="($usedA + $leftA)" />
>                                               <xsl:with-param name="leftB"
> select="$leftB" />
>                                               <xsl:with-param name="leftC"
> select="$leftC" />
>                                       </xsl:apply-templates>
>                               </xsl:otherwise>
>                       </xsl:choose>
>               </xsl:when>
>               <xsl:when test="not($leftB = 0)">
>                       <xsl:choose>
>                               <xsl:when test="($leftB &gt;= (hours - $used))">
>                                       <xsl:apply-templates select=".">
>                                               <xsl:with-param name="used"
> select="hours" />
>                                               <xsl:with-param name="usedA"
> select="$usedA" />
>                                               <xsl:with-param name="usedB"
> select="($usedB +     hours - $used)" />
>                                               <xsl:with-param name="leftB"
> select="($leftB - (hours - $used))" />
>                                               <xsl:with-param name="leftC"
> select="$leftC" />
>                                       </xsl:apply-templates>
>                               </xsl:when>
>                               <xsl:otherwise>
>                                       <xsl:apply-templates select=".">
>                                               <xsl:with-param name="used"
> select="($used + $leftB)" />
>                                               <xsl:with-param name="usedA"
> select="$usedA" />
>                                               <xsl:with-param name="usedB"
> select="($usedB + $leftB)" />
>                                               <xsl:with-param name="leftC"
> select="$leftC" />
>                                       </xsl:apply-templates>
>                               </xsl:otherwise>
>                       </xsl:choose>
>               </xsl:when>
>               <xsl:when test="not($leftC = 0)">
>                       <xsl:choose>
>                               <xsl:when test="($leftC &gt;= (hours - $used))">
>                                       <xsl:apply-templates select=".">
>                                               <xsl:with-param name="used"
> select="hours" />
>                                               <xsl:with-param name="usedA"
> select="$usedA" />
>                                               <xsl:with-param name="usedB"
> select="$usedB" />
>                                               <xsl:with-param name="leftB"
> select="($leftB - (hours - $used))"
> />                                                    <xsl:with-param
> name="usedC"  elect="($usedC + hours -
> $used)" />
>                                               <xsl:with-param name="leftC"
> select="($leftC -(hours - $used))" />
>                                       </xsl:apply-templates>
>                               </xsl:when>
>                               <xsl:otherwise>
>                                       <xsl:apply-templates select=".">
>                                               <xsl:with-param name="usedA"
> select="$usedA" />
>                                               <xsl:with-param name="usedB"
> select="$usedB" />
>                                               <xsl:with-param name="used"
> select="($used + leftB)" />
>                                               <xsl:with-param name="usedC"
> select="($usedC + $leftC)" />
>                                       </xsl:apply-templates>
>                               </xsl:otherwise>
>                       </xsl:choose>
>               </xsl:when>
>               <xsl:otherwise>
>                       <xsl:apply-templates select=".">
>                               <xsl:with-param name="used" select="hours" />
>                               <xsl:with-param name="usedA" select="$usedA" />
>                               <xsl:with-param name="usedB"  select="$usedB" />
>                               <xsl:with-param name="usedC"  select="$usedC" />
>                       </xsl:apply-templates>
>               </xsl:otherwise>
>       </xsl:choose>
> </xsl:template>
> -----
>
> On 7/12/06, Andrew Franz <afranz0@xxxxxxxxxxxxxxxx> wrote:
> > Okay, assuming the following input:
> >
> > <xml>
> >  <factory x="A" capacity = "3" />
> >  <factory x="B" capacity= "5" />
> >   <factory x="C" capacity = "3" />
> >   <factory x="D" capacity = "2" />
> >   <factory x="E" capacity = "2" />
> >   ...etc...
> > </xml>
> >
> >
> > O(N) algorithm using recursion (not tested):
> >
> > <xsl:template match="xml">
> >     <xsl:apply-templates select="factory">
> >         <xsl:with-param name="leftW" select="$Widget_quota" />
> >         <xsl:with-param name="leftG" select="$Gadget_quota" />
> >     <xsl:apply-templates select="factory">
> > </xsl:template>
> >
> > <xsl:template match="factory">
> >     <!-- running totals of capacity already used   -->
> >         <xsl:param name="used">0</xsl:param>
> >         <xsl:param name="usedW">0</xsl:param>
> >         <xsl:param name="usedG">0</xsl:param>
> >     <!-- running totals of quota not used   -->
> >         <xsl:param name="leftW">0</xsl:param>
> >         <xsl:param name="leftG">0</xsl:param>
> >
> >     <xsl:choose>
> >     <!-- Capacity used up, output used and move on   -->
> >     <xsl:when test="(@capacity = $used)">
> >             <out factory="{@x}" Widgets="{$usedW}" Gadgets="{$usedG}"
> > Excess="{$used - ($usedW + $usedG)}" />
> >             <xsl:apply-templates select="following-sibling::factory[1]">
> >                 <xsl:with-param name="leftW" select="$leftW" />
> >                 <xsl:with-param name="leftG" select="$leftG" />
> >             </xsl:apply-templates>
> >     </xsl:when>
> >
> >     <xsl:when test="not($leftW = 0)">
> >         <xsl:choose>
> >         <xsl:when test="($leftW &gt;= (@capacity - $used))">
> >                 <xsl:apply-templates select=".">
> >                     <xsl:with-param name="used" select="@capacity" />
> >                     <xsl:with-param name="usedW" select="($usedW +
> > @capacity - $used)" />
> >                     <xsl:with-param name="leftW" select="($leftW -
> > (@capacity - $used))" />
> >                     <xsl:with-param name="leftG" select="$leftG" />
> >                 </xsl:apply-templates>
> >         </xsl:when>
> >         <xsl:otherwise>
> >                 <xsl:apply-templates select=".">
> >                     <xsl:with-param name="used" select="($used + $leftQ)" />
> >                     <xsl:with-param name="usedW" select="($usedW +
> > $leftW)" />
> >                     <xsl:with-param name="leftG" select="$leftG" />
> >                 </xsl:apply-templates>
> >         </xsl:otherwise>
> >         </xsl:choose>
> >     </xsl:when>
> >
> >     <xsl:when test="not($leftG = 0)">
> >         <xsl:choose>
> >         <xsl:when test="($leftG &gt;= (@capacity - $used))">
> >                 <xsl:apply-templates select=".">
> >                     <xsl:with-param name="used" select="@capacity" />
> >                     <xsl:with-param name="usedW" select="$usedW" />
> >                     <xsl:with-param name="usedG"  select="($usedG +
> > @capacity - $used)" />
> >                     <xsl:with-param name="leftG" select="($leftG -
> > (@capacity - $used))" />
> >                 </xsl:apply-templates>
> >         </xsl:when>
> >         <xsl:otherwise>
> >                 <xsl:apply-templates select=".">
> >                     <xsl:with-param name="used" select="($used + $leftG)" />
> >                     <xsl:with-param name="usedW" select="$usedW" />
> >                     <xsl:with-param name="usedG" select="($usedG +
> > $leftG)" />
> >                 </xsl:apply-templates>
> >         </xsl:otherwise>
> >         </xsl:choose>
> >     </xsl:when>
> >
> >     <xsl:otherwise>
> >                 <xsl:apply-templates select=".">
> >                     <xsl:with-param name="used" select="@capacity" />
> >                     <xsl:with-param name="usedW" select="$usedW" />
> >                     <xsl:with-param name="usedG"  select="$usedG" />
> >                 </xsl:apply-templates>
> >     </xsl:otherwise>
> >
> >     </xsl:choose>
> > </xsl:template>

Current Thread