2012-09-19 73 views
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總計。我可能會比現在更加困難。

回答

0

首先,爲您的小計行,你現在正在做這個

<xsl:value-of 
    select="format-number(sum(preceding::paycheck/hours/hours_type/@qty) 
    + sum(hours_type/@qty), '###,###.##')"/> 

有幾個與此問題。首先,您應該使用之前的兄弟,而不是之前的,因爲否則如果它們出現在您的XML中,它可能會提取前幾年。其次,小計只需要總計先前的值,其中@HID屬性存在於您正在查看的當前工資支票/小時中。

因此,你需要稍微scaried表達

<xsl:value-of 
    select="format-number(sum(../preceding-sibling::paycheck/hours/hours_type 
     [@HID=current()/hours_type/@HID]/@qty) 
     + sum(hours_type/@qty), '###,###.##')"/> 

同樣,對於您的hours_type模板,你會利用前同輩做共計

<xsl:value-of 
    select="@qty + sum(../../preceding-sibling::paycheck/hours/hours_type 
    [@HID = current()/@HID]/@qty)" /> 

嘗試遵循XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="pay_year"> 
     <table> 
     <tr> 
      <th>Date</th> 
      <th>Type</th> 
      <th>Qty</th> 
      <th>Qty YTD</th> 
      <th>Pay</th> 
      <th>Pay YTD</th> 
     </tr> 
     <xsl:apply-templates select="paycheck/hours" /> 
     </table> 
    </xsl:template> 


    <xsl:template match="hours"> 
     <xsl:apply-templates select="hours_type"> 
     <xsl:sort order="descending"/> 
     </xsl:apply-templates> 

     <tr> 
     <th class="sub" colspan="2" align="justify">Subtotal/Totals YTD</th> 
     <td class="sub"> 
      <xsl:value-of select="format-number(sum(hours_type/@qty), '###,##0.00')"/> 
     </td> 
     <td class="sub"> 
      <xsl:value-of select="format-number(sum(../preceding-sibling::paycheck/hours/hours_type[@HID=current()/hours_type/@HID]/@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-sibling::paycheck/hours/hours_type[@HID=current()/hours_type/@HID]/@pay) + sum(hours_type/@pay), '###,##0.00')"/> 
     </td> 
     </tr> 
    </xsl:template> 

    <xsl:template match="hours_type"> 
     <tr> 
     <td> 
      <xsl:value-of select="../../@date" /> 
     </td> 
     <td> 
      <xsl:value-of select="@HID" /> 
     </td> 
     <xsl:apply-templates select="@qty"/><!--Not Sure how to make this work here --> 
     <td align="justify"> 
      <xsl:value-of select="@qty + sum(../../preceding-sibling::paycheck/hours/hours_type[@HID = current()/@HID]/@qty)" /> 
     </td> 
     <xsl:apply-templates select="@pay"/><!--Not Sure how to make this work here --> 
     <td align="justify"> 
      <xsl:value-of select="@pay + sum(../../preceding-sibling::paycheck/hours/hours_type[@HID = current()/@HID]/@pay)" /> 
     </td> 
     </tr> 
    </xsl:template> 

    <xsl:template match="@qty|@pay"> 
     <td align="justify"> 
     <xsl:value-of select="."/> 
     </td> 
    </xsl:template> 
</xsl:stylesheet> 

當應用於您的示例XML時,輸出以下內容

<table> 
    <tr> 
     <th>Date</th> 
     <th>Type</th> 
     <th>Qty</th> 
     <th>Qty YTD</th> 
     <th>Pay</th> 
     <th>Pay YTD</th> 
    </tr> 
    <tr> 
     <td>Jun-20-2011</td> 
     <td>Reg</td> 
     <td align="justify">38.75</td> 
     <td align="justify">38.75</td> 
     <td align="justify">1115.25</td> 
     <td align="justify">1115.25</td> 
    </tr> 
    <tr> 
     <th class="sub" colspan="2" align="justify">Subtotal/Totals YTD</th> 
     <td class="sub">38.75</td> 
     <td class="sub">38.75</td> 
     <td class="sub">1,115.25</td> 
     <td class="sub">1,115.25</td> 
    </tr> 
    <tr> 
     <td>Jul-05-2011</td> 
     <td>Reg</td> 
     <td align="justify">76.21</td> 
     <td align="justify">114.96</td> 
     <td align="justify">2193.60</td> 
     <td align="justify">3308.85</td> 
    </tr> 
    <tr> 
     <td>Jul-05-2011</td> 
     <td>Hol</td> 
     <td align="justify">7.75</td> 
     <td align="justify">7.75</td> 
     <td align="justify">223.07</td> 
     <td align="justify">223.07</td> 
    </tr> 
    <tr> 
     <th class="sub" colspan="2" align="justify">Subtotal/Totals YTD</th> 
     <td class="sub">83.96</td> 
     <td class="sub">122.71</td> 
     <td class="sub">2,416.67</td> 
     <td class="sub">3,531.92</td> 
    </tr> 
    <tr> 
     <td>Jul-20-2011</td> 
     <td>Reg</td> 
     <td align="justify">76.21</td> 
     <td align="justify">191.17</td> 
     <td align="justify">2193.60</td> 
     <td align="justify">5502.45</td> 
    </tr> 
    <tr> 
     <td>Jul-20-2011</td> 
     <td>Sic</td> 
     <td align="justify">7.75</td> 
     <td align="justify">7.75</td> 
     <td align="justify">223.07</td> 
     <td align="justify">223.07</td> 
    </tr> 
    <tr> 
     <th class="sub" colspan="2" align="justify">Subtotal/Totals YTD</th> 
     <td class="sub">83.96</td> 
     <td class="sub">198.92</td> 
     <td class="sub">2,416.67</td> 
     <td class="sub">5,725.52</td> 
    </tr> 
</table> 
+0

謝謝!這工作完美。我的確在努力讓它變得艱難! – Tazzy