Search

I try to set a parameter or variable, but with no success… Why doesn’t this work?

<xsl:template name="checksite">     
    <xsl:choose>
        <xsl:when test="$root = 'http//www.abc.com'">
    <xsl:variable name="site" select="'abc'" />
        </xsl:when>
        <xsl:when test="$root = 'http//www.xyz.com'">
    <xsl:variable name="site" select="'xyz'" />
        </xsl:when>
    </xsl:choose>
    My variable is: <xsl:value-of select="$site" />
</xsl:template>

I tried parameters, I tried variables, but all I get is an empty result or a XSLT error.

I want to set a parameter or variable which I can use throughout the template to perform certain XSLT actions if the site is ‘abc’ or the site is ‘xyz’.

try this:

<xsl:template name="checksite">     
    <xsl:variable name="site">
        <xsl:choose>
            <xsl:when test="$root = 'http//www.abc.com'">
                <xsl:value-of select="'abc'">
            </xsl:when>
            <xsl:when test="$root = 'http//www.xyz.com'">
                <xsl:value-of select="'xyz'" />
            </xsl:when>
        </xsl:choose>
    </xsl:variable>
    My variable is: <xsl:value-of select="$site" />
</xsl:template>

And remeber that variable in xsl is not real variable once you set it, you can’t change it. It works like a constant.

Thanks! However, I already had achieved the same result in the meanwhile with a shorter approach like this:

<xsl:variable name="site" select="substring-before(substring-after($root, 'http://'), '.')" />

While I am typing it, I see my shorter approach will not work because it will give problems when vistors type ‘www.website.com’ or ‘website.com’ in their address bar :S

Looks like I’m going to use the slightly longer approach after all ;)

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