xsl-list
[Top] [All Lists]

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

2006-07-17 11:54:28
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(_at_)optushome(_dot_)com(_dot_)au> 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="{(_at_)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>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

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