XSLT <xsl:import> Element

The XSLT <xsl:import> element is used to import the content of one stylesheet to another stylesheet. The importing stylesheet has higher precedence over imported stylesheet.

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

Parameter explanation

href: It is used to provide the path of xslt stylesheet to be imported.

XSLT <xsl:import> Element Example

Let's take an example to create a list of <employee> element with its attribute "id" and its child <firstname>, <lastname>, <nickname>, and <salary> iterating over each employee. In this example, we have created two xsl stylesheets where employee_import.xsl stylesheet imports employee.xsl and employee.xml is linked to employee_import.xsl.

Employee.xml

snippet
<?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>

Employee.xsl

snippet
<?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:

snippet
<?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>

Output:

XSLT Xsl import element 1
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +