1
我想根據特定ID以及YTD計算總計。我到處尋找,似乎無法找出解決方案。該XML文檔看起來與此類似:根據特定ID和前面的屬性計算YTD總計
<pay>
<pay_year year="Year-2011">
<paycheck date="Jun-20-2011">
<hours>
<hours_type HID="Reg" qty="38.75" pay="1115.25"/>
</hours>
</paycheck>
<paycheck date="Jul-05-2011">
<hours>
<hours_type HID="Reg" qty="76.21" pay="2193.60"/>
<hours_type HID="Hol" qty="7.75" pay="223.07"/>
</hours>
</paycheck>
<paycheck date="Jul-20-2011">
<hours>
<hours_type HID="Reg" qty="76.21" pay="2193.60"/>
<hours_type HID="Sic" qty="7.75" pay="223.07"/>
</hours>
</paycheck>
</pay_year>
</pay>
我使用的XSLT是:
<xsl:template match="hours">
<xsl:apply-templates select="hours_type">
<xsl:sort order="descending" />
</xsl:apply-templates>
<tr>
<th class="sub" colspan="" align="justify">Subtotal/Totals YTD</th>
<td class="sub"><xsl:value-of select="sum(hours_type/@qty)" /></td>
<td class="sub"><xsl:value-of select="format-number(sum(preceding::paycheck/hours/hours_type/@qty) + sum(hours_type/@qty), '###,###.##')"/></td>
<td class="sub">
<xsl:value-of select="format-number(sum(hours_type/@pay), '$###,##0.00')" />
</td>
<td class="sub">
<xsl:value-of select="format-number(sum(preceding::paycheck/hours/hours_type/@pay) + sum(hours_type/@pay), '$###,##0.00')"/>
</td>
</tr>
</xsl:template>
<xsl:template match="hours_type">
<xsl:if test="position()=1">
<xsl:apply_templates select="@qty" />
<!--Not Sure how to make this work here -->
<xsl:apply_templates select="@pay" />
<!--Not Sure how to make this work here -->
</xsl:if>
<xsl:if test="position()=2">
<xsl:apply_templates select="@qty" />
<!--Not Sure how to make this work here -->
<xsl:apply_templates select="@pay" />
<!--Not Sure how to make this work here -->
</xsl:if>
<xsl:if test="position()=3">
<xsl:apply_templates select="@qty" />
<!--Not Sure how to make this work here -->
<xsl:apply_templates select="@pay" />
<!--Not Sure how to make this work here -->
</xsl:if>
</xsl:template>
<xsl:template match="@qty|@pay">
<td align="justify"><xsl:value-of select="." /></td>
</xsl:template>
輸出會是這個樣子的HTML:
Date---------Hours Type-----------Qty-----YTD-------Amount-----Pay YTD
Jun-20-2011--Reg------------------38.75---38.75-----1115.25----1115.25
Subtotal--------------------------38.78---38.75-----1115.25----1115.25
Jul-05-2011--Reg------------------76.21---114.96----2193.60----3308.85
-------------Hol------------------7.75----7.75------223.07-----223.07
Subtotal--------------------------83.96---122.71----2416.67----3531.92
Jul-20-2011--Reg------------------76.21---191.17----2193.60----5502.45
-------------Sic------------------7.75----7.75------223.07-----223.07
Subtotal--------------------------83.96---198.92----2416.67----5725.52
我能得到小計行很好,問題是我無法通過特定ID獲取內聯YTD總計。我可能會比現在更加困難。
謝謝!這工作完美。我的確在努力讓它變得艱難! – Tazzy