Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

summing values of column widths

Posted on 2011-02-17
8
Medium Priority
?
364 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
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…
Suggested Courses

972 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