You could always convert the position into a number and it should work consitently (although, I'd argue that it should do the same for checking it as a string...):
<xsl:if test="number(position()) = 1">
It's either that or the position really is not equal to 1.
Yes, that is especially strange (I just learned this myself!) as it used to return a number on another .net project I've worked on before. So I am a bit puzzled as well.
I feel I need to chip in and say that position() does not return a string, but you could somehow say, that in XSLT you typically get the type you need without having to worry too much about it (I like that).
TT's problem here ended up being that position() was in fact never 1 inside the template - otherwise it would have worked without 'stringifying'.
If you use value-of you typically get a string back, but even then you can easily perform number stuff without explicit casting.
The only time you really need to cast variables in XSLT is when what you're trying to do could be mis-interpreted (e.g., when you find that adding 5 and 3 gives you 53 instead of 8)
position() = 1 not working in a template
Sorry, I know yet another question.
I am trying to find the first and last item in a list.
The code below works for the last, but not for the first.
What have I missed?
Many thanks.
TT
Ah, the wonders of XSLT... Calling position() actually returns a string, so this will work:
Sebastiaan
Thanks for this, this has worked on one menu, but not another.
I am now trying to look as to why (it is the same code. I am going to try and rename items just in case the two are causing problems with each other).
Thanks for the solution to the first one.
TT
You could always convert the position into a number and it should work consitently (although, I'd argue that it should do the same for checking it as a string...):
It's either that or the position really is not equal to 1.
Sebastiaan
Found the problem, I need a better brain.
I had a problem within the XSLT as I was using it, namely the first page was hidden from navigation, all solved now.
Thanks for the tip about the position returning a string (one has to ask why).
Thanks
TT
Yes, that is especially strange (I just learned this myself!) as it used to return a number on another .net project I've worked on before. So I am a bit puzzled as well.
Glad you got it worked out though!
I thanks to your help.
It would be interesting to find out why position returns a string.
Hi (all),
I feel I need to chip in and say that position() does not return a string, but you could somehow say, that in XSLT you typically get the type you need without having to worry too much about it (I like that).
TT's problem here ended up being that position() was in fact never 1 inside the template - otherwise it would have worked without 'stringifying'.
If you use value-of you typically get a string back, but even then you can easily perform number stuff without explicit casting.
The only time you really need to cast variables in XSLT is when what you're trying to do could be mis-interpreted (e.g., when you find that adding 5 and 3 gives you 53 instead of 8)
/Chriztian
Hi Chriztian
Thanks for this.
Have just checked and it now works with, or without the quotation mark.
Thanks
TT
is working on a reply...