[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Help cleaning up this 1.0 gem


Subject: [xsl] Help cleaning up this 1.0 gem
From: Steve <subsume@xxxxxxxxx>
Date: Fri, 5 Oct 2007 15:39:44 -0400

 In general, my company provides services and we get credit up to a
certain quota from the funding sources (A, B, C, OTHER).

I'm simply looking for suggestions to simplify this template in order
to a) increase maintainability b) solve some minor externalities.

Because we report to the nearest quarter hour, I had to pass the
difference because, after several recursive rounds of the below
template, the small bit rounded off adds up to significant
discrepencies with the original, unrounded, total. However, I'm still
finding that the total ($XshouldBe) is different than the recursive
total generated (by a quarter-hour).

Template can also be passed a Record/Activity but that can be ignored here.

$services variable goes like:
<records>
 <Record>
    <service>Service n</service>
    <hours>123</hours>
  </Record>
</records>

<xsl:template select="/">
	<xsl:apply-templates select="$services[1]">
		<xsl:with-param name="AshouldBe" select="$Atotal" />
		<xsl:with-param name="BshouldBe" select="$Btotal" />
		<xsl:with-param name="CshouldBe" select="$Ctotal" />

               <!-- below WF = "Without (funding source) Five". Since
its hours don't count -->

		<xsl:with-param name="Aratio" select="$SSAtotal div $totalWF" />
		<xsl:with-param name="Bratio" select="$partBtotal div $totalWF" />
		<xsl:with-param name="Cratio" select="$Ctotal div $totalWF" />
		<xsl:with-param name="OTHERratio" select="($totalWF - ( $Atotal +
$Btotal + $Ctotal ) )  div $totalWF" />
	</xsl:apply-templates>
</xsl:template>
<xsl:template match="Record">
	<xsl:param name="AshouldBe" />
	<xsl:param name="BshouldBe" />
	<xsl:param name="CshouldBe" />

        <!-- below 't' suffix = 'total' -->

	<xsl:param name="Allt">0</xsl:param>
	<xsl:param name="At">0</xsl:param>
	<xsl:param name="Ct">0</xsl:param>
	<xsl:param name="Bt">0</xsl:param>
	<xsl:param name="OTHERt">0</xsl:param>

        <!-- ratio - percent of hours allocated to each funding source -->

	<xsl:param name="Aratio"	/>
	<xsl:param name="Bratio" 	/>
	<xsl:param name="Cratio" 	/>
	<xsl:param name="OTHERratio" 	/>

        <!-- differences -->

	<xsl:param name="Adiff">0</xsl:param>
	<xsl:param name="Bdiff">0</xsl:param>
	<xsl:param name="Cdiff">0</xsl:param>

        <!-- needed because when service = 5, no allocation comes from
A,B, or C. -->
	<xsl:variable name="i_A">
		<xsl:choose>
			<xsl:when test="not(service) or service != 'Service
5"><xsl:value-of select="$Aratio" /></xsl:when>
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="i_B">
		<xsl:choose>
			<xsl:when test="not(service) or service != 'Service
5'"><xsl:value-of select="$Bratio" /></xsl:when>
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="i_C">
		<xsl:choose>
			<xsl:when test="not(service) or service != 'Service
5'"><xsl:value-of select="$Cratio" /></xsl:when>
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="i_OTHER">
		<xsl:choose>
			<xsl:when test="not(service) or service != 'Service
5'"><xsl:value-of select="$OTHERratio" /></xsl:when>
			<xsl:otherwise>1</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="All" select="round(hours * 4) div 4" />

       <!-- 'wo' = without. As in, without any correction for the
