tags:

views:

106

answers:

2

I need to include an XSLT that exists in 2 variants, depending on a param value. However, it's seems to be not possible to write an expression in the href attribute of the xsl:include element. My last trial looks like that:

< xsl:param name="ml-fmt" select="mono"/>

...
< xsl:include href="{$ml-fmt}/format.xsl"/>

The XSLT engine used is Saxon 9.2.0.6

Have anybody an idea about how I could do something close to that ?

+1  A: 

You can't.

If you know all possible xslt stylesheet modules to be included, you could use the xsl:use-when attribute in order to selectively include only some of them. However, xsl:use-when has its own limitations. To quote the XSLT 2.0 Spec:

"Any element in the XSLT namespace may have a use-when attribute whose value is an XPath expression that can be evaluated statically".

There is a way to achieve dynamic inclusion, but it requires some non-XSLT initialization:

The code (think C# or Java or ... your programming language) that invokes the transformation, can edit the DOM of the loaded (as XML) XSLT stylesheet and can set the value of the href attribute of any <xsl:import> element to the desired URL.

Dimitre Novatchev
Thanks. I finally understand that xsl:include is a compile-time thing, while the parameter is run-time !The use-when looks interested but is also compile-time as far as I understand, so it can't do the job here, right ? It reminds me the godd old #ifdef of C and C++ :)
addinquy
@addinquy: Yes, your understanding is more or less correct.
Dimitre Novatchev
+1  A: 

As Dimitre has said you can't do it, but you can generate the XSLT file from scratch or slightly modify an existing XSLT file by inserting the node in the code preparing the transformation.

newtover
I finally found that I could build up an XML parameter file and make my XSLT more generic to handle my different cases using values from this parameter file. I get the right parameter file with something like document(concat($ml-fmt','-param.xml') ...However the generic thing leads to a more complex XSLT. I think at some point it will be unreasonable tofollow the "generic direction", therefore assembling an XSLT as a first step will be a goo way to do it.Thanks for the creative answer !
addinquy