Subject: Re: [xsl] Re: Keeping a running total? From: Steve <subsume@xxxxxxxxx> Date: Mon, 17 Jul 2006 14:54:04 -0400 |
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.
<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 >= (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 >= (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 >= (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> -----
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 >= (@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 >= (@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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Re: Keeping a running tot, Florent Georges | Thread | [xsl] value-of node test., Steve |
RE: [xsl] A beef with XSLT Sometime, Michael Kay | Date | [xsl] XSL For-Each Help!, Rusty Morton |
Month |