Open In App

XSLT <import> Element

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

XSLT <xsl:import> element is used to include the content of one stylesheet within another. When you import a stylesheet, it allows you to incorporate the templates, definitions, and other components from the imported stylesheet into the importing stylesheet.

Syntax:

<xsl: import href=URI>
</xsl:import>

Parameters:

  • URI: It is the path of the XSLT style that we want to import.

Example 1: In this example, we will import the third.xsl style sheet which prints the student details in table format.

XML




<!--first.xsl:--> 
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl href="Third.xsl" ?>
<student>
<s>
    <name> Divyank Singh Sikarwar </name>
    <branch> CSE</branch>
    <age>18</age>
</s>
<s>
    <name> Aniket Chauhan </name>
    <branch> CSE</branch>
    <age> 20</age>
</s>
<s>
    <name> Simran Agarwal</name>
    <branch> CSE</branch>
    <age> 23</age>
</s>
<s>
    <name> Abhay Chauhan</name>
    <branch> CSE</branch>
    <age> 17</age>
</s>
<s>
    <name> Himanshu Bhatia</name>
    <branch> IT</branch>
    <age> 25</age>
</s>
</student>


XML




<!--Second.xsl:-->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  
<xsl:import href="third.xsl"/>
  
<xsl:template match="/">
  <div align='center'>
  <h1 align="center">Students' Basic Details Using Import</h1>
  <xsl:apply-imports/>
  </div>
</xsl:template>
  
</xsl:stylesheet>


XML




<!--third.xsl:--> 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
<xsl:template match="/">
    <html>
        <body>
            <h1 align="center">Students' Basic Details Using Import</h1>
            <table border="3" align="center" >
            <tr>
                <th>Name</th>
                <th>Branch</th>
                <th>Age</th>
            </tr>
            <xsl:for-each select="student/s">
                <xsl:sort select="name" order="descending"/>
                <tr>
                    <td>
                        <xsl:apply-templates select="name"/>
                    </td>
                    <td>
                        <xsl:apply-templates select="branch"/>
                    </td>
                    <td>
                        <xsl:apply-templates select="age"/>
                    </td>
                </tr>
            </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>
<xsl:template match="name">
    <span style="color:#ff0000">
        <xsl:value-of select="."/>
    </span>
    <br />
</xsl:template>
<xsl:template match="branch">
    <span style="color:#0ff000">
        <xsl:value-of select="."/>
    </span>
    <br />
</xsl:template>
<xsl:template match="age">
    <span style="color:#0000ff">
        <xsl:value-of select="."/>
    </span>
    <br />
</xsl:template>
</xsl:stylesheet>


Output:

import11

Example 1

Example 2: In this example we will import another style sheet which prints the student details in list format.

XML




<!--first.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl href="second.xsl" ?>
<student>
<s>
    <name> Divyank Singh Sikarwar </name>
    <branch>CE</branch>
    <age>18</age>
</s>
<s>
    <name> Aniket Chauhan </name>
    <branch> CSE</branch>
    <age>20</age>
</s>
<s>
    <name> Simran Agarwal</name>
    <branch> CSE</branch>
    <age>23</age>
</s>
<s>
    <name> Abhay Chauhan</name>
    <branch>ME</branch>
    <age>17</age>
</s>
</student>


XML




<!--second.xsl--> 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
<xsl:template match="/">
    <html>
        <body>  
            <h1 align="center">Students' Basic Details</h1>
            <table border="3" align="center" >
            <tr>
                <th>Details</th>
            </tr>
            <xsl:for-each select="student/s">
                <xsl:sort select="age"/>
                <tr>
                    <td>
                        <xsl:apply-templates select="name"/>
                        <xsl:apply-templates select="branch"/>
                        <xsl:apply-templates select="age"/>
                    </td>
                </tr>
            </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>
<xsl:template match="name">
    Name:
    <span style="font-family:cursive;color:#ff0000">
        <xsl:value-of select="."/>
    </span>
    <br />
</xsl:template>
<xsl:template match="branch">
    Branch:
    <span style="font-family:serif;color:#0ff000">
        <xsl:value-of select="."/>
    </span>
    <br />
</xsl:template>
<xsl:template match="age">
    Age:
    <span style="font-family:fantsy;color:#0000ff">
        <xsl:value-of select="."/>
    </span>
    <br />
</xsl:template>
</xsl:stylesheet>


XML




<!--Third.xsl-->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  
<xsl:import href="second.xsl">
</xsl:import>
  
<xsl:template match="/">
<div align='center'>
  
<h1>This is Import Part</h1>
 <xsl:apply-imports/>
</div>
</xsl:template>
  
</xsl:stylesheet>


Output:

Example 2

Overall, <xsl:import> is a powerful feature in XSLT that promotes code reusability and facilitates the creation of more organized and flexible XSLT transformations.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads