什么xsl来汇总日期范围内的元素?

时间:2012-12-14 17:49:28

标签: xml xslt xslt-1.0 xslt-2.0

xml摘录(http://pastebin.com/HDrmzctC处的完整示例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食品20
    2012-10啤酒40
    2012-11食品22
    2012-11啤酒44

或xsl是不是很简单,因为xml设计错了?

1 个答案:

答案 0 :(得分:1)

此转化

<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('&#xA;',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 Grouping Method