我需要显示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>
模板匹配=“距离|距离”