我有一个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点添加表时,它出错了。我只需要能够在那里完成。
<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[.!=""])"/>
</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!=""]/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!=""]/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>
答案 0 :(得分:1)
您可以将这些内容用作实际解决方案的骨架:
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> </th>
<th>
<xsl:value-of select="LS_Company"/>
</th>
</tr>
<xsl:apply-templates select="LS_DepositDate_Record"/>
<tr>
<th> </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"> </th>
<th>
<xsl:value-of select="LS_Deposit_Date"/>
</th>
</tr>
<xsl:apply-templates select="LS_Check_Record"/>
<tr>
<th colspan="2"> </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"> </td>
<td>
<xsl:value-of select="LS_Check_Number"/>
</td>
<td>
<xsl:value-of select="LS_Check_Amount"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
应用于您的示例输入,渲染结果将如下所示: