Example to split items into sets of 5 using last() and position():
<items><item>1</item><item>2</item>. . .<item>23</item></items>
<xsl:template match="items">
<hierarchy>
<xsl:for-each select="item[position() mod 5 = 1]">
<xsl:choose>
<xsl:when test="4 > count(following-sibling::item) ">
<subset>
<xsl:copy-of select="."/>
<xsl:copy-of select="following-sibling::item [5 > position()]"/>
</subset>
</xsl:when>
<xsl:otherwise>
<set>
<xsl:copy-of select="."/>
<xsl:copy-of select="following-sibling::item [5 > position()]"/>
</set>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</hierarchy>
</xsl:template>