我有以下.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<upkeepList>
<upkShare month_year="11_16-12_16" post_dte="01-20-17" due="02-04-17">
<OpngBlnce>22</OpngBlnce>
<mnthCrrntAmnt>77</mnthCrrntAmnt>
<Rcpt dte="01-26-17" No="5725">
<amnt>22</amnt>
<descrpt>"11/16" upkeep </descrpt>
</Rcpt>
<Rcpt dte="01-26-17" No="5726">
<amnt>41</amnt>
<descrpt>"12/16" upkeep </descrpt>
</Rcpt>
</upkShare>
<upkShare month_year="01_17-02_17" post_dte="03-17-17" due="04-03-17">
<OpngBlnce></OpngBlnce> <!-- starting from this sect. this tag can be omitted as it'a calculated field -->
<mnthCrrntAmnt>74</mnthCrrntAmnt>
<Rcpt dte="03-30-17" No="5783">
<amnt>50</amnt>
<descrpt>"01/17-02/17" upkeep</descrpt>
</Rcpt>
</upkShare>
<upkShare month_year="03-17" post_dte="04-16-17" due="05-02-17">
<OpngBlnce></OpngBlnce>
<mnthCrrntAmnt>55</mnthCrrntAmnt>
<Rcpt dte="05-10-17" No="5815">
<amnt>40</amnt>
<descrpt>"03/17-04/17" upkeep 1</descrpt>
</Rcpt>
<Rcpt dte="05-15-17" No="5825">
<amnt>9</amnt>
<descrpt>"03/17-04/17" upkeep 2</descrpt>
</Rcpt>
</upkShare>
<upkShare month_year="04_17-05_17" post_dte="06-05-17" due="06-30-17">
<OpngBlnce></OpngBlnce>
<mnthCrrntAmnt>64</mnthCrrntAmnt>
<Rcpt dte="06-14-17" No="5858">
<amnt>37</amnt>
<descrpt>"05/17" upkeep 1</descrpt>
</Rcpt>
<Rcpt dte="06-18-17" No="5863">
<amnt>21</amnt>
<descrpt>"05/17" upkeep 2</descrpt>
</Rcpt>
</upkShare>
<upkShare month_year="06_17" post_dte="07-16-17" due="07-30-17">
<OpngBlnce></OpngBlnce>
<mnthCrrntAmnt>45</mnthCrrntAmnt>
<Rcpt dte="07-28-17" No="5948">
<amnt>38</amnt>
<descrpt>"06/17" upkeep</descrpt>
</Rcpt>
</upkShare>
<upkShare month_year="07_17" post_dte="08-16-17" due="08-31-17">
<OpngBlnce></OpngBlnce>
<mnthCrrntAmnt>54</mnthCrrntAmnt>
<Rcpt dte="07-28-17" No="6002">
<amnt>33</amnt>
<descrpt>"07/17" upkeep 1</descrpt>
</Rcpt>
<Rcpt dte="08-02-17" No="6017">
<amnt>12</amnt>
<descrpt>"07/17" upkeep 2</descrpt>
</Rcpt>
</upkShare>
</upkeepList>
并通过一些.xslt转换,我想以这种方式显示它:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>apartment's building monthly maintenance allowances share bills
</title>
</head>
<body>
<h3> apartment's monthly maintenance share bills (2017) </h3>
<table border="1" width="56%" style="text-align:center; margin-left:65px; border-collapse:collapse; margin-top:22px">
<tr> <th>PostDate</th> <!-- maintanance upkeep bills post date -->
<th>OpngBalnce</th> <!-- opening balance - OB (starting from 2nd line this OB will read previous CB-->
<th>MnthCrrntAmnt</th> <!-- monthly current upkeep amount to pay - MUPA -->
<th>TotAmntToPay</th> <!-- total amount to pay - TAP = OB + MUPA -->
<th>TotAmntPd</th> <!-- total amount paid (receipts sum) - TAPD -->
<th>ClsngBalnce</th> <!-- closing balance CB = TAP - TAPD -->
</tr>
<tr> <td>01-20-17</td> <!-- PostDate -->
<td>22</td> <!-- OB initially is 22 -->
<td>77</td> <!-- MUPA -->
<td>99</td> <!-- TAP = OB + MUPA -->
<td>63</td> <!-- TAPD -->
<td>36</td> <!-- CB = TAP - TAPD -->
</tr>
<tr> <!-- 2nd row -->
<td>03-17-17</td>
<td>36</td>
<td>74</td>
<td>110</td>
<td>50</td>
<td>60</td>
</tr>
<tr> <!-- 3rd row -->
<td>04-16-17</td>
<td>60</td>
<td>55</td>
<td>115</td>
<td>49</td>
<td>66</td>
</tr>
<tr> <!-- 4th row -->
<td>06-05-17</td>
<td>66</td>
<td>64</td>
<td>130</td>
<td>58</td>
<td>72</td>
</tr>
<tr><!-- 5th row -->
<td>07-16-17</td>
<td>72</td>
<td>45</td>
<td>117</td>
<td>38</td>
<td>79</td>
</tr>
<tr><!-- 6th row -->
<td>08-16-17</td> <!-- PostData -->
<td>79</td> <!-- OB = prev CB -->
<td>54</td> <!-- MUPA -->
<td>133</td> <!-- TAP = OB + MUPA -->
<td>43</td> <!-- TAPD -->
<td>90</td> <!-- CB = TAP - TAPD -->
</tr>
</table>
</body>
</html>
这是一个公寓大楼的每月维修津贴账单,我需要通过对该.xml文档的xslt转换来显示。
每间公寓都有一些公寓大楼共享账单,需要将其过帐。要建立的维护清单..人们知道他们必须为当月消费的公用事业支付多少费用。 用水/消耗量主要... 前面的表首字母缩写词的含义都将跟随(我将再次重复)。
OB =期初余额或初始余额;这是一个固定值,通过它我可以打开第1行的示例;从第二行开始,此OB与先前的期末余额(CB)相同。 例如:第2行的OB是第1行的CB(36) 第3行的OB是第2行的CB,依此类推(60);
MUPA =每月支付的当前保养金额;这是一个人应该为他/她使用的物品支付的保养费用;
TAP =总付款额; TAP = OB + MUPA-这是一个人要支付的总金额;
TAPD =支付的总金额(收款额);一个会用一张,两张甚至三张收据来支付一定的金额 已支付的所有收据的总和为已支付的总金额(TAPD);
CB =期末余额(CB)或应付款是从TAP中减去TAPD后的剩余金额(CB = TAP-TAPD)
诀窍在于,我只是想不出我应该如何将上一个CB带到下一行OB。
答案 0 :(得分:0)
请考虑以下简化示例:
这里的目标是产生类似于银行对账单的输出,其中每笔交易都是具有先前余额,金额和运行总额的一行。
转换过程分为两步:
首先,按日期对事务进行排序(此处,通过使用可以按文本排序的ISO-8601日期简化了任务),并且所有缺失值都被强制为零;
< / li>接下来,处理已排序的值,并通过对所有先前的交易值求和来生成先前的余额。这不是一种有效的方法-但由于它的简单性,对于相对少量的数据,它可能很方便。
XML
<transactions>
<transaction>
<date>2019-01-01</date>
<opening-balance>100</opening-balance>
<amount/>
</transaction>
<transaction>
<date>2019-03-03</date>
<opening-balance/>
<amount>33</amount>
</transaction>
<transaction>
<date>2019-02-02</date>
<opening-balance/>
<amount>-22</amount>
</transaction>
<transaction>
<date>2019-05-05</date>
<opening-balance/>
<amount>55</amount>
</transaction>
<transaction>
<date>2019-04-04</date>
<opening-balance/>
<amount>-44</amount>
</transaction>
</transactions>
XSLT 1.0(+ EXSLT)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:decimal-format name="coerce" NaN="0" />
<xsl:template match="/transactions">
<!-- FIRST PASS: SORT TRANSACTIONS BY DATE -->
<xsl:variable name="txs">
<xsl:for-each select="transaction">
<xsl:sort select="date"/>
<!-- COERCE MISSING VALUES TO ZERO -->
<tx date="{date}"
opening-balance="{format-number(opening-balance, '#', 'coerce')}"
amount="{format-number(amount, '#', 'coerce')}"/>
</xsl:for-each>
</xsl:variable>
<!-- OUTPUT -->
<statement>
<xsl:for-each select="exsl:node-set($txs)/tx">
<line>
<date>
<xsl:value-of select="@date"/>
</date>
<xsl:variable name="prev-balance" select="sum(preceding-sibling::tx/@opening-balance) + sum(preceding-sibling::tx/@amount) + @opening-balance" />
<previous-balance>
<xsl:value-of select="$prev-balance"/>
</previous-balance>
<amount>
<xsl:value-of select="@amount"/>
</amount>
<run-total>
<xsl:value-of select="@amount + $prev-balance"/>
</run-total>
</line>
</xsl:for-each>
</statement>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<statement>
<line>
<date>2019-01-01</date>
<previous-balance>100</previous-balance>
<amount>0</amount>
<run-total>100</run-total>
</line>
<line>
<date>2019-02-02</date>
<previous-balance>100</previous-balance>
<amount>-22</amount>
<run-total>78</run-total>
</line>
<line>
<date>2019-03-03</date>
<previous-balance>78</previous-balance>
<amount>33</amount>
<run-total>111</run-total>
</line>
<line>
<date>2019-04-04</date>
<previous-balance>111</previous-balance>
<amount>-44</amount>
<run-total>67</run-total>
</line>
<line>
<date>2019-05-05</date>
<previous-balance>67</previous-balance>
<amount>55</amount>
<run-total>122</run-total>
</line>
</statement>
一种更有效的方法是使用所谓的 sibling recursion 方法来消除相同节点一遍又一遍的重复求和:
XSLT 1.0(+ EXSLT)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:decimal-format name="coerce" NaN="0" />
<xsl:template match="/transactions">
<!-- FIRST PASS: SORT TRANSACTIONS BY DATE -->
<xsl:variable name="txs">
<xsl:for-each select="transaction">
<xsl:sort select="date"/>
<!-- COERCE MISSING VALUES TO ZERO -->
<tx date="{date}"
opening-balance="{format-number(opening-balance, '#', 'coerce')}"
amount="{format-number(amount, '#', 'coerce')}"/>
</xsl:for-each>
</xsl:variable>
<!-- OUTPUT -->
<statement>
<!-- PROCESS THE FIRST TRANSACTION -->
<xsl:apply-templates select="exsl:node-set($txs)/tx[1]"/>
</statement>
</xsl:template>
<xsl:template match="tx">
<xsl:param name="balance" select="0"/>
<xsl:variable name="prev-balance" select="$balance + @opening-balance" />
<xsl:variable name="run-total" select="$prev-balance + @amount" />
<line>
<date>
<xsl:value-of select="@date"/>
</date>
<previous-balance>
<xsl:value-of select="$prev-balance"/>
</previous-balance>
<amount>
<xsl:value-of select="@amount"/>
</amount>
<run-total>
<xsl:value-of select="$run-total"/>
</run-total>
</line>
<!-- PROCESS THE NEXT TRANSACTION -->
<xsl:apply-templates select="following-sibling::tx[1]">
<xsl:with-param name="balance" select="$run-total"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>