【问题标题】:XSLT Check the sum of colwidth for each tgroup and add new attribute to tableXSLT 检查每个 tgroup 的 colwidth 总和并将新属性添加到表中
【发布时间】:2014-04-09 22:03:31
【问题描述】:

我有以下 XSLT,它按预期工作;现在我需要调整它,以便我可以获得每个 tgroup 的所有 colwidth 的总和,这将允许我在父表中添加一个新类以便稍后进行格式化。 使用下面的 XSLT,我一直在检查 tgroup 元素上的 @cols=”” 的值,但它不像 @colwidth 值那样准确。一些小表可能有更多的 4 列,这使它们被视为中表或大表。 目标是添加属性:@tablesize="bigtable" 为colwidth 总和等于或大于400pt 的每个tgroup,@tablesize="mediumtable" 为总colwidth 值等于或大于250pt 的tgroup 和colwidth 值小于249pt 的tgroup 添加@tablesize="smalltable" .

我还没有找到实现这一目标的直接方法,这就是我问的原因。 谢谢。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">
<topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" id="topic-id" ditaarch:DITAArchVersion="1.2">
    <title>My Tables</title>
    <body>
        <p>Some test tables that need to be transformed.</p>
        <table frame="none" id="table_t4k_bgk_sn">
            <title>Test 1 - Medium table</title>
            <tgroup cols="4">
                <colspec colname="c1" colnum="1" colwidth="75pt"/>
                <colspec colname="c2" colnum="2" colwidth="75pt"/>
                <colspec colname="c3" colnum="3" colwidth="75pt"/>
                <colspec colname="c4" colnum="4" colwidth="75pt"/>
                <thead>
                    <row>
                        <entry/>
                        <entry/>
                        <entry/>
                        <entry/>
                    </row>
                </thead>
                <tbody>
                    <row>
                        <entry>This cell 1</entry>
                        <entry>Cell 2</entry>
                        <entry>Cell 3</entry>
                        <entry>Cell 4</entry>
                    </row>
                    <row>
                        <entry>Test 2</entry>
                        <entry>Test 3</entry>
                        <entry>Test 4</entry>
                        <entry>Test 5</entry>
                    </row>
                    <row>
                        <entry>Test 7</entry>
                        <entry>Test 8</entry>
                        <entry>Test 9</entry>
                        <entry>Test 10</entry>
                    </row>
                    <row>
                        <entry>Test 12</entry>
                        <entry>Test 13</entry>
                        <entry>Test 14</entry>
                        <entry>Test 15</entry>
                    </row>
                    <row>
                        <entry>Test 17</entry>
                        <entry>Test 18</entry>
                        <entry>Test 19</entry>
                        <entry>Test 20</entry>
                    </row>
                </tbody>
            </tgroup>
        </table>
        <table frame="none" id="table_qhj_dgk_sn">
            <title>Test 2 - Big table</title>
            <tgroup cols="7">
                <colspec colname="c1" colnum="1" colwidth="75pt"/>
                <colspec colname="c2" colnum="2" colwidth="75pt"/>
                <colspec colname="c3" colnum="3" colwidth="75pt"/>
                <colspec colname="c4" colnum="4" colwidth="75pt"/>
                <colspec colname="c5" colnum="5" colwidth="75pt"/>
                <colspec colname="c6" colnum="6" colwidth="75pt"/>
                <colspec colname="c7" colnum="7" colwidth="75pt"/>
                <thead>
                    <row>
                        <entry/>
                        <entry/>
                        <entry/>
                        <entry/>
                        <entry/>
                        <entry/>
                        <entry/>
                    </row>
                </thead>
                <tbody>
                    <row>
                        <entry>Test 1</entry>
                        <entry>Test 2</entry>
                        <entry>Test 3</entry>
                        <entry> Test 4</entry>
                        <entry>Test 5</entry>
                        <entry>Test 6</entry>
                        <entry>Test 7</entry>
                    </row>
                    <row>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                    </row>
                    <row>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                    </row>
                    <row>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                        <entry>Test </entry>
                    </row>
                </tbody>
            </tgroup>
        </table>
        <table frame="none" id="table_zcb_fgk_sn">
            <title>Test 3 - Small table</title>
            <tgroup cols="2">
                <colspec colname="c1" colnum="1" colwidth="75pt"/>
                <colspec colname="c2" colnum="2" colwidth="75pt"/>
                <thead>
                    <row>
                        <entry/>
                        <entry/>
                    </row>
                </thead>
                <tbody>
                    <row>
                        <entry>Test </entry>
                        <entry>Test </entry>
                    </row>
                    <row>
                        <entry>Test </entry>
                        <entry>Test </entry>
                    </row>
                </tbody>
            </tgroup>
        </table>
    </body>
</topic>

这是我的 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[tgroup]">
        <xsl:copy>
            <xsl:choose>
                <xsl:when test="tgroup/@cols &gt;= 5">
                    <xsl:attribute name="tablesize">bigtable</xsl:attribute>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates />
                </xsl:when>

                <xsl:when test="tgroup/@cols &gt;= 3">
                    <xsl:attribute name="tablesize">mediumteble</xsl:attribute>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates />
                </xsl:when>

                <xsl:when test="tgroup/@cols &lt;= 2">
                    <xsl:attribute name="tablesize">smalltable</xsl:attribute>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    尝试用这个替换您的 *[tgroup] 模板...

    <xsl:template match="*[tgroup]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:attribute name="tablesize">
                <xsl:variable name="width" select="sum(tgroup/colspec/number(replace(@colwidth,'[^0-9]','')))"/>
                <xsl:choose>
                    <xsl:when test="$width >= 400">
                        <xsl:text>bigtable</xsl:text>
                    </xsl:when>
                    <xsl:when test="$width >= 250">
                        <xsl:text>mediumtable</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>smalltable</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>        
    </xsl:template>
    

    【讨论】:

    • 非常感谢丹尼尔,这正是我所需要的。
    【解决方案2】:

    您可以在您选择的 XPath 中使用 sum,例如 tgroup/colspec/@colwidth

    在此之前,您需要将 @colwidth 转换为数字,方法是将 pt 替换为空。还有更通用的对话解决方案here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      相关资源
      最近更新 更多