XSL学习(3)
清单 1. 表示足球锦标赛结果的 XML 文档
清单 2. 足球赛结果的基本样式表
清单3. 计算球队名次表的样式表
<results group="A">
<match>
<date>10-Jun-1998</date>
<team score="2">Brazil</team>
<team score="1">Scotland</team>
</match>
<match>
<date>10-Jun-1998</date>
<team score="2">Morocco</team>
<team score="2">Norway</team>
</match>
<match>
<date>16-Jun-1998</date>
<team score="1">Scotland</team>
<team score="1">Norway</team>
</match>
<match>
<date>16-Jun-1998</date>
<team score="3">Brazil</team>
<team score="0">Morocco</team>
</match>
<match>
<date>23-Jun-1998</date>
<team score="1">Brazil</team>
<team score="2">Norway</team>
</match>
<match>
<date>23-Jun-1998</date>
<team score="0">Scotland</team>
<team score="3">Morocco</team>
</match>
</results>
<match>
<date>10-Jun-1998</date>
<team score="2">Brazil</team>
<team score="1">Scotland</team>
</match>
<match>
<date>10-Jun-1998</date>
<team score="2">Morocco</team>
<team score="2">Norway</team>
</match>
<match>
<date>16-Jun-1998</date>
<team score="1">Scotland</team>
<team score="1">Norway</team>
</match>
<match>
<date>16-Jun-1998</date>
<team score="3">Brazil</team>
<team score="0">Morocco</team>
</match>
<match>
<date>23-Jun-1998</date>
<team score="1">Brazil</team>
<team score="2">Norway</team>
</match>
<match>
<date>23-Jun-1998</date>
<team score="0">Scotland</team>
<team score="3">Morocco</team>
</match>
</results>
清单 2. 足球赛结果的基本样式表
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="results">
<html>
<head><title>
Results of Group <xsl:value-of select="@group">
</title></head>
<body><h1>
Results of Group <xsl:value-of select="@group">
</h1>
<xsl:apply-templates>
</body></html>
</xsl:template>
<xsl:template match="match">
<h2>
<xsl:value-of select="team[1]"> versus <xsl:value-of select="team[2]">
</h2>
<p>Played on <xsl:value-of select="date"></p>
<p>Result:
<xsl:value-of select="team[1] ">
<xsl:value-of select="team[1]/@score">,
<xsl:value-of select="team[2] ">
<xsl:value-of select="team[2]/@score">
</p>
</xsl:template>
</xsl:transform>
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="results">
<html>
<head><title>
Results of Group <xsl:value-of select="@group">
</title></head>
<body><h1>
Results of Group <xsl:value-of select="@group">
</h1>
<xsl:apply-templates>
</body></html>
</xsl:template>
<xsl:template match="match">
<h2>
<xsl:value-of select="team[1]"> versus <xsl:value-of select="team[2]">
</h2>
<p>Played on <xsl:value-of select="date"></p>
<p>Result:
<xsl:value-of select="team[1] ">
<xsl:value-of select="team[1]/@score">,
<xsl:value-of select="team[2] ">
<xsl:value-of select="team[2]/@score">
</p>
</xsl:template>
</xsl:transform>
清单3. 计算球队名次表的样式表
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:variable name="teams" select="//team[not(.=preceding::team)]">
<xsl:variable name="matches" select="//match">
<xsl:template match="results">
<html><body>
<h1>Results of Group <xsl:value-of select="@group"></h1>
<table cellpadding="5">
<tr>
<td>Team</td>
<td>Played</td>
<td>Won</td>
<td>Drawn</td>
<td>Lost</td>
<td>For</td>
<td>Against</td>
</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="drawn"
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">
<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="$drawn"></td>
<td><xsl:value-of select="$lost"></td>
<td><xsl:value-of select="$for"></td>
<td><xsl:value-of select="$against"></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:transform>
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:variable name="teams" select="//team[not(.=preceding::team)]">
<xsl:variable name="matches" select="//match">
<xsl:template match="results">
<html><body>
<h1>Results of Group <xsl:value-of select="@group"></h1>
<table cellpadding="5">
<tr>
<td>Team</td>
<td>Played</td>
<td>Won</td>
<td>Drawn</td>
<td>Lost</td>
<td>For</td>
<td>Against</td>
</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="drawn"
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">
<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="$drawn"></td>
<td><xsl:value-of select="$lost"></td>
<td><xsl:value-of select="$for"></td>
<td><xsl:value-of select="$against"></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:transform>
posted on 2007-08-17 17:20 mjgforever 阅读(385) 评论(0) 编辑 收藏 举报