XSLT » Elements » xsl:include

Syntax:
<xsl:include
  href="uri"
/>

The xsl:include element is used to add (include) a stylesheet to another. You can repeat this element in order to include more than one stylesheet.

The definitions and template rules of the included stylesheet are treated equal to the definitions and template rules of the including stylesheet. The effect is the same as if the included stylesheet was actually part of the stylesheet with which it is being included.
 
An analogy can be made about this element with regard to the #include used in the C computer language.
 
The similar xsl:import element is also used to add (import) a stylesheet to another, but the definitions and template rules of the importing stylesheet are treated as having greater importance than the definitions and template rules of the imported stylesheet.
 
The xsl:include element can only occur as a child of the xsl:stylesheet element. All xsl:include elements must occur after all xsl:import elements.
 
A stylesheet cannot include itself, either directly or indirectly. Nor should a stylesheet be included more than once, either directly or indirectly. Both situations may cause errors due to duplication.
 
This is a self-closing element and it cannot contain any child elements or any content.

Examples

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:include href="xslt_example_template.xsl" />
<xsl:template match="devguru_staff/programmer">
<html>
<body>
<xsl:apply-templates select="name" />
<xsl:apply-templates select="dob" />
<xsl:apply-templates select="age" />
<br />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
Bugs Bunny
DOB: 03/21/1970
AGE: 31

Daisy Duck
DOB: 08/09/1949
AGE: 51

Minnie Mouse
DOB: 04/13/1977
AGE: 24

Pluto
DOB: 07/04/1979
AGE: 21

Porky Pig
DOB: 11/30/1956
AGE: 44

Road Runner
DOB: 01/19/1953
AGE: 48
Explanation:

This is the code for xslt_example_include.xsl.
 
We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_include.xsl"?>

Language(s): XSLT
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="devguru_staff/programmer">
<html>
<body>
<xsl:apply-templates select="name" />
<xsl:apply-templates select="dob" />
<xsl:apply-templates select="age" />
<br />
</body>
</html>
</xsl:template>

<xsl:template match="name">
<span style="font-size=22px;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>

<xsl:template match="dob">
DOB:
<span style="color:blue;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>

<xsl:template match="age">
AGE:
<span style="color:green;">
<xsl:value-of select="." />
</span>
<br />
</xsl:template>

</xsl:stylesheet>
Explanation:

This is the code for xslt_example_template.xsl

See Also: