7

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 ?

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
Sami Li
  • 181
  • 1
  • 1
  • 9
  • I have no idea what you are asking. Flip the question over and say 1) what XML is your input (show **all** of it), 2) what the output currently is, and 3) show very clearly what the output HTML **should** look like. – Mathias Müller Mar 05 '15 at 22:21
  • @MathiasMüller I think the OP only want to know how to include an XSL file into another one so he can re-use his `header.xsl` file in all of his pages. – Jean-François Savard Mar 05 '15 at 23:16
  • @Jean-FrançoisSavard As you should know by now, sometimes people get it completely backwards. What they think is a solution actually isn't. But we don't know anything about the OP's problem - except that they want to "import one XSL in another". Including stylesheets might not be the correct solution. – Mathias Müller Mar 05 '15 at 23:25
  • @MathiasMüller I agree that this may be an XY problem. But you said that you had no idea what he were asking so I tought maybe you did not understand his question and I could point you in the right direction. That's right, we don't know anything about OP's problem, but he want to include the file into another which is as common use case of header file, isn't it ? I also agree that including might not be the correct solution for his *unknown* problem, but since we don't have any more details I think the best for now is to give him an answer that would actually do what he wants. – Jean-François Savard Mar 05 '15 at 23:29

1 Answers1

10

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.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76