【问题标题】:Need to create @namest and @nameend attributes in the 'entry' element需要在 'entry' 元素中创建 @namest 和 @nameend 属性
【发布时间】:2021-12-16 04:44:12
【问题描述】:

如果我们得到<gridSpan val="4"/>,则在“entry”元素中创建@namest@nameend 属性。

注意: May be gridSpan element will appear in 3rd entry then @namest and @nameend will be come in the third entry

then output should be <entry namest="c1" nameend="c4">

<gridSpan val="3"/>

then output should be <entry namest="c2" nameend="c3">

<gridSpan val="2"/>

then output should be <entry namest="c1" nameend="c2">

Xml:

<tbl>
<tblPr>
<tblW w="9885" type="dxa"/>
</tblPr>
<tblGrid>
<gridCol w="1488"/>
<gridCol w="3100"/>
<gridCol w="3267"/>
<gridCol w="2021"/>
</tblGrid>
<tr>
<tc><p>Content here</p></tc>
<tc><p>content here</p></tc>
<tc>content here</tc>
<tc>contenet</tc>
</tr>
<tr>
<tc>
<tcPr>
<!-- May be gridSpan element will appear in 3rd entry then @namest and named will be come in the third entry
<gridSpan val="4"/>
</tcPr>
<p>Content here</p>
</tc>
</tr>
</tbl>

输出:

<table>
<tgroup cols="4">
<colspec colname="c1" colnum="1"/>
<colspec colname="c2" colnum="2"/>
<colspec colname="c3" colnum="3"/>
<colspec colname="c4" colnum="4"/>
<tbody>
<row>
<entry>Content here</entry>
</row>
<row>
<entry>Content here</entry>
</row>
<row>
<entry>Content here</entry>
</row>
<row>
<entry>Content here</entry>
</row>
</tbody>
</tgroup>
</table>

预期输出:

<table>
<tgroup cols="4">
<colspec colname="c1" colnum="1"/>
<colspec colname="c2" colnum="2"/>
<colspec colname="c3" colnum="3"/>
<colspec colname="c4" colnum="4"/>
<tbody>
<row>
<entry>Content here</entry>
</row>
<row>
<entry>Content here</entry>
</row>
<row>
<entry>Content here</entry>
</row>
<row namest="c1" nameend="c4">
<entry>Content here</entry>
</row>
</tbody>
</tgroup>
</table>

