Re: [xsl] Counting Colums in WML

Subject: Re: [xsl] Counting Colums in WML
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 28 Feb 2001 19:21:20 +0000
Hi Ciaran,

>         I'm attempting to calculate the number of colums
> as the no. of columns required by the row with the most columns.
> So in my stylesheet I have something along the lines of
> <xsl:for-each select="tr">
>         <xsl:choose>
>                 <xsl:when test="count(td) &gt; count(preceding::td)">
>                         <xsl:value-of select="count(td)"/>
>                 </xsl:when>
>         </xsl:choose>
> </xsl:for-each>
> How do I store the max. value ? In a param/ variable ?

You can get the maximum just with an xsl:for-each as above: go through
the table rows, using the number of table cells they contain to
organise them in descending order.  Then pick off the first one:

  <xsl:for-each select="tr">
     <xsl:sort select="count(td)" data-type="number"
               order="descending" />
     <xsl:if test="position() = 1">
        <xsl:value-of select="count(td)" />

If you're dealing with really big tables, then you might want to use
recursion instead. You need to create a template that matches a table
row.  If it can find another row that has more cells than it does,
then apply templates to it.  If there isn't one, then give the value
of the number of cells in this row.

<xsl:template match="tr" mode="max">
   <xsl:variable name="next-larger"
                           [count(td) &gt; count(current()/td)][1]" />
      <xsl:when test="$next-larger">
         <xsl:apply-templates select="$next" mode="max" />
         <xsl:value-of select="count(td)" />

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread