我正在尝试使用 sum 和 count 变量对每个地方进行“平均目标得分”排序:
我的 XML 是:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="rugby.xsl"?>
<rugby>
<games>
<game>
<place>USA</place>
<team1>Team1</team1>
<team1>Team2</team1>
<score>20-1</score>
</game>
<game>
<place>USA</place>
<team1>Team1</team1>
<team1>Team2</team1>
<score>2-20</score>
</game>
<game>
<place>USA</place>
<team1>Team1</team1>
<team1>Team2</team1>
<score>24-11</score>
</game>
<game>
<place>USA</place>
<team1>Team1</team1>
<team1>Team2</team1>
<score>12-41</score>
</game>
<game>
<place>USA</place>
<team1>Team1</team1>
<team1>Team2</team1>
<score>20-100</score>
</game>
<game>
<place>Mexico</place>
<team1>Team1</team1>
<team1>Team2</team1>
<score>1-32</score>
</game>
<game>
<place>Mexico</place>
<team1>Team1</team1>
<team1>Team2</team1>
<score>2-100</score>
</game>
<game>
<place>Peru</place>
<team1>Team1</team1>
<team1>Team2</team1>
<score>2-10</score>
</game>
<game>
<place>Peru</place>
<team1>Team1</team1>
<team1>Team2</team1>
<score>100-2</score>
</game>
</games>
</rugby>
和 XSLT:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/rugby">
<html>
<head>
<link rel="stylesheet" href="my_style.css" type="text/css"/>
</head>
<body>
<table>
<tr>
<td>Place</td>
<td>Average Goals Score</td>
<td>Average Goals Conceded</td>
</tr>
<xsl:variable name="numOfGames" select="count(//games/game)"/>
<xsl:for-each select="//games/game/place">
<xsl:variable name="nameOfPlace" select="text()"/>
<xsl:variable name="score" select="sum(//games/game/place[contains(., $nameOfPlace)]/following-sibling::score/number(substring-before(.,'-')))"/>
<xsl:variable name="conceded" select="sum(//games/game/place[contains(., $nameOfPlace)]/following-sibling::score/number(substring-after(.,'-')))"/>
<tr>
<td><xsl:value-of select="place"/></td>
<td><xsl:value-of select="$score div $numOfGames"/></td>
<td><xsl:value-of select="$conceded div $numOfGames"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我试图对变量进行排序:
<xsl:sort select="$score div $numOfGames" order="ascending" data-type="number"/>
但它不起作用,因为排序必须在 for each 下(我必须先创建变量)。我试图删除变量 sum 和 count 但我无法删除变量 nameOfPlace,所以排序根本不起作用。
<xsl:for-each select="//games/game/place">
<xsl:variable name="nameOfPlace" select=""/>
<xsl:sort select="sum(//games/game/place[contains(., $nameOfPlace)]/following-sibling::score/number(substring-before(.,'-'))) div count(//games/game)" order="ascending" data-type="number"/>
<tr>
<td><xsl:value-of select="place"/></td>
<td><xsl:value-of select="sum(//games/game/place[contains(., $nameOfPlace)]/following-sibling::score/number(substring-before(.,'-'))) div count(//games/game)"/></td>
<td><xsl:value-of select="sum(//games/game/place[contains(., $nameOfPlace)]/following-sibling::score/number(substring-after(.,'-'))) div count(//games/game)"/></td>
</tr>
</xsl:for-each>
我怎样才能避免这种情况?