AWG Blogs

Friday, November 27, 2009

Vertical List Oriented Columns with XSLT

The general idea is snake your list of items a set number of times down each column. So for a list of 7 items (XML siblings) and "n-rows" being set to 3, the resulting columns will look like this:



item 1item 4item 7
item 2item 5
item 3item 6

The XSLT code:
<xsl:variable name="n-rows" select="3"/>

<xsl:template name="recurse-rows">

<xsl:param name="index" select="1"/>

<xsl:param name="modrow" select="1" />

<tr>

<xsl:for-each select="/root/item[position() mod $n-rows = $modrow]">

<td>

<xsl:value-of select="value" />

</td>

</xsl:for-each>

</tr>

<xsl:if test="$index &lt; $n-rows">

<xsl:call-template name="recurse-rows">

<xsl:with-param name="index" select="$index + 1"/>

<xsl:with-param name="modrow" select="($index + 1) mod $n-rows"/>

</xsl:call-template>

</xsl:if>

</xsl:template>


sources for ideas:
http://www.stylusstudio.com/xsllist/200407/post30810.html
http://www.perlmonks.org/?node_id=518095