-report>
我们想 计算 </跨度> 表示数据中每天可用的平均销售额的数字。假设所有的田间标签与其他标签具有相同的天数(I 信任 </跨度> ] ——————————- ñ
其中’n’是该行中有效天数。
那些数字(td [1] / cnt [1])是我们已经正确的数字 计算 </跨度> 对于该表中的前几个单元格。不幸的是,那个总数,如
使用XSLT 2.0,您可以轻松地将计算结果存储在序列或临时树中:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> <xsl:output method="html" indent="yes"/> <xsl:template match="row"> <tr> <td>Average Income</td> <!-- use one list of days, we only need that from the first field we expect the lists to always be sorted so not sort in XSLT --> <xsl:variable name="sales-by-day"> <xsl:for-each-group select="field[@group = 'sales']/day" group-by="@date"> <sales day="{current-grouping-key()}"> <amount><xsl:value-of select="sum(current-group()/amount)"/></amount> <count><xsl:value-of select="sum(current-group()/count)"/></count> </sales> </xsl:for-each-group> </xsl:variable> <xsl:for-each select="$sales-by-day/sales"> <td> <xsl:text>$</xsl:text><xsl:value-of select="amount div count"/> </td> </xsl:for-each> <!-- this cell is the one I have a problem with... --> <td class="average"> <xsl:text>$</xsl:text><xsl:value-of select="sum($sales-by-day/sales/(amount div count)) div count(field[@group = 'sales'][1]/day)"/> </td> </tr> </xsl:template> <xsl:template match="snap-report"> <table class="report"> <xsl:apply-templates select="row"/> </table> </xsl:template> </xsl:stylesheet>
我认为以上内容可以缩短为
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> <xsl:output method="html" indent="yes"/> <xsl:template match="row"> <tr> <td>Average Income</td> <!-- use one list of days, we only need that from the first field we expect the lists to always be sorted so not sort in XSLT --> <xsl:variable name="sales-by-day"> <xsl:for-each-group select="field[@group = 'sales']/day" group-by="@date"> <sales day="{current-grouping-key()}"> <xsl:value-of select="sum(current-group()/amount) div sum(current-group()/count)"/> </sales> </xsl:for-each-group> </xsl:variable> <xsl:for-each select="$sales-by-day/sales"> <td> <xsl:text>$</xsl:text><xsl:value-of select="."/> </td> </xsl:for-each> <!-- this cell is the one I have a problem with... --> <td class="average"> <xsl:text>$</xsl:text><xsl:value-of select="sum($sales-by-day/sales) div count($sales-by-day/sales)"/> </td> </tr> </xsl:template> <xsl:template match="snap-report"> <table class="report"> <xsl:apply-templates select="row"/> </table> </xsl:template> </xsl:stylesheet>