几天来,我一直在努力确定如何获取比赛结果(球队和最终比分)的 XML 文件并生成球队排名列表,显示每支球队以及他们根据比赛状态(已玩)赢、输或并列的次数. 我能够在游戏状态(已玩和待定)上显示团队排名。我想显示只玩过的球队排名。任何帮助将不胜感激。
这是我的xml代码:
<Schedule>
<game status="played">
<Home_Team>A</Home_Team>
<Away_Team>B</Away_Team>
<Date>2013-06-15</Date>
<Home_Team_Score>3</Home_Team_Score>
<Away_Team_Score>3</Away_Team_Score>
</game>
<game status="played">
<Home_Team>A</Home_Team>
<Away_Team>C</Away_Team>
<Date>2013-06-17</Date>
<Home_Team_Score>7</Home_Team_Score>
<Away_Team_Score>4</Away_Team_Score>
</game>
<game status="played">
<Home_Team>C</Home_Team>
<Away_Team>A</Away_Team>
<Date>2013-06-19</Date>
<Home_Team_Score>3</Home_Team_Score>
<Away_Team_Score>3</Away_Team_Score>
</game>
<game status="played">
<Home_Team>D</Home_Team>
<Away_Team>C</Away_Team>
<Date>2013-06-19</Date>
<Home_Team_Score>8</Home_Team_Score>
<Away_Team_Score>7</Away_Team_Score>
</game>
<game status="pending">
<Home_Team>B</Home_Team>
<Away_Team>C</Away_Team>
<Date>2013-07-25</Date>
<Home_Team_Score>0</Home_Team_Score>
<Away_Team_Score>0</Away_Team_Score>
</game>
<game status="pending">
<Home_Team>C</Home_Team>
<Away_Team>D</Away_Team>
<Date>2013-07-27</Date>
<Home_Team_Score>0</Home_Team_Score>
<Away_Team_Score>0</Away_Team_Score>
</game>
</Schedule>
这是我的 xsl 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="TeamByName" match="Home_Team|Away_Team" use="."/>
<xsl:template match="/*">
<html>
<body>
<table border="1">
<tr>
<td>Team</td>
<td>Wins</td>
<td>Losses</td>
<td>Tie</td>
</tr>
<xsl:for-each select="//game[@status='played']">
<xsl:apply-templates select="(*/Home_Team | */Away_Team)[generate- id()=generate- id(key('TeamByName', .)[1])]">
<xsl:sort select="."/>
< /xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Home_Team|Away_Team">
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:value-of select=
"count(key('TeamByName', .)
[self::Home_Team
and
../Home_Team_Score > ../Away_Team_Score
or
self::Away_Team
and
../Away_Team_Score > ../Home_Team_Score
]
)"/>
</td>
<td>
<xsl:value-of select= "count(key('TeamByName', .)
[self::Home_Team
and
../Away_Team_Score > ../Home_Team_Score
or
self::Away_Team
and
../Home_Team_Score > ../Away_Team_Score
]
)"/>
</td>
<td>
<xsl:value-of select="count(key('TeamByName', .)
[../Home_Team_Score = ../Away_Team_Score]
)"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
输出应采用以下形式:
<table border="1">
<tr>
<td>Team</td>
<td>Wins</td>
<td>Losses</td>
<td>Ties</td>
</tr>
<tr>
<td>A</td>
<td>1</td>
<td>0</td>
<td>2</td>
</tr>
<tr>
<td>B</td>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>C</td>
<td>0</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>D</td>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
</table>