Re: [xsl] Changing table output from horizontal to vertical

Subject: Re: [xsl] Changing table output from horizontal to vertical
From: Liam R E Quin <liam@xxxxxx>
Date: Sun, 18 Sep 2011 13:16:30 -0400
On Sun, 2011-09-18 at 09:50 -0700, Mark wrote:
> Hi,
> I am using an XSLT template to produce an XHTML table.


>  Currently, the
> stylesheet fills the table in a horizontal direction; I would like to fill
> the table in a vertical direction.
Instead of thinking of filling a table, think of it as a mapping from
the input tree to the output grid... Each output tr will start with the
nth input item and then have the n + NRth item, then n + NR * 2, and so
on, where NR is the number of rows (rounded up to the nearest integer if
there's a partial row).

You can work out the total number of rows (I'd store it in an XSLT
variable for clarity) e.g. in the template for List you could do
<xsl:template match="List">
  <xsl:variable name="rowcount"
      select="(ncount(Item[@lang eq $langcode]) + 2) div 3"
      as="xs:integer" />
(if you are still in 1998 and XSLT 1, leave of the "as" attribute and
use = instead of eq)

  <!--* now you can map the Items to table cells. *-->
  <xsl:for-each
    select="Item[@lang eq $langcode][position() &lt;= $rowcount]">
    <xsl:apply-templates select="." />
  </xsl:for-each>
</xsl:template>

In this example $langcode is a global parameter to the stylesheet, but
to handle both languages make it a tunnel parameter, or in XSLT 1 make
it a literal parameter and add it as an argument to the List template,
or if List generates the tables, you could instead do,
  <xsl:for-each select="distinct-values(Item/@lang)">
    generate a table in here
  </xsl:for-each>
perhaps with an xsl:sort instruction in there :)

Liam

-- 
Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/

Current Thread