Improve company productivity with a Business Account.Sign Up

x
?
Solved

summing values of column widths

Posted on 2011-02-17
8
Medium Priority
?
370 Views
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
0
Comment
Question by:Stazzie
  • 3
  • 2
6 Comments
 
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
0
 

Author Comment

by:Stazzie
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"/>
<tbody>
<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
transactions</entry>
</row>
</tbody>
</tgroup>
</table>

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!
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 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="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="table">
        <table border="1">
            <xsl:apply-templates/>
        </table>
    </xsl:template>
    <xsl:template match="tgroup">
        <xsl:apply-templates select="tbody"/>
    </xsl:template>
    <xsl:template match="tbody">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="row">
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>
    <xsl:template match="tr">
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>
    <xsl:template match="entry">
        <xsl:variable name="colspecs" select="ancestor::tgroup/colspec"/>
        <xsl:variable name="col" select="@colname"></xsl:variable>
        <td>
            <xsl:attribute name="width">
                <xsl:choose>
                    <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:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$colspecs[@colname = $col]/@colwidth"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <xsl:apply-templates/>
        </td>
    </xsl:template>
    <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:choose>
            <xsl:when test="$namest = $nameend">
                <xsl:value-of select="format-number($new-sum, '0.00')"/>
                <xsl:text>px</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <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>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 

Author Comment

by:Stazzie
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!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36096769
recommendation:
accept my answer: http:#a34920504
0
 
LVL 143

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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I recently worked on a Wordpress site that utilized the popular ContactForm7 (https://contactform7.com/) plug-in that only sends an email and does not save data. The client wanted the data saved to a custom CRM database. This is my solution.
AngularJS web development a very simple procedure. So, to put it, in short, AngularJS’ stand out features are – Two-way data binding, MVC structure, directives, templates, dependency injections and testing.
The viewer will learn how to dynamically set the form action using jQuery.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

580 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