数据源(students.xml):
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xsheet.xsl"?>
<students>
<student id="123">
<name>Andy</name>
<score>69</score>
</student>
<student id="345">
<name>bill</name>
<score>88</score>
</student>
<student id="678">
<name>Felix</name>
<score>96</score>
</student>
<student id="987">
<name>Zerg</name>
<score>72</score>
</student>
<student id="236">
<name>Grrr</name>
<score>59</score>
</student>
</students>
XSL文件(xsheet.xsl):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head>
<title>成绩单</title>
</head>
<body>
<table border="1">
<caption>学生成绩单</caption>
<tr>
<td>序号</td>
<td>ID</td>
<td>姓名</td>
<td>成绩</td>
</tr>
<xsl:for-each select="students/student">
<xsl:sort order="descending" select="score"/>
<tr>
<xsl:choose>
<xsl:when test="position() mod 2 = 1">
<xsl:attribute name="style">background:#336699;</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="style">background:#00ffcc;</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<td><xsl:value-of select="position()"/></td>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="name"/></td>
<xsl:choose>
<xsl:when test="score > 60">
<td><xsl:value-of select="score"/></td>
</xsl:when>
<xsl:otherwise>
<td><font color="red"><xsl:value-of select="score"/></font></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
<br/>
<table border="1">
<caption>统计表格</caption>
<tr><td>总分</td><td><xsl:value-of select="sum(//score)"/></td></tr>
<tr><td>个数</td><td><xsl:value-of select="count(/students/student)"/></td></tr>
<tr><td>及格人数</td><td><xsl:value-of select="count(/students/student[score > 60])"/></td></tr>
<tr><td>平均分</td><td><xsl:value-of select="sum(//score) div count(/students/student)"/></td></tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
用IE打开数据源文件的效果:
XSLT相关知识请参考:
http://soft-app.iteye.com/blog/916652