[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Calculating column widths

Subject: Re: Calculating column widths
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 17 May 2001 10:44:27 +0100
calculating the width of columns
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:variable>
  <xsl:choose>
    <xsl:when test="@colwidth">
      <xsl:variable name="width"
            select="translate(@colwidth,
                       translate(@colwidth, $digits, ''), '')" />
      <xsl:value-of select="$width + $width-of-rest" />
    </xsl:when>
    <xsl:otherwise>
       <xsl:value-of select="$width-of-rest" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

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">
  <td>
    <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:variable>
        <colgroup>
          <xsl:for-each select="colspec">
            <col>
              <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, '%')" />
                </xsl:attribute>
              </xsl:if>
            </col>
          </xsl:for-each>
        </colgroup>
      </xsl:if>
      <xsl:apply-templates/>
    </table>
  </td>
</xsl:template>

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

---
Jeni Tennison
http://www.jenitennison.com/



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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.