Re: [xsl] Calculating column widths

Subject: Re: [xsl] Calculating column widths
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 17 May 2001 10:44:27 +0100
Hi Marc,

>> You could do the summation "by hand" in a recursive template,
>> rather than using the sum() function, but I'm not sure I'd
>> recommend this. It rather depends what you're trying to achieve.
> Yes, I had tried this, based on the example on page 502 of your book
> (1st edition). I would prefer it, but I can't get it to work. I am
> enclosing the XSLT code. In this, I am only stripping the
> proportional unit indicator "*" from the CALS colwidth value. This
> stylesheet excerpt as it stands is returning "Error" for the
> variable "rest" and so the whole calculation won't work. Also, the
> "self" variable is giving me an empty value.... (I tried with Saxon
> 6.02 or Saxon 6.3)

I think the main problem is that you need to separate getting the sum
of the widths from getting the width of an individual column.

You need a template that, given a colspec, returns the sum of the
width of that colspec, plus the sum of the width of the rest of the
colspecs in the table. Of course you can get the sum of the width of
the *rest* by getting the width of the first of those, plus the sum of
the width of the rest of them and so on recursively.

So have a template that matches that colspec and returns the result of
adding its width to the result of applying templates to the *next*
colspec in sum mode. (Here I'm using a translate() trick to get only
the numbers in the @colwidth.)

<xsl:variable name="digits" select="'0123456789.'" />

<xsl:template match="colspec" mode="sum">
  <xsl:variable name="width-of-rest">
    <xsl:apply-templates select="following-sibling::colspec[1]"
                         mode="sum" />
    <xsl:when test="@colwidth">
      <xsl:variable name="width"
                       translate(@colwidth, $digits, ''), '')" />
      <xsl:value-of select="$width + $width-of-rest" />
       <xsl:value-of select="$width-of-rest" />

Then you can apply templates to the first colspec element in 'sum'
mode to get the sum for the entire table:

   <xsl:apply-templates select="colspec[1]" mode="sum" />

And you can create the table using something like:

<xsl:template match="entrytbl">
    <table border="0" class="color">
      <xsl:if test="colspec[@colwidth or @align]">
        <xsl:variable name="total-width">
          <xsl:apply-templates select="colspec[1]" mode="sum" />
          <xsl:for-each select="colspec">
              <xsl:copy-of select="@align" />
              <xsl:if test="@colwidth">
                <xsl:variable name="width"
                    select="round(@colwidth div $total-width) * 97" />
                <xsl:attribute name="width">
                  <xsl:value-of select="concat($width, '%')" />

If you wanted to do this as separate templates, it would be best to
either pass the total table width in as a variable when to a
colspec-matching template or to have the total table width as a global
variable so it was accessible everywhere.

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread