XSLT学习笔记

XSLT全称eXtended Stylesheet Language Transformation

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="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 >
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: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:if  test ="expression/condition" >
 
</ xsl:if >

2. 类似于switch(){case n: ...}的语法
< xsl:choose >
 < xsl:when  test ="condition" > </ xsl:when >
 < xsl:when  test ="condition" > </ xsl:when >
 < xsl:otherwise > </ xsl:otherwise >
</ xsl:choose >
3.foreach语法
< xsl:for-each  select ="node1" >
  ...
</ xsl:for-each >
4.模版函数定义
< xsl:template  name ="template_name" >
   
< xsl:param  name ="parameter1" />
< xsl:param  name ="parameter2"  select ="defaultvalue" />
...
</ xsl:template >
其中,parameter2使用select属性指定了默认值defaultvalue。
对于模版函数中的参数可以用$variable来引用
实例3
< xsl:call-template  name ="template1" >
 < 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 >

IE中使用 xslt的注意点

1.在xml中引用xslt时,必须把type=text/xml改为type=text/xsl

2.必须先匹配根结点后,再匹配其他结点,否则可能无法显示,即match=/

3.IE浏览器不支持任何XSLT默认规则,因此必须自己写

添加javascript代码时的注意点
如果要在<script language="javascript"></script>中添加代码,必须使用<![CDATA[...]]>,因为添加的代码没有任何标记,会使xslt文件不符合xml格式规范

(转自:http://www.cnblogs.com/tonyqus/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值