This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Balancing Columns
Quoting "W. Eliot Kimber" <eliot@isogen.com>:
[...]
> I think you've misunderstood what I mean by "balanced columns". I don't
> mean columns of equal width, but columns in which the composed text is
> of equal depth across the columns, e.g.:
>
>
> xxxxxxxxxxx xxxxxxxxxx xxxxxxxxxx
> xxxxxxxxxxx xxxxxxxxxx
>
> Not:
>
> xxxxxxxxxxx xxxxxxxxxx
> xxxxxxxxxxx
> xxxxxxxxxxx
> xxxxxxxxxxx
Here's a snippet from one of my transformations. I think something like this
is what you're looking for. It outputs a list of items (passed in through the
tracked-tickers parameter) as columns in a table.
<!-- Output an HTML table of all tracked stocks. The table should
have no more than $tracked-tickers-max-columns columns and at
least $tracked-tickers-min-rows rows (unless there are fewer
than that many ticker symbols). Each ticker symbol is linked
to its Yahoo quotes page.
-->
<xsl:template name="list-tracked-stocks">
<xsl:param name="tracked-tickers"></xsl:param>
<!-- $tracked-tickers might contain the ticker-symbol elements
of track-stock elements or the ticker attributes of stock-info
elements. -->
<xsl:variable name="sorted-tickers-rtf">
<xsl:for-each select="$tracked-tickers">
<xsl:sort select="string()" data-type="text" order="ascending"/>
<xsl:variable name="ticker" select="string()"/>
<!-- need to make sure $ticker is non-empty -->
<xsl:if test="boolean($ticker)">
<!-- need to make sure there are no duplicates in
$tracked-tickers -->
<xsl:if test="generate-id(.) =
generate-id($tracked-tickers[string()=$ticker]
[1])">
<ticker-symbol>
<xsl:value-of select="$ticker"/>
</ticker-symbol>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="sorted-tickers"
select="saxon:node-set($sorted-tickers-rtf)"/>
<xsl:variable name="cell-count"
select="count($sorted-tickers/ticker-symbol)"/>
<!-- $tracked-tickers-max-columns $tracked-tickers-min-rows -->
<xsl:variable name="row-count-1"
select="$cell-count div $tracked-tickers-max-columns"/>
<xsl:variable name="row-count">
<xsl:choose>
<xsl:when test="$row-count-1 > $tracked-tickers-min-rows">
<xsl:value-of select="ceiling($row-count-1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$tracked-tickers-min-rows"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="column-count"
select="ceiling($cell-count div $row-count)"/>
<table border="0">
<!-- for $row-number from 1 to $row-count -->
<xsl:for-each select="$sorted-tickers/ticker-symbol[position()
< ($row-count + 1)]">
<xsl:variable name="row-number" select="position()"/>
<tr>
<!-- for $column-number from 1 to $column-count -->
<xsl:for-each select="$sorted-tickers/ticker-symbol[position
()
< ($column-count + 1)]">
<xsl:variable name="column-number"
select="position()"/>
<xsl:variable name="this"
select="$sorted-tickers/ticker-symbol
[position() =
($row-number +
(($column-number - 1)
* $row-count))]"/>
<td>
<xsl:choose>
<xsl:when test="$this">
<xsl:call-template name="make-yahoo-href">
<xsl:with-param name="ticker-symbol"
select="$this"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
<!-- provide adequate spacing between columns -->
<xsl:text> </xsl:text>
<xsl:text> </xsl:text>
<xsl:text> </xsl:text>
<xsl:text> </xsl:text>
<xsl:text> </xsl:text>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list