Default URL param values for XSL templates (not data-sources)
This is an open discussion with 5 replies, filed under XSLT.
Search
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.
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
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.