我需要显示XML,并以一天的总距离总和进行排序,以便首先显示大多数公里的一天我只能使距离的总和
输入
<output>
<cars>
<car>
<id>1</id>
<license>B-01-TST</license>
</car>
<car>
<id>2</id>
<license>IF-02-TST</license>
</car>
</cars>
<distances>
<distance>
<id_car>1</id_car>
<date>20110901</date>
<distance>111</distance>
</distance>
<distance>
<id_car>1</id_car>
<date>20110903</date>
<distance>56</distance>
</distance>
<distance>
<id_car>2</id_car>
<date>20110901</date>
<distance>92</distance>
</distance>
<distance>
<id_car>2</id_car>
<date>20110902</date>
<distance>97</distance>
</distance>
</distances>
</output>
输出预期
<output>
<cars>
<car>
<id>1</id>
<license>B-01-TST</license>
<distance totalKm="Day 01: 111"/>
<distance totalKm="Day 03: 56"/>
</car>
<car>
<id>2</id>
<license>IF-02-TST</license>
<distance totalKm="Day 01: 92"/>
<distance totalKm="Day 02: 97"/>
</car>
</cars>
我无法复制所有XSL,因为它的代码太多了,该问题,因此,主要信息是此
到目前为止,XSL看起来像这样:
<xsl:key name="byCarIdAndDate" match="distance" use="concat(id_car, '|', date)"/>
<xsl:key name="byCarId" match="distance" use="id_car"/>
<xsl:template match="car">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:apply-templates select="key('byCarId', id)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="distance[generate-id()= generate-id(key('byCarIdAndDate', concat(id_car, '|', date))[1])]">
<xsl:variable name="thisDate" select="key('byCarIdAndDate', concat(id_car, '|', date))"/>
<xsl:variable name="sum" select="sum($thisDate/distance)"/>
<!--make some sort by higher value -->
<distance totalKm="Day {substring(date, 7, 2)}: {$sum}"/>
</xsl:template>
要删除我这样做的距离块
我认为最好求助于 xsl:for-each-group
,然后按日期进行分组,并让我们轻松地总结一个组:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="byCarId" match="distances/distance" use="id_car"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="distances"/>
<xsl:template match="car">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:for-each-group select="key('byCarId', id)" group-by="date">
<xsl:sort select="sum(current-group()/distance)" order="descending"/>
<xsl:apply-templates select="."/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="distance">
<distance totalKm="Day {substring(date, 7, 2)}: {sum(current-group()/distance)}"/>
</xsl:template>
</xsl:transform>
http://xsltransform.net/3mvmrax