Search

It's unclear to me how to create a "default" value for a Symphony-provided param that may or may not be present. For example:

I would like to use the Shorten extension to 301 redirect a URL like example.com/1dn to example.com/entries/entry-title. In my master.xsl I have something like

<xsl:choose>
  <xsl:when test="$short-id != ''">
    <!-- HTTP Headers -->
    <xsl:text>HTTP/1.1 301 Moved Permanently
      Location: </xsl:text>
    <xsl:value-of select="concat($root,'/',/data/entries-by-short-id/section/@handle,'/',/data/entries-by-short-id/entry/title/@handle)" />
    <xsl:text>


    </xsl:text>
  </xsl:when>
  <xsl:otherwise>

    … doctype, HTML document, etc.

  </xsl:otherwise>
</xsl:choose>

My "Home" page has a URL parameter of $short-id, and when visiting a short URL everything works as expected. But when visiting example.com (sans short ID), the test fails completely because $short-id does not exist.

When I add something like <xsl:variable name="short-id" /> to my template, however, test works for the second case but the redirect fails, presumably because the URL param $short-id has been overwritten by my declaration.

I've searched the forum/Stack Overflow/the internet but not been able to figure this out. Apologies if I'm missing something obvious.

XSL variables cannot be “overwritten” per say. What should be happening is that if the URL param is set, the second declaration in the stylesheet is ignored.

When you mean it fails, are you seeing the output provided by the xsl:otherwise case?

When you mean it fails, are you seeing the output provided by the xsl:otherwise case?

Yes, sorry that was vague. When I add <xsl:variable name="short-id" /> to my template it always renders the otherwise case.

But I think your question helped my answer my own. I was putting <xsl:variable name="short-id" /> inside of <xsl:template match="/">. Moving it higher in my stylesheet (as a sibling to <xsl:template match="/">) appears to have resolved the issue. Is that what you would expect?

Even so, your variable is still empty. Not sure why, but at least it works.

Maybe something like

<xsl:variable name="short-id">
    <xsl:choose>
        <xsl:when test="/data/params/url-short-id">
            <xsl:value-of select="$short-id"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>default</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

or you can have it as a param as the first child node of the used xsl:template, instead of variable. will have than only scope in that template.

or a bit nicer way, if you can have all possible values for the short-id from XML nodes:

<xsl:variable name="short-id" select="/data/params/url-short-id | /some/other/value | /some/third/option"/>

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