我有如下 xml(使用 xslt 1.0):
<?xml version="1.0" encoding="utf-8"?>
<receives>
<receive>
<Year>2013</Year>
<money>120</money>
</receive>
<receive>
<Year>2013</Year>
<money>150</money>
</receive>
<receive>
<Year>2014</Year>
<money>130</money>
</receive>
<receive>
<Year>2011</Year>
<money>120</money>
</receive>
</receives>
我想按年份对钱进行分组,如果年份不在列表中(如上 xml,没有 2011),我需要将 2012 放入结果中,totalamount=0 如下:
<year>2011</year>
<totalamount>120</totalamount>
<year>2012</year>
<totalamount>0</totalamount>
<year>2013</year>
<totalamount>270</totalamount>
<year>2014</year>
<totalamount>130</totalamount>
目前,我已经完成了 xslt,如下所示:
<xsl:key name="receive-key" match="receive" use="Year" />
<xsl:template match="/receives">
<xsl:for-each
select="receive[generate-id() = generate-id(key('receive-key', Year))]">
<xsl:sort select="../receive[Year = current()/Year]/Year"></xsl:sort>
<year>
<xsl:value-of select="../receive[Year = current()/Year]/Year" />
</year>
<totalamount>
<xsl:value-of select="sum(../receive[Year = current()/Year]/money)" />
</totalamount>
</xsl:for-each>
</xsl:template>
这只能按现有年份对钱进行分组:
<year>2011</year>
<totalamount>120</totalamount>
<year>2013</year>
<totalamount>270</totalamount>
<year>2014</year>
<totalamount>130</totalamount>
关于如何插入的任何想法
<year>2012</year>
<totalamount>0</totalamount>
结果?
非常感谢!