Solved

summing values of column widths

Posted on 2011-02-17
8
354 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
8 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 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="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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

856 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