xsl summing/totals 当前使用 for-each 的嵌套 xsl 2.0

xsl summing/totals nested xsl 2.0 currently using for-each

我有一个 xml,我需要在 3 个不同级别中求和。我有最内层和最外层的工作,但我无法让内部的正常工作。我正在使用 for-each,因为这是我最熟悉的,但它变得非常长。我希望报告有一个总计,每个存款日期变化的总计和每个公司变化的总计。

这是我的 xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!--Sample XML file generated by XMLSpy v2008 sp1 (http://www.altova.com)-->
<LS_Daily_Checks_Processed Process_Date="2014-10-13">
  <LS_User_Record>
    <LS_UserName>Donna</LS_UserName>
    <LS_Company_Record>
      <LS_Company>10</LS_Company>
      <LS_DepositDate_Record>
        <LS_Deposit_Date>10/10/2014</LS_Deposit_Date>
        <LS_Check_Record>
          <LS_Lockbox_Number>CHI-62303</LS_Lockbox_Number>
          <LS_Bank_Batch_Number>1</LS_Bank_Batch_Number>
          <LS_Time_Processed></LS_Time_Processed>
          <LS_Check_Number>1234</LS_Check_Number>
          <LS_Check_Amount>100.00</LS_Check_Amount>
        </LS_Check_Record>
        <LS_Check_Record>
          <LS_Lockbox_Number>CHI-62303</LS_Lockbox_Number>
          <LS_Bank_Batch_Number>2</LS_Bank_Batch_Number>
          <LS_Time_Processed></LS_Time_Processed>
          <LS_Check_Number>98989</LS_Check_Number>
          <LS_Check_Amount>150.00</LS_Check_Amount>
        </LS_Check_Record>
      </LS_DepositDate_Record>
    </LS_Company_Record>
    <LS_Company_Record>
      <LS_Company>1</LS_Company>
      <LS_DepositDate_Record>
        <LS_Deposit_Date>10/09/2014</LS_Deposit_Date>
        <LS_Check_Record>
          <LS_Lockbox_Number>CHI-62303</LS_Lockbox_Number>
          <LS_Bank_Batch_Number>2</LS_Bank_Batch_Number>
          <LS_Time_Processed></LS_Time_Processed>
          <LS_Check_Number>9998</LS_Check_Number>
          <LS_Check_Amount>300.00</LS_Check_Amount>
        </LS_Check_Record>
      </LS_DepositDate_Record>
    </LS_Company_Record>
  </LS_User_Record>
  <LS_User_Record>
    <LS_UserName>Rob</LS_UserName>
    <LS_Company_Record>
      <LS_Company>10</LS_Company>
      <LS_DepositDate_Record>
        <LS_Deposit_Date>10/10/2014</LS_Deposit_Date>
        <LS_Check_Record>
          <LS_Lockbox_Number>CHI-74223</LS_Lockbox_Number>
          <LS_Bank_Batch_Number>2</LS_Bank_Batch_Number>
          <LS_Time_Processed></LS_Time_Processed>
          <LS_Check_Number>5432</LS_Check_Number>
          <LS_Check_Amount>222.99</LS_Check_Amount>
        </LS_Check_Record>
      </LS_DepositDate_Record>
    </LS_Company_Record>
    <LS_Company_Record>
      <LS_Company>1</LS_Company>
      <LS_DepositDate_Record>
        <LS_Deposit_Date>10/10/2014</LS_Deposit_Date>
        <LS_Check_Record>
          <LS_Lockbox_Number>CHI-62303</LS_Lockbox_Number>
          <LS_Bank_Batch_Number>3</LS_Bank_Batch_Number>
          <LS_Time_Processed></LS_Time_Processed>
          <LS_Check_Number>9999</LS_Check_Number>
          <LS_Check_Amount>299.99</LS_Check_Amount>
        </LS_Check_Record>
      </LS_DepositDate_Record>
    </LS_Company_Record>
  </LS_User_Record>
</LS_Daily_Checks_Processed>

这是我正在尝试的 xslt,但没有按公司添加支票或正确显示它。当我在 LS_Company_Record 位置添加 table 时出现了问题。我只需要能够在那里总计。

<xsl:spreadsheet>
  <xsl:variable name="XML" select="/" />
  <xsl:template match="/">
    <html>

    <head>
      <title/>
    </head>

    <body>
      <span style="font-family:Arial; font-size:larger; ">
    <xsl:text>LS Daily Checks Processed Report for </xsl:text>
    </span>
      <xsl:for-each select="$XML">
        <xsl:for-each select="n1:LS_Daily_Checks_Processed">
          <xsl:for-each select="@Process_Date">
            <span style="background-color:transparent; color:#801000; font-family:Arial; font-size:larger; ">
    <xsl:value-of select="string(.)"/>
    </span>
          </xsl:for-each>
        </xsl:for-each>
        <br/>
        <br/>
        <br/>
        <table style="font-family:Arial; " border="1">
          <thead>
            <tr style="background-color:#c04b00; ">
              <th>
                <span>
    <xsl:text>Name</xsl:text>
    </span>
              </th>
              <th>
                <span>
    <xsl:text>Company</xsl:text>
    </span>
              </th>
              <th>
                <span>
    <xsl:text>Deposit Date</xsl:text>
    </span>
              </th>
              <th>
                <span>
    <xsl:text>Checks Processed</xsl:text>
    </span>
              </th>
            </tr>
          </thead>
          <tfoot>
            <xsl:for-each select="/">
              <tr style="background-color:#FF0000;">
                <td>
                  <span style="font-weight:bold; ">
    <xsl:text>Total Checks Processed:</xsl:text>
    </span>
                </td>
                <td>
                  <span style="font-weight:bold;">
    <xsl:value-of select="count(//n1:LS_Check_Number[.!=&quot;&quot;])"/>
    </span>
                </td>
                <td>
                  <span style="font-weight:bold;">
    <xsl:value-of select="format-number(number(sum  //n1:LS_Check_Amount)),'###,##0.00')"/>
    </span>
                </td>
              </tr>
            </xsl:for-each>
          </tfoot>
          <tbody>
            <xsl:for-each select="n1:LS_Daily_Checks_Processed">
              <xsl:for-each select="n1:LS_User_Record">
                <tr>
                  <td style="text-align:left; vertical-align:top; ">
                    <xsl:for-each select="n1:LS_UserName">
                      <span style="font-weight:bold; text-align:left; ">
    <xsl:apply-templates/>
    </span>
                    </xsl:for-each>
                  </td>
                </tr>
                <xsl:for-each select="n1:LS_Company_Record">
                  <tr>
                    <table style="text-align:right;" border="1">
                      <thead>
                        <tr>
                          <td></td>
                          <td style="text-align:left; vertical-align:top; ">
                            <xsl:for-each select="n1:LS_Company">
                              <span style="font-weight:bold; text-align:left; ">
    <xsl:apply-templates/>
    </span>
                            </xsl:for-each>
                          </td>
                          <td></td>
                        </tr>
                      </thead>
                      <tfoot>
                        <tr style="background-color:#ff8000; ">
                          <td></td>
                          <td>
                            <span style="font-weight:bold; ">
    <xsl:text>Checks Per Company:</xsl:text>
    </span>
                          </td>
                          <td>
                            <span style="font-weight:bold; ">
    <xsl:value-of select="format-number(number(count(  /LS_DepositDate_Record/LS_Check_Record[n1:LS_Check_Number!=&quot;&quot;]/n1:LS_Check_Number  )), '##0')"/>
    </span>
                          </td>
                        </tr>
                      </tfoot>
                      <tbody>
                        <tr>
                          <td></td>
                          <td></td>
                          <xsl:for-each select="n1:LS_DepositDate_Record">
                            <tr>
                              <td>
                              </td>
                              <td>
                              </td>
                              <td style="text-align:left; vertical-align:top;">
                                <xsl:for-each select="n1:LS_Deposit_Date">
                                  <span style="font-weight:bold; text-align:left">
    <xsl:apply-templates/>
    </span>
                                </xsl:for-each>
                              </td>
                              <td>
                                <table style="text-align:right; " border="1">
                                  <thead>
                                    <tr style="background-color:#ff8000; ">
                                      <th>
                                        <span>
    <xsl:text>Check Number</xsl:text>
    </span>
                                      </th>
                                      <th>
                                        <span>
    <xsl:text>Check Amount</xsl:text>
    </span>
                                      </th>
                                      <th>
                                        <span>
    <xsl:text>Lockbox</xsl:text>
    </span>
                                      </th>
                                      <th>
                                        <span>
    <xsl:text>Bank Batch Number</xsl:text>
    </span>
                                      </th>
                                      <th>
                                        <span>
    <xsl:text>Report Time</xsl:text>
    </span>
                                      </th>
                                    </tr>
                                  </thead>
                                  <tfoot>
                                    <tr style="color:#801000; ">
                                      <td>
                                        <span style="font-weight:bold; ">
    <xsl:value-of select="format-number(number(count(  n1:LS_Check_Record[n1:LS_Check_Number!=&quot;&quot;]/n1:LS_Check_Number  )), '##0')"/>
    </span>
                                      </td>
                                      <td>
                                        <span style="font-weight:bold; ">
    <xsl:value-of select="format-number(number(sum    (n1:LS_Check_Record/n1:LS_Check_Amount)), '###,##0.00')"/>
    </span>
                                      </td>
                                      <td/>
                                      <td/>
                                      <td/>
                                    </tr>
                                  </tfoot>
                                  <tbody>
                                    <xsl:for-each select="n1:LS_Check_Record">
                                      <tr>
                                        <td>
                                          <xsl:for-each select="n1:LS_Check_Number">
                                            <xsl:apply-templates/>
                                          </xsl:for-each>
                                        </td>
                                        <td>
                                          <xsl:for-each select="n1:LS_Check_Amount">
                                            <span>
    <xsl:value-of select="format-number(number(string(.)), '###,##0.00')"/>
    </span>
                                          </xsl:for-each>
                                        </td>
                                        <td>
                                          <xsl:for-each select="n1:LS_Lockbox_Number">
                                            <xsl:apply-templates/>
                                          </xsl:for-each>
                                        </td>
                                        <td>
                                          <xsl:for-each select="n1:LS_Bank_Batch_Number">
                                            <xsl:apply-templates/>
                                          </xsl:for-each>
                                        </td>
                                        <td style="background-color:transparent; ">
                                          <xsl:for-each select="n1:LS_Time_Processed">
                                            <xsl:apply-templates/>
                                          </xsl:for-each>
                                        </td>
                                      </tr>
                                    </xsl:for-each>
                                  </tbody>
                                </table>
                              </td>
                            </tr>
                          </xsl:for-each>
                        </tr>
                      </tbody>
                    </table>
                  </tr>
                </xsl:for-each>
              </xsl:for-each>
            </xsl:for-each>
          </tbody>
        </table>
      </xsl:for-each>
    </body>

    </html>
  </xsl:template>
  </xsl:stylesheet>

  ​

您可以将以下内容用作实际解决方案的框架:

XSLT 1.0(或 2.0)

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

<xsl:template match="/LS_Daily_Checks_Processed">
    <h3>
        <xsl:text>LS Daily Checks Processed Report for </xsl:text>
        <xsl:value-of select="@Process_Date"/>
    </h3>
    <table border="1">
        <thead>
            <tr>
                <th>User</th>
                <th>Company</th>
                <th>Date</th>
                <th>Check Number</th>
                <th>Check Amount</th>
            </tr>
        </thead>
        <tbody>
            <xsl:apply-templates select="LS_User_Record"/>
        </tbody>
        <tfoot>
            <tr>
                <th colspan="4">Grand Total:</th>   
                <th>    
                    <xsl:value-of select="sum(LS_User_Record/LS_Company_Record/LS_DepositDate_Record/LS_Check_Record/LS_Check_Amount)"/>
                </th>
            </tr>
        </tfoot>
    </table>
</xsl:template> 

<xsl:template match="LS_User_Record">
    <tr>
        <th>
            <xsl:value-of select="LS_UserName"/>
        </th>   
    </tr>
    <xsl:apply-templates select="LS_Company_Record"/>
    <tr>
        <th colspan="4">User Total:</th>    
        <th>    
            <xsl:value-of select="sum(LS_Company_Record/LS_DepositDate_Record/LS_Check_Record/LS_Check_Amount)"/>
        </th>   
    </tr>
</xsl:template> 

<xsl:template match="LS_Company_Record">
    <tr>
        <th>&#160;</th>
        <th>
            <xsl:value-of select="LS_Company"/>
        </th>   
    </tr>
    <xsl:apply-templates select="LS_DepositDate_Record"/>
    <tr>
        <th>&#160;</th>
        <th colspan="3">Company Total:</th> 
        <th>    
            <xsl:value-of select="sum(LS_DepositDate_Record/LS_Check_Record/LS_Check_Amount)"/>
        </th>   
    </tr>   
</xsl:template> 

<xsl:template match="LS_DepositDate_Record">
    <tr>
        <th colspan="2">&#160;</th>
        <th>
            <xsl:value-of select="LS_Deposit_Date"/>
        </th>   
    </tr>
    <xsl:apply-templates select="LS_Check_Record"/>
    <tr>
        <th colspan="2">&#160;</th>
        <th colspan="2">Day Total:</th> 
        <th>    
            <xsl:value-of select="sum(LS_Check_Record/LS_Check_Amount)"/>
        </th>   
    </tr>   
</xsl:template> 

<xsl:template match="LS_Check_Record">
    <tr>
        <td colspan="3">&#160;</td>
        <td>
            <xsl:value-of select="LS_Check_Number"/>
        </td>
        <td>
            <xsl:value-of select="LS_Check_Amount"/>
        </td>
    </tr>
</xsl:template>         

</xsl:stylesheet>

应用于您的示例输入,呈现的结果将如下所示: