Tomas
Tomas

Reputation: 333

xslt include/import path

I use XSLT as template engine in my PHP framework. Some XSLT files are in different folders,so if I want to include/import xslt template not from main directory I have to type a lot:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">   
    <xsl:import href="dir1/dir2/dir3/dir4/layout.xsl"/> 

    <xsl:template name="content">
        <html>
            <head>
                <title>action.xsl</title>
            </head>
            <body>
                test
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

Is there any easy way to reduce this dir1/dir2/dir3/dir4/layout.xsl ? dir1/dir2/dir3/dir4/ are static (never changes).

Separate template with all includes does not fit, because each template requires different files, and after some time it is difficult to understand what's included and where.

I thought perhaps ​​it is possible to do something with xml:base <xsl:import href="layout.xsl" xml:base="dir1/dir2/dir3/dir4/" />, but do not know how.

Thanks

Upvotes: 1

Views: 2169

Answers (2)

Emiliano Poggi
Emiliano Poggi

Reputation: 24846

[XSLT 1.0]

You can't use for example xsl:import like this:

 <xsl:import href="$mypath"/>

because hrefevaluates always to a string. AVT is not allowed either.


One possible solution is to define a specific stylesheet in the target folder which provides all required imports (and in this stylesheet you will need simple relative paths). In this way the main xsl will need to import only that single specific stylesheet.

For instance, transformA.xsl will import ir1/dir2/dir3/dir4/transformA_imports.xsl which will import all required files such as layout.xsl

Upvotes: 1

LarsH
LarsH

Reputation: 28014

<xsl:import href="layout.xsl" xml:base="dir1/dir2/dir3/dir4/" />

is possible in XSLT 2.0 but not in 1.0.

Did you try it in XSLT 2.0 (e.g. in Saxon), and if so, what was the result?

You cannot put import statements into templates; each import statement must be at the top level below the stylesheet element.

Upvotes: 1

Related Questions