Variable confusion
This is an open discussion with 4 replies, filed under XSLT.
Search
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.
Related article, Understanding whitespace in XML.
Create an account or sign in to comment.
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:
and then I did this:
It seems to be false, even though I can do this:
and get:
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:
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