I have a list of elements that I want to split into individual lists of 3. The end result would be something like this:
<ul>
<li>element</li>
<li>element</li>
</ul>
<ul>
<li>element</li>
<li>element</li>
</ul>
<ul>
<li>element</li>
<li>element</li>
</ul>
My XSLT is like this, but it doesn’t work, because I can’t insert </ul>, and I can’t insert a less than sign (<).
<ul>
<xsl:for-each select="$myroot/item">
<li></li>
<xsl:if test="position() mod $maxItemsPerColumn = 0">
<!-- I want to close my ul, and start a new one here, but it doesn't work! -->
</xsl:if>
</xsl:for-each>
</ul>
Any ideas? Thanks in advance!
You don’t need to do anything fancy like recursion. And good lord, don’t even contemplate using CDATA.
You just have to think like XSLT and ask: “What input element do I want to transform into my output element?”
Assuming that each
ulis supposed to contain Nitems, you want to transform every Nth inputitem, starting with the first, into aul:Each of these
itemelements becomes aulthat contains the item and each of its N-1 following siblings:Assuming an input document like this:
…you get this output, if
$nis set to 4: