I am learning XML and XSL. I am building on an example I found on line (developer Works: XML zone: What kind of language is XSLT, by Michael Kay) that takes xml data with soccer scores and with two different XSL files produces two
outputs. The second output is a table with the played, won, drawn, lost, for, and against totals. I can successfully add
to this the ranking score (3 times wins, 1 times ties).
What I want to do is output the table sorted by the rankings.
The XML contains nodes like: (ignore the feature="no". I used this for the other type of display).
<match feature="no">
<date>31-May-2002 </date>
<team score="1">Senegal </team>
<team score="0">France </team>
</match>
The xsl for the unordered table follows. What I want to
do (perhaps) is create a new tree, with nodes with the computed values, and then sort on it.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform
xmlns:xsl=" version="1.0">
<xslutput method="html"/>
<xsl:variable name="teams" select="//team[not(.=preceding::team)]"/>
<xsl:variable name="matches" select="//match"/>
<xsl:template match="/results">
<html>
<head><title>Results of World Cup
</title>
<LINK REL="stylesheet" TYPE="text/css" HREF="results.css"/>
</head>
<body>
<h2> Results of World Cup </h2>
<table cellpadding="5">
<tr>
<th> Team </th>
<th> Played </th>
<th> Won </th>
<th> Lost </th>
<th> Tied </th>
<th> For </th>
<th> Against </th>
<th> Points </th>
</tr>
<xsl:for-each select ="$teams">
<xsl:variable name="this" select="."/>
<xsl:variable name="played" select="count($matches[team=$this])"/>
<xsl:variable name="won" select="count($matches[team[.=$this]/@score >
team[.!=$this]/@score])"/>
<xsl:variable name="lost" select="count($matches[team[.=$this]/@score <
team[.!=$this]/@score])"/>
<xsl:variable name="tied" select="count($matches[team[.=$this]/@score = team[.!=$this]/@score])"/>
<xsl:variable name="for" select="sum($matches/team[.=current()]/@score)"/>
<xsl:variable name="against" select="sum($matches[team=current()]/team/@score)-$for"/>
<xsl:variable name="points" select="3*$won+$tied"/>
<tr>
<td><xsl:value-of select="."/></td>
<td><xsl:value-of select="$played"/></td>
<td><xsl:value-of select="$won"/></td>
<td><xsl:value-of select="$lost"/></td>
<td><xsl:value-of select="$tied"/></td>
<td><xsl:value-of select="$for"/></td>
<td><xsl:value-of select="$against"/></td>
<td><xsl:value-of select="$points"/> </td>
</tr>
</xsl:for-each>
</table>
</body> </html>
</xsl:template>
</xsl:transform>
outputs. The second output is a table with the played, won, drawn, lost, for, and against totals. I can successfully add
to this the ranking score (3 times wins, 1 times ties).
What I want to do is output the table sorted by the rankings.
The XML contains nodes like: (ignore the feature="no". I used this for the other type of display).
<match feature="no">
<date>31-May-2002 </date>
<team score="1">Senegal </team>
<team score="0">France </team>
</match>
The xsl for the unordered table follows. What I want to
do (perhaps) is create a new tree, with nodes with the computed values, and then sort on it.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform
xmlns:xsl=" version="1.0">
<xslutput method="html"/>
<xsl:variable name="teams" select="//team[not(.=preceding::team)]"/>
<xsl:variable name="matches" select="//match"/>
<xsl:template match="/results">
<html>
<head><title>Results of World Cup
</title>
<LINK REL="stylesheet" TYPE="text/css" HREF="results.css"/>
</head>
<body>
<h2> Results of World Cup </h2>
<table cellpadding="5">
<tr>
<th> Team </th>
<th> Played </th>
<th> Won </th>
<th> Lost </th>
<th> Tied </th>
<th> For </th>
<th> Against </th>
<th> Points </th>
</tr>
<xsl:for-each select ="$teams">
<xsl:variable name="this" select="."/>
<xsl:variable name="played" select="count($matches[team=$this])"/>
<xsl:variable name="won" select="count($matches[team[.=$this]/@score >
team[.!=$this]/@score])"/>
<xsl:variable name="lost" select="count($matches[team[.=$this]/@score <
team[.!=$this]/@score])"/>
<xsl:variable name="tied" select="count($matches[team[.=$this]/@score = team[.!=$this]/@score])"/>
<xsl:variable name="for" select="sum($matches/team[.=current()]/@score)"/>
<xsl:variable name="against" select="sum($matches[team=current()]/team/@score)-$for"/>
<xsl:variable name="points" select="3*$won+$tied"/>
<tr>
<td><xsl:value-of select="."/></td>
<td><xsl:value-of select="$played"/></td>
<td><xsl:value-of select="$won"/></td>
<td><xsl:value-of select="$lost"/></td>
<td><xsl:value-of select="$tied"/></td>
<td><xsl:value-of select="$for"/></td>
<td><xsl:value-of select="$against"/></td>
<td><xsl:value-of select="$points"/> </td>
</tr>
</xsl:for-each>
</table>
</body> </html>
</xsl:template>
</xsl:transform>