XSLT全称eXtended Stylesheet Language Transformation
xslt文件头
xslt文件头
<?
xml version="1.0" encoding="utf-8"
?>
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
重要标签解析
<xsl:template match="xpath"> 该标签用于定义模版,同时分配给指定结点
<xsl:apply-templates select="xpath"> 该标签用于指定要应用模版的结点
提示: xsl:template 中可以再次使用xsl:apply-templates,用于样式的多级嵌套
实例1:
planets.xml
<?
xml version="1.0" encoding="utf-8"
?>
<? xml-stylesheet type="text/xsl" href="planets.xslt" ?>
< planets >
< planet color ="red" >
< name > Mercury </ name >
< mass units ="(Earth=1)" > .0553 </ mass >
< day units ="days" > 58.65 </ day >
< radius units ="miles" > 1516 </ radius >
< density units ="(Earth=1)" > .983 </ density >
< distance units ="million miles" > 43.4 </ distance >
</ planet >
< planet color ="yellow" >
< name > Venus </ name >
< mass units ="(Earth=1)" > .815 </ mass >
< day units ="days" > 116.75 </ day >
< radius units ="miles" > 3716 </ radius >
< density units ="(Earth=1)" > .943 </ density >
< distance units ="million miles" > 66.8 </ distance >
</ planet >
</ planets >
<? xml-stylesheet type="text/xsl" href="planets.xslt" ?>
< planets >
< planet color ="red" >
< name > Mercury </ name >
< mass units ="(Earth=1)" > .0553 </ mass >
< day units ="days" > 58.65 </ day >
< radius units ="miles" > 1516 </ radius >
< density units ="(Earth=1)" > .983 </ density >
< distance units ="million miles" > 43.4 </ distance >
</ planet >
< planet color ="yellow" >
< name > Venus </ name >
< mass units ="(Earth=1)" > .815 </ mass >
< day units ="days" > 116.75 </ day >
< radius units ="miles" > 3716 </ radius >
< density units ="(Earth=1)" > .943 </ density >
< distance units ="million miles" > 66.8 </ distance >
</ planet >
</ planets >
planets.xslt
<?
xml version="1.0" encoding="utf-8"
?>
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
< xsl:template match ="/" >
< html >
< body >
< xsl:apply-templates />
</ body >
</ html >
</ xsl:template >
< xsl:template match ="planet" >
< p >
< xsl:value-of select ="name" />
</ p >
</ xsl:template >
</ xsl:stylesheet >
< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
< xsl:template match ="/" >
< html >
< body >
< xsl:apply-templates />
</ body >
</ html >
</ xsl:template >
< xsl:template match ="planet" >
< p >
< xsl:value-of select ="name" />
</ p >
</ xsl:template >
</ xsl:stylesheet >
以上例子中,先对所有结点使用<xsl:apply-templates>,然后再使用<xsl:template>对planet结点作处理
<xsl:attribute> 可为 html标签添加属性
实例2:<a><xsl:attribute name="href" select="http://www.cnblogs.com"></a>该语句生成的结果为<a href="http://www.cnblogs.com"></a>
<xsl:value-of select="xpath"> 获得结点的值
语法结构的使用
1. 类似于if(){...}else{}的语法
<xsl:attribute> 可为 html标签添加属性
实例2:<a><xsl:attribute name="href" select="http://www.cnblogs.com"></a>该语句生成的结果为<a href="http://www.cnblogs.com"></a>
<xsl:value-of select="xpath"> 获得结点的值
语法结构的使用
1. 类似于if(){...}else{}的语法
<
xsl:if
test
="expression/condition"
>
</ xsl:if >
</ xsl:if >
2. 类似于switch(){case n: ...}的语法
3.foreach语法
在 IE中使用 xslt的注意点
如果要在<script language="javascript"></script>中添加代码,必须使用<![CDATA[...]]>,因为添加的代码没有任何标记,会使xslt文件不符合xml格式规范
<
xsl:choose
>
< xsl:when test ="condition" > </ xsl:when >
< xsl:when test ="condition" > </ xsl:when >
< xsl:otherwise > </ xsl:otherwise >
</ xsl:choose >
< xsl:when test ="condition" > </ xsl:when >
< xsl:when test ="condition" > </ xsl:when >
< xsl:otherwise > </ xsl:otherwise >
</ xsl:choose >
<
xsl:for-each
select
="node1"
>
...
</ xsl:for-each >
...
</ xsl:for-each >
4.模版函数定义
<
xsl:template
name
="template_name"
>
< xsl:param name ="parameter1" />
< xsl:param name ="parameter2" select ="defaultvalue" />
...
< xsl:param name ="parameter1" />
< xsl:param name ="parameter2" select ="defaultvalue" />
...
</
xsl:template
>
其中,parameter2使用select属性指定了默认值defaultvalue。
对于模版函数中的参数可以用$variable来引用
实例3
对于模版函数中的参数可以用$variable来引用
实例3
<
xsl:call-template
name
="template1"
>
< xsl:with-param name ="parameter1" />
< xsl:value-of select ="$parameter1" />
</ xsl:call-template >
< xsl:with-param name ="parameter1" />
< xsl:value-of select ="$parameter1" />
</ xsl:call-template >
5.模版函数调用
<
xsl:call-template
name
="template name"
>
< xsl:with-param name ="parameter1" select ="parameter value" />
...
</ xsl:call-template >
< xsl:with-param name ="parameter1" select ="parameter value" />
...
</ xsl:call-template >
在 IE中使用 xslt的注意点
1.在xml中引用xslt时,必须把type=”text/xml”改为type=”text/xsl”
2.必须先匹配根结点后,再匹配其他结点,否则可能无法显示,即match=”/”
3.IE浏览器不支持任何XSLT默认规则,因此必须自己写
添加javascript代码时的注意点如果要在<script language="javascript"></script>中添加代码,必须使用<![CDATA[...]]>,因为添加的代码没有任何标记,会使xslt文件不符合xml格式规范