'create @colwidth attribute and value in the 'colspec' element

In the @colwidth value are coming same value from tblW/@w:w value, Please help me on this

Xml input:

<tbl>
    <tblPr>
        <tblW w="5000" type="pct"/>
    </tblPr>
    <tblGrid>
        <gridCol/>
        <gridCol/>
        <gridCol/>
    </tblGrid>
    <tr>
        <tc>
            <tcPr>
                <tcW w="2450" type="pct"/>
            </tcPr>
            <p>Content here</p>
        </tc>
        <tc>
            <tcPr>
                <tcW w="50" type="pct"/>
            </tcPr>
            <p>Content here</p>
        </tc>
        <tc>
            <tcPr>
                <tcW w="2500" type="pct"/>
            </tcPr>
            <p>Payment at Maturity:</p>
        </tc>
    </tr>
</tbl>

Output:

<table>
    <tgroup cols="3">
        <colspec colname="c1" colnum="1" colwidth="24.5%.5%25.0%"/>
        <colspec colname="c2" colnum="2" colwidth="24.5%.5%25.0%"/>
        <colspec colname="c3" colnum="3" colwidth="24.5%.5%25.0%"/>
        <tbody>
            <row>
                <entry>
                    <p>Content Here</p>
                </entry>
            </row>
<!--here all content will come-->
        </tbody>
    </tgroup>
</table>

Xslt Code:

<xsl:choose>
<xsl:when test="not(@w:w)">
<xsl:attribute name="colwidth">
<xsl:for-each select="ancestor::w:tbl/w:tr/w:tc/w:tcPr/w:tcW">
<xsl:value-of select="concat(format-number(@w:w div 100,'#.##'), '%')"/>
</xsl:for-each>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="colwidth">
<xsl:value-of select="concat(format-number(@w:w div number(ancestor::w:tbl/w:tblPr/w:tblW/@w:w) * 100,'#.##'), '%')"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>

Expected Output:

<table>
    <tgroup cols="3">
        <colspec colname="c1" colnum="1" colwidth="24.5%"/>
        <colspec colname="c2" colnum="2" colwidth="0.5%"/>
        <colspec colname="c3" colnum="3" colwidth="25.0%"/>
        <tbody>
            <row>
                <entry>
                    <p>Content Here</p>
                </entry>
            </row>
<!-- All content will come here-->
        </tbody>
    </tgroup>
</table>


Solution 1:[1]

I can assume from part of the XSLT code that you provided that you match gridCol element and insert colspec in output.

If it's correct then you need to get the position of the current gridCol and collect @w:w values from descendants of tc elements with the same position.

I added a template that matches w:gridCol with an example of how it works.

<!-- Please make sure that you match *:gridCol element in this template -->
<xsl:template match="w:gridCol">
    <!-- get position of current w:gridCol element -->
    <xsl:variable name="currentPosition" select="count(preceding-sibling::w:gridCol) + 1"/>

    <colspec>

        <!-- changed part of code provided in 'Xslt Code'-->
        <xsl:choose>
            <xsl:when test="not(@w:w)">
                <xsl:attribute name="colwidth">
                    <xsl:variable name="currentColumn" select="ancestor::w:tbl/w:tr/w:tc[position() = $currentPosition]/w:tcPr/w:tcW"/>
                    <xsl:variable name="currentColwidth" select="$currentColumn/@w:w"/>
                    <xsl:value-of select="concat(format-number($currentColwidth div 100,'0.##'), '%')"/>
                </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <xsl:attribute name="colwidth">
                    <xsl:value-of
                            select="concat(format-number(@w:w div number(ancestor::w:tbl/w:tblPr/w:tblW/@w:w) * 100,'0.##'), '%')"/>
                </xsl:attribute>
            </xsl:otherwise>
        </xsl:choose>
    </colspec>
</xsl:template>

with this template you should get:

    <colspec colwidth="24.5%"/>
    <colspec colwidth="0.5%"/>
    <colspec colwidth="25%"/>

Please note that your template can be different because your expected output contains also colname and colnum attributes. But this should be a good starting point.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman