Calculate the colwidth while namest and namend appearing in "entry" element

112 views Asked by At

I am trying to Calculated of sum tgroup/colspec/@colwidth when namest to nameend appearing in entry element.

I run the below XSLT code but expected output is not coming, Please help me Thanks in advance!

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<table>
    <tgroup cols="9">
        <colspec colwidth="60*"/>
        <colspec colwidth="1*"/>
        <colspec colwidth="1*"/>
        <colspec colwidth="12*"/>
        <colspec colwidth="1*"/>
        <colspec colwidth="1*"/>
        <colspec colwidth="1*"/>
        <colspec colwidth="12*"/>
        <colspec colwidth="1*"/>
    </tgroup>
        <thead>
            <row>
                <entry colname="col1"> </entry>
                <entry colname="col2"/>
                <entry colname="col3" namest="col3" nameend="col8">
                    <b>My content here</b>
                </entry>
                <entry colname="col9"/>
            </row>
    </thead>
</table>

My XSLT Code <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

<xsl:output method="xml"/>
    <xsl:template match="table">
        <table width="90%">
            <xsl:if test="tgroup/thead">
                <thead>
                    <xsl:for-each select="tgroup/thead/row">
                        <tr>
                            <xsl:for-each select="entry">
                                <xsl:variable name="entrycol" select="@colname"/>
                                <td>
                                    
                                    <xsl:if test="(@namest ne '') and (@nameend ne '')">
                                        <xsl:attribute name="colspan">
                                            <xsl:value-of select="number(substring-after(@nameend,'col')) - number(substring-after(@namest,'col')) + 1"/>  
                                        </xsl:attribute>
                                    </xsl:if>
                                    
                                    <xsl:attribute name="style">
                                        <xsl:if test="@colname = ancestor::tgroup/colspec/@colname">
                                            <xsl:variable name="kk1" select="format-number(sum(ancestor::tgroup/colspec/@colwidth/xs:decimal(translate(., '*', ''))), '#.##')"/>
                                            <xsl:text>width:</xsl:text>
                                            <xsl:value-of select="concat(format-number(ancestor::tgroup/colspec[@colname = $entrycol][1]/number(translate(@colwidth, '*', ''))  div number($kk1) * 100,'#.##'), '%')"/>
                                            <xsl:text>; </xsl:text>
                                        </xsl:if>
                                    </xsl:attribute>
                                    <xsl:if test="tgroup/tbody">
                                        <tbody>
                                            <tr></tr>
                                        </tbody>
                                    </xsl:if>
                                </td>
                            </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                </thead>
            </xsl:if>
        </table>
    </xsl:template>    
</xsl:stylesheet>

Expected Output

    <?xml version="1.0" encoding="UTF-8"?>
    <table width="90%">
        <thead>
            <tr>
                <td style="width:60%;">
                    <p> </p>
                </td>
                <td style="width:1%;">
                    <p></p>
                </td>
                <td colspan="6" style="width:28%;">
                    <p>
                        <b>My content here</b>
                        
                    </p>
                </td>
                <td style="width:1%;">
                    <p></p>
                </td>
            </tr>
        </thead>
        <tbody>
            <tr> </tr>
            <tr> </tr>
            <tr> </tr>
            <tr> </tr>
            <tr> </tr>
            <tr> </tr>
            <tr> </tr>
            <tr> </tr>
            <tr> </tr>
        </tbody>
    </table>
1

There are 1 answers

0
Martin Honnen On

That format is not clear to me but perhaps

   <xsl:template match="entry[@namest]">
        <xsl:variable name="col-range" select="@namest/xs:integer(replace(., 'col', '')) to @nameend/xs:integer(replace(., 'col', ''))"/>
        <tr colspan="{$col-range[last()] - $col-range[1] + 1}" width="{sum(ancestor::tgroup/colspec[position() = $col-range]/@colwidth/xs:decimal(translate(., '*', '')))}%">
            <xsl:apply-templates/>
        </tr>
    </xsl:template>

helps.