RE: Making balanced two-column tables from one-column data

Subject: RE: Making balanced two-column tables from one-column data
From: Mike Brown <mbrown@xxxxxxxxxxxxx>
Date: Wed, 19 Jan 2000 12:56:21 -0700
> <xsl:template match="COMPONENTS">
>   <P>There are <xsl:value-of select="count(COMPONENT)"/> 
> components.</P>
>   <TABLE>
>      <xsl:variable name="h" select="round(count(COMPONENT) div 2)" />
>      <xsl:for-each select="COMPONENT">
>        <xsl:variable name="p" select="position()" /> 
>        <xsl:if test="$h>=$p">
>            <TR>
>                   <TD><xsl:value-of 
> select="../COMPONENT[position()=$p]"
> /></TD>
>                   <TD><xsl:value-of 
> select="../COMPONENT[position()=$p+$h]"
> /></TD>
>             </TR>
>          </xsl:if>
>       </xsl:for-each>
>   </TABLE>
> </xsl:template>
> 
> This just strikes me as the wrong way to go about it.

Since HTML tables have to be created row by row, your options are limited.
This isn't much better, but your code can be optimized like this:

<TABLE>
  <xsl:for-each select="COMPONENT[position() lt;= ceiling(count(COMPONENT)
div 2)]">
    <TR>
      <TD><xsl:value-of select="."/></TD>
      <TD>
        <xsl:choose>
          <xsl:when test="following-sibling::COMPONENT">
            <xsl:value-of select="following-sibling::COMPONENT"/>
          </xsl:when>
          <xsl:otherwise>&#160;</xsl:otherwise>
        </xsl:choose>
      </TD>
    </TR>
  </xsl:for-each>
</TABLE>

Note that the last cell will have a non-breaking space character in the
event there is an odd number of COMPONENTs.


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread