在XSL中按2个字段进行分组 [英] Grouping by 2 fields in XSL

查看:244
本文介绍了在XSL中按2个字段进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个xml:

 < page> 
<文件>
< id> 1001< / id>
< cur> USD< / cur>
< date> 01.01.2009< / date>
<金额> 10< /金额>
< / document>
<文件>
< id> 1001< / id>
< cur> USD< / cur>
< date> 02.01.2009< / date>
<金额> 15< /金额>
< / document>
<文件>
< id> 1001< / id>
< cur> JPY< / cur>
< date> 01.01.2009< / date>
<金额> 5< /金额>
< / document>
<文件>
< id> 1002< / id>
< cur> USD< / cur>
< date> 01.01.2009< / date>
<金额> 5< /金额>
< / document>
...
< / page>

我需要将它转换为html。记录应该由 id cur 分组。并且在每组总数量应该被显示之后。所以我们想要这样的东西:

  Bill:id = 1001,cur = USD 
date = 01.01.2009 amount = 10
date = 02.01.2009 amount = 15
total amount = 25
Bill:id = 1001,cur = JPY
date = 01.01.2009 amount = 5
总金额= 5
帐单:id = 1002,cur = USD
日期= 01.01.2009金额= 5
总金额= 5
...

如何使用XSL实现此目的?

当我试图在google中找到答案时,我发现了Muenchian方法,但是当我们想要按2个字段对结果进行分组时,它太复杂了。我是xsl的初学者,对我来说有点困难。
我还为每个组找到了xslt 2.0运算符。它受主流浏览器的支持吗?是否正常使用它,或者我们只应该依赖xslt 1.0?

解决方案

您可以使用XSLT 1.0来执行此操作



我在这里使用的方法是用两个字段id和cur创建一个复合键。之后我将这些模板应用到每个组中的第一个文档。在模板中,我然后遍历单个文档,最后我总结文档数量字段。

 <?xml version =' 1.0' >?; 
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>

< xsl:key name =idcurmatch =documentuse =concat(id,cur)/>

< xsl:template match =/ page>
< / xsl:template>

< xsl:template match =document>
Bill:id =< xsl:value-of select =id/>,cur =< xsl:value-of select =cur/>
< xsl:for-each select =$ document>
date =< xsl:value-of select =date/> amount =< xsl:select-value =amount/>
< / xsl:for-each>
total amount =< xsl:value-of select =sum($ document / amount)/>
< / xsl:template>
< / xsl:stylesheet>

输出:

  Bill:id = 1001,cur = USD 
date = 01.01.2009 amount = 10
date = 02.01.2009 amount = 15
total amount = 25
Bill:id = 1001,cur = JPY
date = 01.01.2009 amount = 5
total amount = 5
Bill:id = 1002,cur = USD
date = 01.01。 2009金额= 5
总金额= 5


I have next xml:

<page>
   <document>
      <id>1001</id>
      <cur>USD</cur>
      <date>01.01.2009</date>
      <amount>10</amount>
   </document>
   <document>
      <id>1001</id>
      <cur>USD</cur>
      <date>02.01.2009</date>
      <amount>15</amount>
   </document>
   <document>
      <id>1001</id>
      <cur>JPY</cur>
      <date>01.01.2009</date>
      <amount>5</amount>
   </document>
   <document>
      <id>1002</id>
      <cur>USD</cur>
      <date>01.01.2009</date>
      <amount>5</amount>
   </document>
   ...
</page>

And I need to transform it into html. Records should be grouped by id and cur. And after each group total amount should be shown. So we want something like this:

Bill: id=1001, cur=USD
      date=01.01.2009   amount=10
      date=02.01.2009   amount=15
      total amount=25
Bill: id=1001, cur=JPY
      date=01.01.2009   amount=5
      total amount=5
Bill: id=1002, cur=USD
      date=01.01.2009   amount=5
      total amount=5
...

How can I achieve this using XSL?

When I tried to find answer in google I found Muenchian method, but it's too complicated when we want to group result by 2 fields. I'm beginner in xsl and it's a bit difficult to me. I also found xslt 2.0 operator for-each-group. Is it supported by major browsers? Is it normally to use it or we should only rely on xslt 1.0?

解决方案

You can do this with XSLT 1.0

The method i use here is to create a composite key with the two fields, id and cur. I later apply the templates to the first document in each group. Within the template i then loop through the individual documents and finally i summarize the documents amount field.

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="idcur" match="document" use="concat(id,cur)"/>

<xsl:template match="/page">
    <xsl:apply-templates select="document[generate-id() = generate-id(key('idcur',concat(id,cur))[1])]"/>
</xsl:template>

<xsl:template match="document">
<xsl:variable name="document" select="key('idcur',concat(id,cur))"/>
Bill: id=<xsl:value-of select="id"/>, cur=<xsl:value-of select="cur"/>
    <xsl:for-each select="$document">
      date=<xsl:value-of select="date"/>   amount=<xsl:value-of select="amount"/>
    </xsl:for-each>
      total amount=<xsl:value-of select="sum($document/amount)"/>
</xsl:template>
</xsl:stylesheet>

Output:

Bill: id=1001, cur=USD
      date=01.01.2009   amount=10
      date=02.01.2009   amount=15
      total amount=25
Bill: id=1001, cur=JPY
      date=01.01.2009   amount=5
      total amount=5
Bill: id=1002, cur=USD
      date=01.01.2009   amount=5
      total amount=5

这篇关于在XSL中按2个字段进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