在 'colspec' 元素中创建 @colwidth 属性和值

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

@colwidth 值与 tblW/@w:w 值相同,请帮我解决这个问题

Xml 输入:

<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>

输出:

<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代码:

<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>

预期输出:

<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>

我可以根据您提供的部分 XSLT 代码假设您匹配 gridCol 元素并在输出中插入 colspec

如果正确则需要获取当前gridCol的位置并从具有相同位置的tc元素的后代收集@w:w值。

我添加了一个与 w:gridCol 相匹配的模板,并附有其工作原理的示例。

<!-- 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>

使用此模板您应该得到:

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

请注意,您的模板可能会有所不同,因为您的预期输出还包含 colnamecolnum 属性。但这应该是一个很好的起点。