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进行排序,但仍然无法实现排序。以下是上下文的一些XML结构: <?xml version="1.0" encoding="UTF-8"?> <tournament> <match date="2015-12-18"> <team name="FIRST" score="1" /> <team name="SECOND" score="0" /> </

我一直在研究如何使用
和变量对XML进行排序,但仍然无法实现排序。以下是上下文的一些XML结构:

<?xml version="1.0" encoding="UTF-8"?>
<tournament>
    <match date="2015-12-18">
        <team name="FIRST" score="1" />
        <team name="SECOND" score="0" />
    </match>
    <match date="2015-12-20">
        <team name="FIRST" score="1" />
        <team name="THIRD" score="3" />
    </match>

    <match date="2015-12-23">
        <team name="THIRD" score="5" />
        <team name="SECOND" score="1" />
    </match>
</tournament>
现在,我想按点对结果进行排序,以显示:

   |   TEAMS     |    POINTS  |
   |--------------------------|
   |   THIRD     |      3     |
   |   FIRST     |      2     |
   |   SECOND    |      1     |

那么,如何在
中按变量
$points

排序?您不能按变量排序。为了避免计算两次积分,我将分两次计算:

XSLT2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="wins" match="team[@score > ancestor::match/team/@score]" use="@name"/>
<xsl:key name="draws" match="team[not(@score!=ancestor::match/team/@score)]" use="@name"/>

<xsl:template match="/tournament">
    <!-- first pass -->
    <xsl:variable name="teams">
        <xsl:for-each-group select="match/team" group-by="@name">
            <team name="{@name}" points="{2 * count(key('wins', @name)) + count(key('draws', @name))}"/>
        </xsl:for-each-group>
    </xsl:variable>
    <!-- output -->
    <table border="1" width="100%">
        <tr>
           <th>TEAMS</th>
           <th>POINTS</th>
        </tr>
        <xsl:for-each select="$teams/team">
            <xsl:sort select="@points" data-type="number" order="descending"/>
            <tr>
                <td><xsl:value-of select="@name"/></td>
                <td><xsl:value-of select="@points"/></td>
            </tr>
        </xsl:for-each> 
    </table>    
</xsl:template>

</xsl:stylesheet>

团队
要点
   |   TEAMS     |    POINTS  |
   |--------------------------|
   |   THIRD     |      3     |
   |   FIRST     |      2     |
   |   SECOND    |      1     |
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="wins" match="team[@score > ancestor::match/team/@score]" use="@name"/>
<xsl:key name="draws" match="team[not(@score!=ancestor::match/team/@score)]" use="@name"/>

<xsl:template match="/tournament">
    <!-- first pass -->
    <xsl:variable name="teams">
        <xsl:for-each-group select="match/team" group-by="@name">
            <team name="{@name}" points="{2 * count(key('wins', @name)) + count(key('draws', @name))}"/>
        </xsl:for-each-group>
    </xsl:variable>
    <!-- output -->
    <table border="1" width="100%">
        <tr>
           <th>TEAMS</th>
           <th>POINTS</th>
        </tr>
        <xsl:for-each select="$teams/team">
            <xsl:sort select="@points" data-type="number" order="descending"/>
            <tr>
                <td><xsl:value-of select="@name"/></td>
                <td><xsl:value-of select="@points"/></td>
            </tr>
        </xsl:for-each> 
    </table>    
</xsl:template>

</xsl:stylesheet>