XSL简单实例

利用xsl可以做到将数据和格式完全分离,下面比较一个xml+css和xml+xsl的区别。

 

XML + css

resume.xml文件内容如下:

1 <?xml version="1.0" encoding="GB2312"?>
2 <?xml-stylesheet type="text/css" href="resume.css"?> 
3 <resume>  
4 <name>禹希初</name>  
5 <sex></sex>  
6 <birthday>1977.5</birthday>  
7 <skill>数据库设计与维护、WEB开发</skill>  
8 </resume>

resume.css文件内容如下:

1 resume{ display: block;}
2 name{ display: block; font-size:120%;}
3 sex{ display:block; text-indent:2em}
4 birthday{ display:block; text-indent:2em}
5 skill{ display:block; text-indent:2em}

效果:

 

 

 

XML + xsl

resume.xml文件内容如下:

1 <?xml version="1.0" encoding="GB2312"?>
2 <?xml-stylesheet type="text/xsl" href="resume.xsl"?> 
3 <resume>  
4 <name>禹希初</name>  
5 <sex></sex>  
6 <birthday>1977.5</birthday>  
7 <skill>数据库设计与维护、WEB开发</skill>  
8 </resume>

resume.xsl文件内容如下:

 1 <?xml version="1.0" encoding="GB2312"?>
 2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 3 <xsl:template match="/">
 4 <html>
 5 <head>  
 6 <title>个人简历</title>  
 7 </head><body>  
 8 <xsl:for-each select="resume">  
 9 <p/>  
10 <table border="1" cellspacing="0">  
11 <caption style="font-size: 150%; font-weight: bold">  
12 个人简历  
13 </caption>  
14 <tr>  
15 <th>姓名</th><td><xsl:value-of select="name"/></td>  
16 <th>性别</th><td><xsl:value-of select="sex"/></td>  
17 <th>生日</th><td><xsl:value-of select="birthday"/></td>  
18 </tr>  
19 <tr>  
20 <th>技能</th><td colspan="5"><xsl:value-of select="skill"/></td>  
21 </tr>  
22 </table>  
23 </xsl:for-each>  
24 </body> 
25 </html>
26 </xsl:template>
27 </xsl:stylesheet>

效果:

 

参考:

http://justcoding.iteye.com/blog/778361

http://www.w3school.com.cn/xsl/xsl_for_each.asp

 

posted @ 2016-08-07 11:55  月色深潭  阅读(1980)  评论(0编辑  收藏  举报