1

我正在尝试根据特定 ID 和 YTD 计算总数。我到处寻找,似乎无法找出解决方案。XML 文档与此类似:

<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

我可以很好地获得 Subtotal 行,问题是我无法通过特定 ID 获得内联 YTD 总计。我可能会让这比现在更难。

4

1 回答 1

0

首先,对于您的小计行,您目前正在执行此操作

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

这有几个问题。首先,您可能应该使用previous -sibling,而不是previous,否则如果它们出现在您的XML 中,它可能会选择前几年其次,小计只需要汇总您正在查看的当前薪水/小时数中存在@HID属性的先前值。

所以你需要有点害怕的表情

<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模板,您将使用previous -sibling进行总计

<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>
于 2012-09-20T08:17:19.523 回答