xml - 在“colspec”元素中创建@colwidth 属性和值
问题描述
在@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>
解决方案
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%"/>
请注意,您的模板可能会有所不同,因为您的预期输出还包含colname和colnum属性。但这应该是一个很好的起点。
请让我知道它是否适合您。
最好的问候, 瓦西尔克鲁帕
推荐阅读
- django-rest-framework - 如何使用 POST 数据中的 allauth save_user 方法设置自定义用户字段
- python - 如果给定条件,如何计算欧几里得距离?
- laravel - 非法字符串偏移 '' Laravel
- c# - Identity Server 4 无法验证我的访问令牌
- python - 无法从 pycharm 引用 PyQt5
- linux - bash set -e (errexit) 仅适用于管道中的最后一个命令?
- applescript - 期待结束,但继续寻找
- excel - 希望 Excel 宏在新添加的工作表上自动运行
- java - 在 jlabel 中制作打字机效果
- excel - 如何格式化 .txt 文件中的列?