Q:

looping in XSLT - <xsl:for-each

I need to render the following format in XSLT. I have a <xsl:for-each loop with 5 elements  (Text1, Text2... Text5) and need to wrap an ul tag after every three elements. Any suggestions please?
<ul>
    <li>Text1</li>
    <li>Text2</li>
    <li>Text3</li>
</uL>
<ul>
    <li>Text4</li>
    <li>Text5</li>
</uL>

A:

Good question, +1.
This transformation:
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="thing[position() mod 3 = 1]">
  <ul>
   <xsl:apply-templates mode="inGrpoup" select=
    ".|following-sibling::*[not(position() > 2)]"/>
  </ul>
 </xsl:template>

 <xsl:template match="thing" mode="inGrpoup">
  <li><xsl:value-of select="."/></li>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

when applied on the following XML document:
<things>
    <thing>1</thing>
    <thing>2</thing>
    <thing>3</thing>
    <thing>4</thing>
    <thing>5</thing>
    <thing>6</thing>
    <thing>7</thing>
    <thing>8</thing>
</things>

produces the wanted, correct result (things in groups of consecutive three):
<ul>
   <li>1</li>
   <li>2</li>
   <li>3</li>
</ul>
<ul>
   <li>4</li>
   <li>5</li>
   <li>6</li>
</ul>
<ul>
   <li>7</li>
   <li>8</li>
</ul>

Explanation:

Template pattern matching the first thing of a group of three thing elements.
Using modes to process a group of thing elements (in a different way than the processing of the initial starting thing), once determined.

