XSLT xsl:apply-template元素

本文概述

XSLT <xsl:apply-template>元素用于告诉XSLT处理器根据每个选定节点的类型和上下文找到要应用的适当模板。

<xsl:apply-template 
   select = Expression 
   mode = QName>
</xsl:apply-template>

参数说明

Index Name Description
1) select 它用于处理XPath表达式从所有节点及其子节点列表中选择的节点。
2) mode 它用于允许对由其限定名称指定的元素进行多次处理, 每次生成不同的结果。

XSLT <xsl:apply-template>元素示例

让我们以一个示例为例, 通过遍历每个员工来创建具有其属性” id”及其子元素” <firstname>”, ” <lastname>”, ” <nickname>”和” <salary>”的<employee>元素列表。

employee.xml

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?> 
<class> 
   <employee id = "001">
      <firstname>Aryan</firstname> 
      <lastname>Gupta</lastname> 
      <nickname>Raju</nickname> 
      <salary>30000</salary>
   </employee> 
   <employee id = "024"> 
      <firstname>Sara</firstname> 
      <lastname>Khan</lastname> 
      <nickname>Zoya</nickname> 
      <salary>25000</salary>
   </employee> 
   <employee id = "056"> 
      <firstname>Peter</firstname> 
      <lastname>Symon</lastname> 
      <nickname>John</nickname> 
      <salary>10000</salary> 
   </employee> 
</class>

员工.xsl

<?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> 
            <h2>Employees</h2> 
            <xsl:apply-templates select = "class/employee" /> 
         </body> 
      </html>
   </xsl:template>  

   <xsl:template match = "class/employee"> 
      <xsl:apply-templates select = "@id" /> 
      <xsl:apply-templates select = "firstname" /> 
      <xsl:apply-templates select = "lastname" /> 
      <xsl:apply-templates select = "nickname" /> 
      <xsl:apply-templates select = "salary" /> 
      <br /> 
   </xsl:template>  
   <xsl:template match = "@id"> 
      <span style = "font-size = 25px;"> 
         <xsl:value-of select = "." /> 
      </span> 
      <br /> 
   </xsl:template>  
   <xsl:template match = "firstname"> 
      First Name:<span style = "color:brown;"> 
         <xsl:value-of select = "." /> 
      </span> 
      <br /> 
   </xsl:template>  
   <xsl:template match = "lastname"> 
      Last Name:<span style = "color:green;"> 
         <xsl:value-of select = "." /> 
      </span> 
      <br /> 
   <</xsl:template>  

   <xsl:template match = "nickname"> 
      Nick Name:<span style = "color:blue;"> 
         <xsl:value-of select = "." /> 
      </span> 
      <br /> 
   </xsl:template>  
   <xsl:template match = "salary"> 
      Marks:<span style = "color:red;"> 
         <xsl:value-of select = "." />
      </span> 
      <br /> 
   </xsl:template>  	
</xsl:stylesheet>

输出

XSLT Xsl套用范本元素1
微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?