一个标准的XSL

来源:互联网 发布:nip网络摄像头客户端 编辑:程序博客网 时间:2024/06/10 01:15

XSL作为xml的模板现在被越来越多的人使用,我开始学的时候用的是firefox和IE共同调试,期间看了蓝色理想的教程和几本书,发现调试根本就通不过,最后还是自己整理出一份入门的标准格式,如果你也是个初学者被不标准的写法误导,被错误困扰,那不妨看一下我的例子....

xml文件如下:

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="resume1.xsl"?>
<doc>
  <resume type="Current conditions">
    <name>zhanghao</name>
    <sex>男</sex>
    <birthday>20</birthday>
    <skill>数据库设计与维护、WEB开发</skill>
    <skill>吃喝玩乐</skill>
  </resume>
  <resume type="Current conditions">
    <name>zhanghao</name>
    <sex>nv</sex>
    <birthday>1982</birthday>
    <skill>数据库设计与维护、WEB开发</skill>
  </resume>
</doc>

xsl文件如下:

<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!--根模板-->
<xsl:template match="/">
  <HTML>
    <HEAD>
      <TITLE>个人简历</TITLE>
    </HEAD>
    <BODY>
    <xsl:for-each select="doc">
      <P/>
      <xsl:apply-templates select="resume"/>
    </xsl:for-each>
   </BODY>
  </HTML>
</xsl:template>


<!--简历模板-->
<xsl:template match="resume">
  <TABLE border="1" cellspacing="0" align="center">
    <CAPTION style="font-size: 150%; font-weight: bold">
    个人简历
    </CAPTION>
    <tr>
      <xsl:apply-templates select="name"/>
      <xsl:apply-templates select="sex"/>
      <xsl:apply-templates select="birthday"/>
    </tr>
    <TD>技能</TD>
    <td COLSPAN="5">
      <table cellspacing="0">
         <xsl:apply-templates select="skill"/>
      </table>
    </td>
  </TABLE>
</xsl:template>

  
<!--姓名模板-->
<xsl:template match="name">
  <TH>姓名</TH>
  <TD><xsl:apply-templates/></TD>
</xsl:template>

<!--性别模板-->
<xsl:template match="sex">
  <th>性别</th>
  <TD><xsl:apply-templates/></TD>
</xsl:template>

<!--生日模板-->
<xsl:template match="birthday">
  <th>生日</th><td><xsl:apply-templates/></td>
</xsl:template>

<!--技能模板-->
<xsl:template match="skill">
  <tr>
    <td>
    <xsl:apply-templates/></td>
    </tr>
</xsl:template>

</xsl:stylesheet>

原创粉丝点击