XML : Sorting by calculated value in XSLT

I've been looking at threads about sorting XML using <xsl:sort> and variables, and still can't get my sorting to work. Here's some XML structure for context:

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

I want to show the list of teams and total points (A game won worth two points, a game lost worth zero points and one point for both teams in equality) I use this code:

  <xsl:variable name="matches" select="//match"/>  <table border="1" width="100%">      <tr>         <th>TEAMS</th>         <th>POINTS</th>      </tr>        <xsl:for-each select="distinct-values(tournament/match/team/@name)">         <xsl:variable name="this" select="."/>           <xsl:variable name="points" select="sum($matches/team[@name=$this and (@score&gt;following-sibling::*/@score         or @score&gt;preceding-sibling::*/@score)]/2) + sum($matches/team[@name=$this         and (@score=following-sibling::*/@score or @score=preceding-sibling::*/@score)]/1) "/>            <tr>              <td><xsl:value-of select="."/></td>              <td><xsl:value-of select="$points"/></td>          </tr>        </xsl:for-each>   </table>       

This help me to show this :

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

Now, I want to sort this result by points to show this:

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

So, how can I sort in the <xsl:for-each> by the variable $points

No comments:

Post a Comment