Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何使用xslt以最高平均值排序_Xml_Sorting_Xslt - Fatal编程技术网

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

Xml 如何使用xslt以最高平均值排序,xml,sorting,xslt,Xml,Sorting,Xslt,我的xml和xsl需要创建一个表,其中应该显示平均最小值和最大值。这是我能够做到的。但我需要用最高平均值对表进行排序。这是怎么做到的? 示例XML是 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="simpleReport.xsl"?> <Results> <Sample time="1797" prod="Val1" />

我的xml和xsl需要创建一个表,其中应该显示平均最小值和最大值。这是我能够做到的。但我需要用最高平均值对表进行排序。这是怎么做到的? 示例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。平均时间按降序排列


谢谢

不可能按变量排序。但是,您可以根据绑定到相关变量的表达式进行排序。使用以下表达式作为表达式中的第一个元素:

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

请参阅“请包含更多样式表”以及您期望的输出XML HTML。我已更新了“我的样式表”。我浏览了这个链接,但不理解sumkey'kResultByOwner',@Owner/*[name=$pSortBy]谢谢这是有效的,但是你能解释一下这个语句是如何知道按哪个列排序的吗?该语句似乎没有指定任何列。元素的select属性中的路径表达式是相对于上下文节点计算的,上下文节点由元素的select属性建立。这与现有变量绑定的工作方式完全相同。