-1

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>
Kita Ansari
  • 121
  • 7
  • 1
    What is that table input format, any well known or well defined table format? What is the output format, HTML? Have you checked whether libraries exist to transform between the two formats? – Martin Honnen Jun 14 '21 at 09:29
  • Hi @MartinHonnen: Input table format is DITA xml and output format is HTML – Kita Ansari Jun 14 '21 at 09:56
  • Please help me on this issue – Kita Ansari Jun 14 '21 at 10:28
  • But DITA OT certainly contains all the code to transform DITA to HTML. – Martin Honnen Jun 14 '21 at 12:10
  • Check whether https://www.dita-ot.org/ doesn't do the job already. – Martin Honnen Jun 14 '21 at 12:11
  • Right now we are not using DITA-OT, just simply we are using our xslt code and transform the DITA to Html – Kita Ansari Jun 14 '21 at 19:48
  • And what prevents you from using the toolkit? After all it is a tailor made tool for the input format you have to create HTML as one of its output format. It doesn't seem to make sense to try to develop a complex transformation for a complex format in a StackOverflow post if a toolkit exists that was developed to do that job. – Martin Honnen Jun 14 '21 at 20:18
  • In DITA OT there is no logic for calculating the colwidth that's why I am asking here, Please give suggestion – Kita Ansari Jun 15 '21 at 01:21

1 Answers1

0

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.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110