2012-01-19 60 views
2

我想每車總公里,每天平均公里如何在XSL中使用SUM和AVERAGE?

這是輸入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> 

類似這樣的事情或者你可以在輸出中想到的其他安排,以顯示每輛車的總公里數和每天的平均公里數

這是我試圖改變了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> 

如何使用SUM和AVERAGE在XSL來輸出中我想要什麼?

感謝您的時間和精力

回答

2

以下樣式表生成想要的結果:

<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> 

說明

  • 恆等變換輸出最每個car元素的它出現在源
  • 兩個se使用parate鍵:1)按每個車輛的ID進行分組,2)按車輛ID和日期的組合分組
  • 只需要一個額外的模板來抓取每個可能的車輛ID和日期對的第一個distance,我們輸出該組合的平均值
+2

好的答案,+1。 –