Search

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.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="selectMasterPublic">
    <xsl:choose>
        <xsl:when test="/data/params/url-language = ''">
            <xsl:value-of select="EN" />
        </xsl:when>
        <xsl:otherwise test="/data/params/url-language != ''">
            <xsl:value-of select="/data/params/url-language" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

<xsl:include href="concat('../utilities/master-public-', $selectMasterPublic, '.xsl')" />

<xsl:template match="data">
    <xsl:call-template name="content-structure_Home"/>
</xsl:template>

</xsl:stylesheet>

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!

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.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details