summing values of column widths

Posted on 2011-02-17
Last Modified: 2013-11-18
I've got cals tables in xml and am transforming to HTML.
I need to add the column widths of the column numbers indicated in the namest and namend attributes of an entry element. So, if a column spans over col1 to col4  of an 8 column table, I want the sum total of all the widths from col1 to col4, which I would find in the colspec element preceding the entry. How would I do that in xslt? I'm not sure how I can limit the sum to only add columns 1, 2,3 and 4 of the table containing 8 columns.

I am using xslt 1.0
Question by:Stazzie
  • 3
  • 2
LVL 60

Expert Comment

by:Geert Bormans
ID: 34917940
Hi Stazzie,

an other approach could be to define the width for each column, regardless spanning in the colgroup/col element
that is an approach very similar to how cals deals with column definitions.
Then the width of each column is set correctly at the start of the table,
and all you need to do then is setting the colspan correctly based on name-start and name-end attribute of your cell
if your spanning is more than one the browser will automatically set the width right
This way you don't need to take the pixel size differences caused by the sum versus the browser calculation into account

you could calculate the spanning by iterating over the colspec elements staring with the one named as in namest and ending recursion when you hit the colspec named as in nameend

The very same way you could sum however if you really want to

For making things easier you could do this in two steps (first step listing col references explicitely in an extra attribute)

Cals tables are flexible. So I would like you to post an example if you want some code to get this done

Author Comment

ID: 34918323
<table outputclass="fin-dec" id="e13402-tbl15" rowsep="0" colsep="0">
<tgroup cols="7">
<colspec colnum="1" colname="c1" colwidth="255.36px" align="left"/>
<colspec colnum="2" colname="c2" colwidth="74.48px"/>
<colspec colnum="3" colname="c3" colwidth="74.48px"/>
<colspec colnum="4" colname="c4" colwidth="74.48px"/>
<colspec colnum="5" colname="c5" colwidth="74.48px"/>
<colspec colnum="6" colname="c6" colwidth="74.48px"/>
<colspec colnum="7" colname="c7" colwidth="74.48px"/>
<row rowsep="1" valign="bottom">
<entry colname="c1" namest="c1" nameend="c5" outputclass="shade" rowsep="1" fv="1" in="4q">Charges
we deduct from your account value at the time you request certain

The above is the code for a table - the entry is spanning columns c1 to c5.  I want to  add up the values of the column widths for colums c1 to c5 so I can use that as the width of a spanned column. There are mulitiple tables in the document and most of them include spans, but the spans cannot be anticipated, so I have to set a condition that basically says that if I've got a span, calculate the total width of the span based on the column widths that are defined in the colspec element at the top of the table.

Thank you for your help!
LVL 60

Accepted Solution

Geert Bormans earned 500 total points
ID: 34920504
The following code takes quite a bit of assumptions

- @colnum are numbered correctly and are always available (colspec don't necessarily need to be ordered)
- width is always expressed with "px"

it will work in general,
if conditions don't apply, I bet you can change the code to make it work
You generally get the idea here
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl=""
    <xsl:template match="table">
        <table border="1">
    <xsl:template match="tgroup">
        <xsl:apply-templates select="tbody"/>
    <xsl:template match="tbody">
    <xsl:template match="row">
    <xsl:template match="tr">
    <xsl:template match="entry">
        <xsl:variable name="colspecs" select="ancestor::tgroup/colspec"/>
        <xsl:variable name="col" select="@colname"></xsl:variable>
            <xsl:attribute name="width">
                    <xsl:when test="@namest and @nameend">
                        <xsl:call-template name="sum-cross-col">
                            <xsl:with-param name="colspecs" select="$colspecs"/>
                            <xsl:with-param name="namest" select="@namest"/>
                            <xsl:with-param name="nameend" select="@nameend"/>
                            <xsl:with-param name="current-sum" select="0"></xsl:with-param>
                        <xsl:value-of select="$colspecs[@colname = $col]/@colwidth"/>
    <xsl:template name="sum-cross-col">
        <xsl:param name="colspecs"/>
        <xsl:param name="current-sum"/>
        <xsl:param name="nameend"/>
        <xsl:param name="namest"/>
        <xsl:variable name="new-sum" select="$current-sum + translate($colspecs[@colname = $namest]/@colwidth, 'px', '')"/>
        <xsl:variable name="this-colnum" select="$colspecs[@colname = $namest]/@colnum"></xsl:variable>
        <xsl:variable name="next-col" select="$colspecs[@colnum = ($this-colnum + 1)]/@colname"></xsl:variable>
            <xsl:when test="$namest = $nameend">
                <xsl:value-of select="format-number($new-sum, '0.00')"/>
                <xsl:call-template name="sum-cross-col">
                    <xsl:with-param name="colspecs" select="$colspecs"/>
                    <xsl:with-param name="namest" select="$next-col"/>
                    <xsl:with-param name="nameend" select="$nameend"/>
                    <xsl:with-param name="current-sum" select="$new-sum"></xsl:with-param>

Open in new window

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Author Comment

ID: 34920978
Thank you Gertone - I will integrate this into our current xslt and let you know how it turns out. It looks like it's going to do the job. Many thanks for your help and attention!
LVL 60

Expert Comment

by:Geert Bormans
ID: 36096769
accept my answer: http:#a34920504
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36157519
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WPF issue with Trigger 2 95
Stupid git question 2 61
Import data into excel from web page 10 80
Windows and other UI names - The Proper names for the Various elements! 1 58
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now