xml
摘录(完整的示例 xml 在 http://pastebin.com/HDrmzctC ):
<log>
<event>
<when>2012-10-26T21:32:52</when>
<what>
<item>food</item>
<money>10</money>
</what>
</event>
<event>
<when>2012-10-26T21:32:52</when>
<what>
<item>beer</item>
<money>20</money>
</what>
</event>
<event>
<when>2012-10-27T21:32:52</when>
<what>
<item>food</item>
<money>10</money>
</what>
</event>
<event>
<when>2012-10-27T21:32:52</when>
<what>
<item>beer</item>
<money>20</money>
</what>
</event>
<event>
<when>2012-11-26T21:32:52</when>
<what>
<item>food</item>
<money>11</money>
</what>
</event>
<event>
<when>2012-11-26T21:32:52</when>
<what>
<item>beer</item>
<money>22</money>
</what>
</event>
<event>
<when>2012-11-27T21:32:52</when>
<what>
<item>food</item>
<money>11</money>
</what>
</event>
<event>
<when>2012-11-27T21:32:52</when>
<what>
<item>beer</item>
<money>22</money>
</what>
</event>
</log>
期望输出:
2012-10 食品 202012-10 啤酒 402012-11 食品 222012-11 啤酒 44
还是XSL不简单,因为XML设计错误?
此转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kEventByDateItem" match="event"
use="concat(substring(when, 1,7), '+', what/item)"/>
<xsl:template match=
"event
[generate-id()
=
generate-id(key('kEventByDateItem',
concat(substring(when, 1,7), '+', what/item)
)[1]
)
]">
<xsl:value-of select="concat('
',substring(when, 1,7), ' ', what/item, ' ')"/>
<xsl:value-of select=
"sum(key('kEventByDateItem',
concat(substring(when, 1,7), '+', what/item)
)/what/money)"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<log>
<event>
<when>2012-10-26T21:32:52</when>
<what>
<item>food</item>
<money>10</money>
</what>
</event>
<event>
<when>2012-10-26T21:32:52</when>
<what>
<item>beer</item>
<money>20</money>
</what>
</event>
<event>
<when>2012-10-27T21:32:52</when>
<what>
<item>food</item>
<money>10</money>
</what>
</event>
<event>
<when>2012-10-27T21:32:52</when>
<what>
<item>beer</item>
<money>20</money>
</what>
</event>
<event>
<when>2012-11-26T21:32:52</when>
<what>
<item>food</item>
<money>11</money>
</what>
</event>
<event>
<when>2012-11-26T21:32:52</when>
<what>
<item>beer</item>
<money>22</money>
</what>
</event>
<event>
<when>2012-11-27T21:32:52</when>
<what>
<item>food</item>
<money>11</money>
</what>
</event>
<event>
<when>2012-11-27T21:32:52</when>
<what>
<item>beer</item>
<money>22</money>
</what>
</event>
</log>
产生所需的正确结果:
2012-10 food 20
2012-10 beer 40
2012-11 food 22
2012-11 beer 44
解释:
正确使用带有复合键的 Muenchian 分组方法。