Dynamic include statement
This is an open discussion with 2 replies, filed under XSLT.
Search
The include
statement can't contain a dynamic value, unfortunately. You could include a stylesheet with a template that takes a parameter to dictate en
or another language and call that, like so
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:include href="../utilities/master-public.xsl" /> <xsl:template match="data"> <xsl:call-template name="content-structure_Home"> <xsl:with-param name="lang" select="/data/params/url-language" /> </xsl:call-template> </xsl:template> </xsl:stylesheet>
And then in the included file
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template name="content-structure_Home"> <xsl:param name="lang" select="'en'" /> <xsl:choose> <xsl:when test="$lang = 'de'"> <!-- Do stuff for 'de' here --> </xsl:when> <xsl:when test="$lang = '..'"> <!-- Do stuff for other language here --> </xsl:when> <xsl:otherwise> <!-- Do stuff for 'en' here, default --> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
If you have a lot of repetitive code then you can call sub templates passing the parameter lang
down the chain...
It would have been too easy to use a variable... Thanks for your suggestion!
Indeed, I will have to rethink my structure (the content-structure_Home template is actually the same for all languages, only the master is different).
Create an account or sign in to comment.
I have been trying to set an iclude statement dynamically (with the code below), but it seems that variables are resolved only after any include statements.
In the sample above, I am trying to include the correct utility, called master-public-EN.xsl or master-public-DE.xsl, etc, based on the $url-language parameter (which has values EN, or DE etc).
Any ideas how to solve this, or a workaround? Thanks!