I use XSLT Version 2.0 and I want to include a head template head.xsl
in another file home.xsl
.
home.xsl :
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<title/>
</head>
<body>
<div id="main">
<div id="content">
<xsl:call-template name="home-in"/>
</div>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
head.xsl :
xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<title/>
</head>
<body>
<div id="container">
<div id="header">
<div id="menu">
<ul>
<li><a href="#" class="active">Home</a></li>
<li><a href="#">about</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
How can I include my head.xsl
file into home.xsl
?
You can include the other file using <xsl:include>
Your home.xsl
file would look like this
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html" indent="yes"/>
<xsl:include href="head.xsl"/>
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<title/>
</head>
<body>
<div id="main">
<div id="content">
<xsl:call-template name="home-in"/>
</div>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
More details
The XSLT processor will simply replace the instruction with the content of the stylesheet named in the href
attribute. Note that an included stylesheet template will have the same default priorities and import precedence as the including stylesheet.
Don't confuse xsl:include
with xsl:import
, which is similar except that instructions in the imported stylesheet can be be overridden by instructions in the importing stylesheet and in any included stylesheet. In other word, the import precedence of elements in an imported stylesheet is always less than that of the importing stylesheet.
Since using xsl:include
is the same as copying the code in the file, make sure you don't have duplicate template name when using named templates.
Here a paste from the w3 documentation
Including a stylesheet multiple times can cause errors because of duplicate definitions. Such multiple inclusions are less obvious when they are indirect. For example, if stylesheet B includes stylesheet A, stylesheet C includes stylesheet A, and stylesheet D includes both stylesheet B and stylesheet C, then A will be included indirectly by D twice. If all of B, C and D are used as independent stylesheets, then the error can be avoided by separating everything in B other than the inclusion of A into a separate stylesheet B' and changing B to contain just inclusions of B' and A, similarly for C, and then changing D to include A, B', C'.
Note that both xsl:include
and xsl:import
elements are only allowed as top-level element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With