【问题标题】:create @colwidth attribute and value in the 'colspec' element在“colspec”元素中创建@colwidth 属性和值
【发布时间】:2021-11-01 10:38:17
【问题描述】:

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

【问题讨论】:

    标签: xml xslt-2.0


    【解决方案1】:

    Kita Ansari,我可以根据您提供的部分 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 属性。但这应该是一个很好的起点。

    如果它适合你,请告诉我。

    最好的问候, 瓦西尔克鲁帕

    【讨论】:

    • 非常感谢@Vasyl Krupa,它工作正常
    • 拜托,你能帮我解决另一个问题吗,请找到以下链接:
    • @KitaAnsari,当然。如果它对你有用,你能接受答案吗?我将不胜感激:)
    • 是的,我接受了你的回答
    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2013-11-16
    • 2017-02-05
    • 2021-08-30
    • 2018-02-01
    相关资源
    最近更新 更多