XSL学习笔记5 mode属性

简介:
对某一个元素做多次处理,那么选择<xsl:apply-template元素处理。
如果要每次对同一个元素处理输出不同的结果,那么就需要使用mode属性。
<xsl:template>和<xsl:apply-templates>都有一个mode属性,只有在这两个元素里面同时设定了mode属性,并且属性值相同,那么模板规则才会匹配。
 
例如:
employees.xml
<? xml  version ="1.0"  encoding ="GB2312" ?> 
<? xml-stylesheet  type ="text/xsl"  href ="employees.xsl" ?> 

<!-- 这是公司雇员的信息--> 
< employees > 

     < employee  sn ="E-200402100001" > 
         < name >zhangsan </ name > 
         < age >25 </ age > 
        <!-- 月薪小于等于2000元的雇员工资,以现金方式发放--> 
         < monthly_pay  mode ="cash" > 
            1200.00 
         </ monthly_pay > 
     </ employee > 
     
     < employee  sn ="E-200402100006" > 
         < name >lisi </ name > 
         < age >28 </ age > 
         < monthly_pay  mode ="cash" > 
            1600.00 
         </ monthly_pay > 
     </ employee > 
     
     < employee  sn ="E-200503220001" > 
         < name >wangwu </ name > 
         < age >30 </ age > 
        <!-- 月薪高于2000元的雇员工资,以信用卡转帐的方式发放--> 
         < monthly_pay  mode ="credit_card" > 
            3500.00 
         </ monthly_pay > 
     </ employee > 
     
</ employees >
 
employees.xsl
<? xml  version ="1.0" ?> 
< xsl:stylesheet  version ="1.0"  xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" > 
     
     < xsl:template  match ="/" > 
         < table  border ="1" > 
             < xsl:apply-templates  select ="employees/employee"  mode ="table" /> 
         </ table > 
         < xsl:apply-templates  select ="employees/employee"  mode ="list" /> 
     </ xsl:template > 
     
     < xsl:template  match ="employee"  mode ="table" > 
         < tr > 
             < td > < xsl:value-of  select ="name" /> </ td > 
             < td > < xsl:value-of  select ="age" /> </ td > 
             < td > < xsl:value-of  select ="monthly_pay" /> </ td > 
         </ tr > 
     </ xsl:template > 
     
     < xsl:template  match ="employee"  mode ="list" > 
         < ul > 
             < li > < xsl:value-of  select ="name" /> </ li > 
             < li > < xsl:value-of  select ="age" /> </ li > 
             < li > < xsl:value-of  select ="monthly_pay" /> </ li > 
         </ ul > 
     </ xsl:template > 
     
</ xsl:stylesheet >
 
输出的HTML文件:
< table  border ="1" > 
     < tr > 
         < td >zhangsan </td> 
         < td >25 </td> 
         < td >1200.00 </td> 
     </tr> 
     < tr > 
         < td >lisi </td> 
         < td >28 </td> 
         < td >1600.00 </td> 
     </tr> 
     < tr > 
         < td >wangwu </td> 
         < td >30 </td> 
         < td >3500.00 </td> 
     </tr> 
</table> 
< ul > 
     < li >zhangsan </li> 
     < li >25 </li> 
     < li >1200.00 </li> 
</ul> 
< ul > 
     < li >lisi </li> 
     < li >28 </li> 
     < li >1600.00 </li> 
</ul> 
< ul > 
     < li >wangwu </li> 
     < li >30 </li> 
     < li >3500.00 </li> 
</ul>
 
IE中显式效果:
 
看到了吧,匹配了两次模板。


本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/60517,如需转载请自行联系原作者
相关文章
|
5月前
|
Java
jstl错误:According to TLD or attribute directive in tag file, attribute value does not accept any expr
jstl错误:According to TLD or attribute directive in tag file, attribute value does not accept any expr
|
Web App开发 XML 移动开发
JavaScript特性(attribute)、属性(property)和样式(style)
最近在研读一本巨著《JavaScript忍者秘籍》,里面有一篇文章提到了这3个概念。 书中的源码可以在此下载。我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相应示例代码。
JavaScript特性(attribute)、属性(property)和样式(style)
HTML 按钮(button)的 disable 属性和 disable property
HTML 按钮(button)的 disable 属性和 disable property
132 0
HTML 按钮(button)的 disable 属性和 disable property
|
Java Android开发
el表达式设置option标签selected
el表达式设置option标签selected
233 0
el表达式设置option标签selected
|
前端开发
css property 和 attribute 的区别
css property 和 attribute 的区别
111 0
css property 和 attribute 的区别
|
XML 数据格式
使用simple transformation查找xml file内某个节点的attribute是否存在指定value
使用simple transformation查找xml file内某个节点的attribute是否存在指定value
114 0
使用simple transformation查找xml file内某个节点的attribute是否存在指定value
|
移动开发 HTML5