如何使用xslt以最高平均值排序



我的xml whit xsl需要创建一个表,在那里它应该显示平均值min和max。这是我能够做到的。但我需要对平均值最高的表进行排序。这是怎么做到的?示例XML是

<?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。其中AverageTime将按降序排列。

感谢

不可能按变量排序。不过,您可以根据绑定到相关变量的表达式进行排序。使用以下表达式作为<xsl:for-each/>表达式中的第一个元素:

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

最新更新