Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

summing values of column widths

Posted on 2011-02-17
8
Medium Priority
?
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
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)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

721 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