Search

Hi All,

I had a bit of confusion yesterday while messing with variables, I've ended up not needing it now but could I get some advice about the implications of what I was trying to do?

So, if I made a variable like this:

<xsl:variable name="nav-page">
news
</xsl:variable>

and then I did this:

<xsl:if test="$nav-page = 'news'">

<h1>true</h1>

</xsl:if>

It seems to be false, even though I can do this:

<h1><xsl:value-of select="$nav-page"/></h1>

and get:

<h1>news</h1>

I'm thinking that the value that is represented by $nav-page isn't a string as I'm comparing it to above. If that's the case I found that I didn't know how to make these two things equal or make them be the same type of variable.

I had tried:

string($nav-page)

but it didn't seem to help, and then I got lost in a world of other string manipulation stew floating around my head that must belong to other languages.

All the best,

-andy

I believe you're not getting the results you expected because of white space. Try

<xsl:variable name="nav-page">news</xsl:variable>

or

<xsl:variable name="nav-page" select="string('news')" />

Thanks Lewis, that's correct.

I didn't realise but I suppose it's an LF/CF thing.

string()

didn't help, which confused me early on in my investigation. I went there first when I thought it was a variable type problem. But, I was trying it on the if statement rather than the declaration.

I tried and also this works too:

<xsl:if test="normalize-space($nav-page) = 'news'">

Cheers mate

I think there can be also used nested elements to avoid excess whitespace, e.g.

<xsl:variable name="nav-page">
    <xsl:text>news</xsl:text>
</xsl:variable>

but the select attribute is more handy in this way (or you use that tight >< syntax) and normalize-space makes it also sure for situations where you have to count with possible whitespaces around, e.g. from user inputs.

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