XSLT xsl:import元素

本文概述

XSLT <xsl:import>元素用于将一个样式表的内容导入到另一个样式表。导入样式表的优先级高于导入样式表。

<xsl:import href = "uri"> 
</xsl:import>

参数说明

href:用于提供要导入的xslt样式表的路径。


XSLT <xsl:import>元素示例

让我们以创建一个<employee>元素的列表为例, 该元素的属性为” id”, 其子元素<firstname>, <lastname>, <nickname>和<salary>遍历每个员工。在此示例中, 我们创建了两个xsl样式表, 其中employee_import.xsl样式表导入employee.xsl, 而employee.xml链接到employee_import.xsl。

employee.xml

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee_import.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>Employee</h2> 
            <table border = "1"> 
               <tr bgcolor = "pink"> 
                  <th>ID</th> 
                  <th>First Name</th> 
                  <th>Last Name</th> 
                  <th>Nick Name</th> 
                  <th>Salary</th> 
               </tr>
     
               <xsl:for-each select = "class/employee"> 
     
                  <tr> 
                     <td><xsl:value-of select = "@id"/></td> 
                     <td><xsl:value-of select = "firstname"/></td> 
                     <td><xsl:value-of select = "lastname"/></td> 
                     <td><xsl:value-of select = "nickname"/></td> 
                     <td><xsl:value-of select = "salary"/></td> 
                  </tr> 
               </xsl:for-each> 
            </table> 
         </body> 
      </html> 
   </xsl:template>  
</xsl:stylesheet>

Employee_import.xsl:

<?xml version = "1.0" encoding = "UTF-8"?> 
<xsl:stylesheet version = "1.0" 
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  
   <xsl:import href = "employee.xsl"/>  
   <xsl:template match = "/"> 
      <xsl:apply-imports/> 
   </xsl:template>  
</xsl:stylesheet>

输出

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