如何在 XSL 中使用 SUM 和 AVERAGE? [英] How do I use SUM and AVERAGE in XSL?

查看:30
本文介绍了如何在 XSL 中使用 SUM 和 AVERAGE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的是每辆车的总公里数和平均每天的公里数

I want to the total kilometers per car and the average kilometers per day

这是输入的 XML :

 <?xml version="1.0" encoding="ISO-8859-1" ?>   

 <output>   
<cars>  
   <car>    
      <id>1</id>    
      <brand>BMW</brand>    
      <type>M3</type>   
      <license>AD-9999-ATSR</license>   
   </car>   
<car>   
     <id>2</id> 
     <brand>Volkwagen</brand>   
     <type>GTI</type>   
     <license>ASD-7458-WERT</license>   
     </car> 
 </cars>    
 <distances>    
  <distance>    
    <id_car>1</id_car>  
    <date>20120118</date>   
    <distance>90</distance> 
</distance> 
 <distance> 
  <id_car>1</id_car>    
  <date>20120117</date> 
  <distance>23</distance>   
  </distance>
 <distance> 
  <id_car>1</id_car>    
  <date>20120117</date> 
  <distance>17</distance>   
  </distance>
<distance>  
  <id_car>1</id_car>    
 <date>20120116</date>  
 <distance>5</distance> 
 </distance>    
 <distance> 
 <id_car>2</id_car> 
<date>20120101</date>   
 <distance>92</distance>    
</distance> 
 <distance> 
 <id_car>2</id_car> 
 <date>20120102</date>  
 <distance>87</distance>    
 </distance>
 <distance> 
 <id_car>2</id_car> 
 <date>20120102</date>  
 <distance>13</distance>    
 </distance>    
<distance>  
 <id_car>2</id_car> 
 <date>20120103</date>  
 <distance>112</distance>   
  </distance>   
 </distances>   
</output>   

这是输出的xml:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<output>
<cars>
<car>
  <id>1</id>
  <brand>BMW</brand>
  <type>M3</type>
  <license>AD-9999-ATSR</license>
    <distance Total_kM="135"></distance>
    <distance average_KM/day="18/90"></distance>
    <distance average_KM/day="17/20"></distance>
    <distance average_KM/day="16/5"></distance>
</car>
<car>
  <id>2</id>
  <brand>Volkwagen</brand>
  <type>GTI</type>
  <license>ASD-7458-WERT</license>
    <distance Total_kM="304"></distance>
    <distance averageKM/day="01/90"></distance>
    <distance average_KM/day="02/50"></distance>
    <distance average_KM/day="03/112"></distance>
</car>
</cars>
</output>

类似这样或你能想到的其他安排,在输出中,显示每辆车的总公里数和平均每天的公里数

Something like this or onother arangement you can think of ,in the output, to show the total kilometers per car and the average kilometers per day

这是我要更改的 xsl :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="distances" match="distance" use="id_car" />

    <xsl:template match="output">
        <xsl:apply-templates select="cars" />
    </xsl:template>

    <xsl:template match="car">
        <xsl:copy>
            <xsl:apply-templates />
            <distances>
                <xsl:apply-templates select="key('distances', id)" />
            </distances>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="distance">
        <distance day="{date}">
            <xsl:value-of select="distance" />
        </distance>
    </xsl:template>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

如何在 XSL 中使用 SUM 和 AVERAGE 来输出我想要的内容?

How do I use SUM and AVERAGE in XSL to ouput what i want ?

感谢您的时间和精力

推荐答案

以下样式表产生了想要的结果:

The following stylesheet produces the wanted result:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:key name="byCarId" match="distance" use="id_car"/>
    <xsl:key name="byCarIdAndDate" match="distance" 
             use="concat(id_car, '|', date)"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="car">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <distance Total_kM="{sum(key('byCarId', id)/distance)}"/>
            <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)"/>
        <xsl:variable name="count" select="count($thisDate)"/>
        <distance average_KM_day="{substring(date, 7, 2)}/{$sum div $count}"/>
    </xsl:template>
    <xsl:template match="distances|distance"/>
</xsl:stylesheet>

说明:

  • Identity Transform 输出每个 car 元素的大部分内容,因为它出现在源代码中
  • 使用两个单独的键:1) 按每辆汽车的 ID 分组;2) 按汽车 ID 和日期的组合分组
  • 只需要一个额外的模板来获取每个可能的汽车 ID 和日期对的第一个 距离,我们在其中输出该组合的平均值
  • The Identity Transform outputs most of each car element as it appears in the source
  • Two separate keys are used: 1) to group by each car's ID and 2) to group by the combination of car ID and date
  • Only one additional template is needed to grab the first distance for each possible car ID and date pair, in which we output the average for that combination

这篇关于如何在 XSL 中使用 SUM 和 AVERAGE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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