Limit number of items in an For-each with if-expression inside
Hi!
This is on the edge of my xslt-knowledge... I like to limit the numbers of items in an for-each with if-expression inside. Can someone point out some directions / examples would be very helpful.
The thing is that I like to limit the number of items in a <ul> <li> - list, I can not use the position()-function since I have if-expressions, like so_
<xsl:for-each select="...node [expressions]"> <xsl:sort...> <xsl:if test="[more expressions]"> <xsl:if test="show only first $maxItems"> <li> .... </li>
Perhaps I should create a nodelist with for-each and do a for-each on the new list. Dunno how really.
Hi Jonas, please post your code here so that we may review. postion() should do what you need (if I understand your snippet correctly) and what Doug suggested should do it.
Doug, the think is that the expressions got so complex so I felt the need to put some of the logic outside the for-each. Nik, the position works only on the for-each-items as I understand. But since I have the If-expression inside the for-each-loop the position does not work. Yes, a code snippet should clarify things, I'll tidy up and get back soon...
<xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']"> <hr/> Position outside if:<xsl:value-of select="position()"/><br/> <xsl:if test="position() mod 2=0"> Position inside if:<xsl:value-of select="position()"/><br/> <a href="{umbraco.library:NiceUrl(@id)}"> <xsl:value-of select="@nodeName"/> </a> </xsl:if> </xsl:for-each>
The result is something like this:
-------------------------------------------------------------------------------- Position outside if:1 -------------------------------------------------------------------------------- Position outside if:2 Position inside if: 2 Second node name -------------------------------------------------------------------------------- Position outside if:3 -------------------------------------------------------------------------------- Position outside if:4 Position inside if: 4 Fourth node name -------------------------------------------------------------------------------- Position outside if:5 -------------------------------------------------------------------------------- Position outside if:6 Position inside if: 6 Sixth node name
And I need something like this:
-------------------------------------------------------------------------------- Position outside if:1 -------------------------------------------------------------------------------- Position outside if:2 Position inside if: 1 Second node name -------------------------------------------------------------------------------- Position outside if:3 -------------------------------------------------------------------------------- Position outside if:4 Position inside if: 2 Fourth node name -------------------------------------------------------------------------------- Position outside if:5 -------------------------------------------------------------------------------- Position outside if:6 Position inside if: 3 Sixth node name
Lee, thanks for that shot from the hip :) however, the expression mod 2 was just an example and the real one is more complex and hard to calculate with. The macro is all about listing media nodes with permissions. The permissions are set with references from the media folders to content nodes. A bit hackish indeed.
No worries Jonas, I had a feeling it wasn't what you needed! Glad that you've got it working now! (If only you could mark your own posts as solutions?)
Limit number of items in an For-each with if-expression inside
Hi!
This is on the edge of my xslt-knowledge... I like to limit the numbers of items in an for-each with if-expression inside. Can someone point out some directions / examples would be very helpful.
The thing is that I like to limit the number of items in a <ul> <li> - list, I can not use the position()-function since I have if-expressions, like so_
Perhaps I should create a nodelist with for-each and do a for-each on the new list. Dunno how really.
Thanks alot!
Why not combine your if statements with the 'and' operator?
If that's not going to work for you, feel free to give us a longer snippet of your code so we can understand what you're trying to achieve.
cheers,
doug.
Hi Jonas, please post your code here so that we may review. postion() should do what you need (if I understand your snippet correctly) and what Doug suggested should do it.
Thanks,
Nik
Thank you for your inputs.
Doug, the think is that the expressions got so complex so I felt the need to put some of the logic outside the for-each. Nik, the position works only on the for-each-items as I understand. But since I have the If-expression inside the for-each-loop the position does not work. Yes, a code snippet should clarify things, I'll tidy up and get back soon...
Here's an example
The result is something like this:
And I need something like this:
Hi Jonas,
It's a bit of a cowboy hack, but try this:
... or is that TOO much cowboy? ;-)
Cheers, Lee.
Sweet, this works:
Lee, thanks for that shot from the hip :) however, the expression mod 2 was just an example and the real one is more complex and hard to calculate with. The macro is all about listing media nodes with permissions. The permissions are set with references from the media folders to content nodes. A bit hackish indeed.
Glad to see you worked it out. Position was your friend after-all :)
Cheers,
Nik
No worries Jonas, I had a feeling it wasn't what you needed! Glad that you've got it working now! (If only you could mark your own posts as solutions?)
Yes, thanks guys, position() combined with msxsl:node-set() was the key.
is working on a reply...