2013-12-18 62 views
1

我需要创建一个表,它应该显示平均最小值和最大值。这是我能够做到的。但我需要以最高的平均值对表格进行排序。这是如何完成的? 示例XML是如何使用xslt排序最高的平均值使用xslt

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="simpleReport.xsl"?> 
<Results> 
    <Sample time="1797" prod="Val1" /> 
    <Sample time="919" prod="Val2" /> 
    <Sample time="2680" prod="Val3" /> 
    <Sample time="545" prod="Val1" /> 
    <Sample time="520" prod="Val2" /> 
    <Sample time="1041" prod="Val3" /> 
    <Sample time="543" prod="Val1" /> 
    <Sample time="491" prod="Val2" /> 
    <Sample time="286" prod="Val3" /> 
    <Sample time="283" prod="Val1" /> 
    <Sample time="782" prod="Val2" /> 
    <Sample time="440" prod="Val2" /> 
</Results> 

这是我在做什么的

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="html" indent="yes" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" /> 
<xsl:param name="prodReport" select="'Results'"/> 
<xsl:template match="Results"> 
    <html> 
     <head> 
      <title><xsl:value-of select="$prodReport" /></title> 
     </head> 
     <body> 
      <xsl:call-template name="ProductList" /> 
     </body> 
    </html> 
</xsl:template> 

<xsl:template name="ProductList"> 
    <h2>Pages</h2> 
    <table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%"> 
     <tr valign="top"> 
      <th>Sample</th> 
      <th>Count</th> 
      <th>Average Time</th> 
     </tr> 
     <xsl:for-each select="/Results/*[not(@prod = preceding::*/@prod)]"> 
      <xsl:variable name="prodName" select="@prod" /> 
      <xsl:variable name="count" select="count(../*[@prod = current()/@prod])" /> 
      <xsl:variable name="totalTime" select="sum(../*[@prod = current()/@prod]/@time)" /> 
      <xsl:variable name="averageTime" select="$totalTime div $count" /> 
      <tr valign="top"> 
       <td> 
        <xsl:value-of select="$prodName" /> 
       </td> 
       <td align="center"> 
        <xsl:value-of select="$count" /> 
       </td> 
       <td align="right"> 
         <xsl:value-of select="$averageTime" /> 
       </td> 
      </tr> 
     </xsl:for-each> 
    </table> 
</xsl:template> 
</xsl:stylesheet> 

如何排序基于计算的平均值计算平均? HTML表格列将是ProdName Count AverageTime。平均时间将按降序排列。

谢谢

+0

见http://stackoverflow.com/questions/758527/xsl-how-can-i-group-and-sort-based-on- sum –

+0

请包含更多样式表,以及您期望的输出XML(HTML)。 –

+0

我已更新我的样式表。我经历了链接,但不明白“sum(key('kResultByOwner',@Owner)/ @ * [name()= $ pSortBy]) – kumar

回答

0

无法通过变量进行排序。但是,您可以通过绑定到所讨论的变量的表达式进行排序。使用下列表达式作为第一个元素在你的<xsl:for-each/>表达:

<xsl:sort data-type="number" order="descending" 
      select="sum(../*[@prod = current()/@prod]/@time) div 
        count(../*[@prod = current()/@prod])"/> 
+0

感谢这个工作,但你能解释一下,这个声明是怎么知道的这个语句似乎没有指定任何列 – kumar

+0

''元素的'select'属性中的路径表达式是相对于上下文节点计算的,该节点由'select'属性''元素,这和你现有的变量绑定完全一样。 – joemfb