The xsl:import element is used to add (import) one stylesheet to another. You can repeat this element in order to import more than one stylesheet.
The definitions and template rules of the importing stylesheet are treated as having greater importance that the definitions and template rules of the imported stylesheet. Using a formal definition, the importing stylesheet is said to have a greater "import precedence" over the imported stylesheet.
One very important aspect of the xsl:import element is that it allows you to access a library of reusable stylesheets to create a desired appearance for the output.
The xsl:import element can only be a child of the xsl:stylesheet or the xsl:transform elements. Further, the xsl:import element must be the first element to occur after the xsl:stylesheet element (for example, before any xsl:include elements). If there is more than one stylesheet being imported, the one that is imported first has a lower import precedence than the one that is imported second, the second has lower import precedence than the third, and so on.
The similar xsl:include element can also be used to add (include) a stylesheet to another, but the definitions and template rules of the included stylesheet are treated equal to the definitions and template rules of the including stylesheet.
The xsl:apply-imports element is used to invoke (apply) any definitions and template rules of the imported stylesheet that have been overridden by the importing stylesheet.
A stylesheet should not import itself.
This is a self-closing element and it cannot contain any child elements or any content.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:import href="xslt_example_choose.xsl" />
<xsl:template match="/">
<html>
<body>
<xsl:apply-imports />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
NAME: Bugs Bunny AGE: 31
NAME: Daisy Duck AGE: 51
NAME: Minnie Mouse AGE: 24
NAME: Pluto AGE: 21
NAME: Porky Pig AGE: 44
NAME: Road Runner AGE: 48
We import and apply: xslt_example_choose.xsl. This is the code for xslt_example_import.xsl.
We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_import.xsl"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="devguru_staff/programmer">
<xsl:choose>
<xsl:when test="age < 30">
<span style="color:red;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age < 40">
<span style="color:orange;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age < 50">
<span style="color:green;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:when test="age < 60">
<span style="color:blue;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:when>
<xsl:otherwise>
<span style="color:black;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:otherwise>
</xsl:choose>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This is the code for xslt_example_choose.xsl.