【问题讨论】:

    标签: xml xslt-2.0


    【解决方案1】:

    Kita Ansari,在您的模板中将 tc 元素转换为 entry 以获得 namest 值,您可以计算前面的兄弟单元格并加 1。

    <xsl:attribute name="namest" select="count(preceding-sibling::tc) + 1"/>
    

    要获得 nameend 值,您需要将 span 值添加到当前位置

    <xsl:attribute name="nameend" select="count(preceding-sibling::tc) + tcPr/gridSpan/@val"/>
    

    所以匹配 tc 的模板可以如下:

    <xsl:template match="tc">
        <entry>
            <xsl:if test="tcPr/gridSpan/@val">
                <xsl:variable name="spanValue" select="tcPr/gridSpan/@val"/>
                <xsl:attribute name="namest" select="count(preceding-sibling::tc) + 1"/>
                <xsl:attribute name="nameend" select="count(preceding-sibling::tc) + $spanValue"/>
            </xsl:if>
            <xsl:apply-templates/>
        </entry>
    </xsl:template>
    

    请注意,您的模板可以不同并包含命名空间。

    因此,示例中提供的表格输出将如下所示:

    <table>
    <tgroup cols="4">
        <colspec colname="c1" colnum="1"/>
        <colspec colname="c2" colnum="2"/>
        <colspec colname="c3" colnum="3"/>
        <colspec colname="c4" colnum="4"/>
        <tbody>
            <row>
                <entry>
                    Content here
                </entry>
                <entry>
                    content here
                </entry>
                <entry>content here</entry>
                <entry>contenet</entry>
            </row>
            <row>
                <entry namest="1" nameend="4">
                    Content here
                </entry>
            </row>
        </tbody>
    </tgroup>
    </table>
    

    namestnameend 可以添加到任何单元格,所以如果您的表格如下所示:

    <tbl>
    <tblPr>
        <tblW w="9885" type="dxa"/>
    </tblPr>
    <tblGrid>
        <gridCol w="1488"/>
        <gridCol w="3100"/>
        <gridCol w="3267"/>
        <gridCol w="2021"/>
    </tblGrid>
    <tr>
        <tc>
            <p>The first cell</p>
        </tc>
        <tc>
            <p>The second cell</p>
        </tc>
        <tc>
            <p>The third cell</p>
        </tc>
        <tc>
            <p>The fourth cell</p>
        </tc>
    </tr>
    <tr>
        <tc>
            <tcPr>
                <gridSpan val="4"/>
            </tcPr>
            <p>The first cell with gridSpan @val=4</p>
        </tc>
    </tr>
    <tr>
        <tc>
            <tcPr>
                <gridSpan val="3"/>
            </tcPr>
            <p>The first cell with gridSpan @val=3</p>
        </tc>
        <tc>
            <p>The fourth cell</p>
        </tc>
    </tr>
    <tr>
        <tc>
            <p>The first cell</p>
        </tc>
        <tc>
            <p>The second cell</p>
        </tc>
        <tc>
            <tcPr>
                <gridSpan val="2"/>
            </tcPr>
            <p>The first cell with gridSpan @val=2</p>
        </tc>
    </tr>
    </tbl>
    

    你会在输出中得到这个:

    <table>
    <tgroup cols="4">
        <colspec colname="c1" colnum="1"/>
        <colspec colname="c2" colnum="2"/>
        <colspec colname="c3" colnum="3"/>
        <colspec colname="c4" colnum="4"/>
        <tbody>
            <row>
                <entry>
                    The first cell
                </entry>
                <entry>
                    The second cell
                </entry>
                <entry>
                    The third cell
                </entry>
                <entry>
                    The fourth cell
                </entry>
            </row>
            <row>
                <entry namest="1" nameend="4">
                    The first cell with gridSpan @val=4
                </entry>
            </row>
            <row>
                <entry namest="1" nameend="3">
                    The first cell with gridSpan @val=3
                </entry>
                <entry>
                    The fourth cell
                </entry>
            </row>
            <row>
                <entry>
                    The first cell
                </entry>
                <entry>
                    The second cell
                </entry>
                <entry namest="3" nameend="4">
                    The third cell with gridSpan @val=2
                </entry>
            </row>
        </tbody>
    </tgroup>
    </table>
    

    更新:

    对于更复杂的情况,当前面的 tc 元素包含 gridSpan 时,您还需要计算前面元素的 gridSpan。所以模板可以是这样的:

        <xsl:template match="tc">
        <entry>
            <xsl:if test="tcPr/gridSpan/@val">
                <xsl:variable name="spanValue" select="tcPr/gridSpan/@val"/>
                <xsl:variable name="precedingTcNumber"
                              select="sum(for $tcElem in preceding-sibling::tc return(if($tcElem/tcPr/gridSpan/@val) then($tcElem/tcPr/gridSpan/@val) else(1)))"/>
                <xsl:attribute name="namest" select="$precedingTcNumber + 1"/>
                <xsl:attribute name="nameend" select="$precedingTcNumber + $spanValue"/>
            </xsl:if>
            <xsl:apply-templates/>
        </entry>
    </xsl:template>
    

    希望对您有所帮助。请让我知道它是否适合您。

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

    【讨论】:

    • 嗨@Vasal Krupa,上面的答案工作正常,但这是另一种不工作的情况。
    • 另一个场景输入:

      第一个带有gridSpan的单元格@val=2

      第一个带有gridSpan的单元格gridSpan @val=3

    • 应该输出另一个场景:
    • @KitaAnsari ,我为更复杂的场景更新了模板。请看上面的代码。请注意,上述评论中提供的输入不正确(单元格应在一行中)。所以你可以这样改变它:

      第一个单元格gridSpan @val=2

      gridSpan @val=6的第二个单元格

      tc>
    • @KitaAnsari 如果您使用新模板(在“更新:”文本之后)和更正的表格,您应该得到与我相同的表格。
    猜你喜欢
    • 2021-12-21
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2014-10-12
    • 2013-06-08
    相关资源
    最近更新 更多