1

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

还是因为xml设计错误而导致xsl不简单?

4

1 回答 1

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 分组方法。

于 2012-12-14T18:14:36.307 回答