模板大概相当于程序中模块吧,就是把大部分分开来写容易维护,修改。
语法格式为:
<xsl:template match="node-context" language="language-name">
属性:
match ── 确定什幺样的情况下执行此模板。作为一种简化的说明,在此处使用标记的名字;其中最上层模板必须将match设为“/”
language ── 确定在此模板中执行什幺脚本语言,其取值与HTML中的SCRIPT标记的LANGUAGE属性的取值相同,缺省值是Jscript
<xsl:template>用match属性从XML选取满足条件的节点,征对这些特定的节点形成一个特定输出形式的模板。
如何调用模板(块):<xsl:apply-templates>
语法格式为:
<xsl:apply-templates select="pattern" order-by="sort-criteria-list">
属性:
select ── 确定在此上下文环境中应执行什幺模板,即选取用<xsl:template>标记建立的模板(块)。
order-by ── 以分号(;)分隔的排序标准,通常是子标记的序列
下面给出一个小例子直接复制先来看看效果:
文件temp.xml的内容:
<?xml version="1.0" encoding="GB2312"?> <!--Declaration-->
<?xml-stylesheet type="text/xsl" href="test.xsl"?><!-- PI -->
<!--注意每个xml文件中必须有个根元素 本文件中document不能去掉 否则两个resume就并列了没有根元素了-->
<document>
<resume>
<name>high</name>
<age>23</age>
</resume>
<resume>
<name>agao</name>
<age>23</age>
</resume>
</document>样式表test.xsl的代码为:
<?xml version="1.0" encoding="GB2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"><!--所有内容区分大小写-->
<xsl:template match="/"><!-- 根 -->
<BODY>
<xsl:apply-templates select="document/resume"/><!-- 调用resume的模板 -->
</BODY>
</xsl:template>
<!-- 下面是为resume元素写的模板 -->
<xsl:template match="resume"> <!-- 注意此处的match的值 不是document/resume -->
<xsl:value-of select="name"/>
<xsl:value-of select="age"/>
<BR/>
</xsl:template>
</xsl:stylesheet>
<!-- 也可以做的更具体一些
<xsl:template match="resume">
<xsl:apply-templates select="name"/> 调用name模板
<xsl:apply-templates select="age"/> 调用age模板
<BR/>
</xsl:template>
<xsl:template match="name"> 定义name模板
<xsl:value-of/>
</xsl:template>
<xsl:template match="age"> 定义age模板
<xsl:value-of/>
</xsl:template>
</xsl:stylesheet>
-->
以上,在IE6上运行正常。
4559

被折叠的 条评论
为什么被折叠?



