Blog Search
Blog Latest Comments
nice!
by Hunter Eskew on Tuesday, August 16, 2011
Hahaha, not blogging from the iPad. This time it was me trying to do 8 things at once :) But I've since corrected. And ...
by TJ Muehleman on Wednesday, May 11, 2011
Blogging from your iPad again, I see :). Great stuff, TJ! It's always so intriguing to hear where our architecture is ...
by kcoleman on Wednesday, May 11, 2011
Interesting stuff TJ! Please immediately come up with a clever acronym for cloud based OEM before Gartner or Forrester ...
by Kevin Fielding on Wednesday, May 11, 2011
Ho ADACarolyn, To further elaborate: any email generated from the community platform (invites, report abuse notificati...
by kcoleman on Thursday, May 05, 2011
Page  of  Total Items: 28

firstprevnextlast

ThePort's Product and Technology blog. We'll share helpful tips about the platform, talk about upcoming releases, and maybe on occasion share a story or two on how awesome the team is here.
TJ Muehleman
Template Trick: Looping in XSLT
Posted by TJ Muehleman
Monday, March 01, 2010
Comments (0)
One nifty trick that one of our customers showed me recently is how to control looping in XSLT. Previously, I didn't think it was possible to "break" out of a loop. That is, once you've reached 8 or 9 records, exit the loop. While the correct practice for exiting a loop is still to only ask for what you need, its still important to be able to control the ability to exit whenever you need / want to. So, without further ado, here's how you do it:

Let's assume you have an XSLT variable that contains your collection:

$SOCategories

This collection contains some number of group categories. If we only wanted to display the first two, we could do something like:

<xsl:for-each select="$SOCategories/SocialObjectCategories/SocialObjectCategoryCollection/SocialObjectCategory[position() &lt; 3]">

</xsl:for-each>

The [position() &lt; 3] XPath at the end tells the loop to only loop through the first two. Any subsequent items in the collection will be ignored.

You can use this to only display even numbered rows (using mod) or any such algorithm you wanted to craft.

Thanks to our user community for this helpful tip!
 
  • Visit TJ Muehleman's profile
    TJ Muehleman: Minor correction: I didn't think it was easy to break out of a loop :)