differences -->

	<xsl:variable name="Awo">
		<xsl:value-of select="$i_A * hours" />
	</xsl:variable>
	<xsl:variable name="Bwo">
		<xsl:value-of select="$i_B * hours" />
	</xsl:variable>
	<xsl:variable name="Cwo">
		<xsl:value-of select="$i_C * hours" />
	</xsl:variable>

      <!-- funding sources with the differential factored -->

	<xsl:variable name="A">
		<xsl:choose>
			<xsl:when test="$i_A != 0 and round( ( $Awo + $Adiff ) * 4) div 4 > 0">
				<xsl:value-of select="round( ( $Awo + $Adiff ) * 4) div 4" />
			</xsl:when>
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="B">
		<xsl:choose>
			<xsl:when test="$i_B != 0 and round( ( $Bwo + $Bdiff ) * 4) div 4 > 0">
				<xsl:value-of select="round( ( $Bwo + $Bdiff ) * 4) div 4" />
			</xsl:when>
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="C">
		<xsl:variable name="test" select="round( ( $Cwo + $Cdiff ) * 4) div 4" />
		<xsl:choose>
			<xsl:when test="$i_C != 0 and $test &gt; 0 and $test &lt;= $All">
				<xsl:value-of select="$test" />
			</xsl:when>
			<xsl:when test="$test > $All">
				<xsl:value-of select="$All" />
			</xsl:when>
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="OTHER">
		<xsl:choose>
			<xsl:when test="round($OTHERratio * hours * 4) div 4 > 0" >
				<xsl:value-of select="round($OTHERratio * hours * 4) div 4" />
			</xsl:when>
			<xsl:otherwise>
				0
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<tr>
		<td class="left">
			<xsl:choose>
				<xsl:when test="service">
                                          <xsl:value-of select="service" />
				</xsl:when>
				<xsl:otherwise>
			                 <xsl:value-of select="activity" />
				</xsl:otherwise>
			</xsl:choose>
		</td>
		<td><xsl:value-of select="$All" /></td>
		<td><xsl:value-of select="$A" /></td>
		<td><xsl:value-of select="$B" /></td>
		<td><xsl:value-of select="$C" /></td>
		<td><xsl:value-of select="$OTHER + ($All - $A - $B - $OTHER - $C)" /></td>
	</tr>
	<xsl:choose>
		<xsl:when test="following-sibling::Record[1]">
			<xsl:apply-templates select="following-sibling::Record[1]">
				<xsl:with-param name="AshouldBe" select="$AshouldBe" />
				<xsl:with-param name="BshouldBe" select="$BshouldBe" />
				<xsl:with-param name="CshouldBe" select="$CshouldBe" />
				<xsl:with-param name="Aratio" select="$Aratio" />
				<xsl:with-param name="Bratio" select="$Bratio" />
				<xsl:with-param name="Cratio" select="$Cratio" />
				<xsl:with-param name="OTHERratio" select="$OTHERratio" />
				<xsl:with-param name="Adiff">
					<xsl:choose>
						<xsl:when test="$i_A = 0">
							<xsl:value-of select="$Adiff" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<xsl:when test="$A = round( $Awo * 4 ) div 4">
									<xsl:value-of select="($Adiff + $Awo) - $A" />
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="$Awo - $A" />
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:with-param>
				<xsl:with-param name="Cdiff">
					<xsl:choose>
						<xsl:when test="$i_C = 0">
							<xsl:value-of select="$Cdiff" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<xsl:when test="$C = round( $Cwo * 4 ) div 4">
									<xsl:value-of select="($Cdiff + $Cwo) - $C" />
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="$Cwo - $C" />
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:with-param>
				<xsl:with-param name="Bdiff">
					<xsl:choose>
						<xsl:when test="$i_B = 0">
							<xsl:value-of select="$Bdiff" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<xsl:when test="$B = round( $Bwo * 4 ) div 4">
									<xsl:value-of select="($Bdiff + $Bwo) - $B" />
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="$Bwo - $B" />
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:with-param>
				<xsl:with-param name="Allt" select="$Allt + $All"/>
				<xsl:with-param name="At" select="$At + $A"/>
				<xsl:with-param name="Bt" select="$Bt + $B"/>
				<xsl:with-param name="Ct" select="$Ct + $C"/>
				<xsl:with-param name="OTHERt" select="$OTHERt + $OTHER"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:otherwise>
			<tr>
				<td style="text-align:right">Total</td>
				<td><xsl:value-of select="$Allt + $All" /></td>
				<td>
					<xsl:value-of select="$At + $A" />
					<xsl:if test="$At + $A != $AshouldBe"><font color="red"
class="screen">(<xsl:value-of select="$At + $A -
$AshouldBe"/>)</font></xsl:if>
				</td>
				<td>
					<xsl:value-of select="$Bt + $B" />
					<xsl:if test="$Bt + $B != $BshouldBe"><font color="red"
class="screen">(<xsl:value-of select="$Bt + $B -
$BshouldBe"/>)</font></xsl:if>
				</td>
				<td>
					<xsl:value-of select="$Ct + $C" />
					<xsl:if test="$Ct + $C != $CshouldBe"><font color="red"
class="screen">(<xsl:value-of select="$Ct + $C -
$CshouldBe"/>)</font></xsl:if>
				</td>
				<td><xsl:value-of select="$OTHERt + ($All - $A - $B - $OTHER - $C)
+ ($Allt - $At - $Bt - $OTHERt - $Ct)" /></td>
			</tr>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>


Current Thread