当柳上原的风吹向天际的时候...

真正的快乐来源于创造

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
数据源(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 &gt; 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
posted on 2011-12-12 21:26 何杨 阅读(405) 评论(0)  编辑  收藏 所属分类: WMB

只有注册用户登录后才能发表评论。


网站导航: