需要在条目元素中创建@namest和@nameend属性 [英] Need to create @namest and @nameend attributes in the 'entry' element

查看:13
本文介绍了需要在条目元素中创建@namest和@nameend属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们获取<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>

推荐答案

Kita Ansari,在将tc元素转换为项的模板中,若要获得名称最大的值,可以对前面的同级单元格进行计数并加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"/>

匹配的so模板可以是:

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

名称名称结束可以添加到任何单元格中,因此如果您的表格如下所示:

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

我希望这对我有帮助。请让我知道它是否适用于您。

致以最诚挚的问候 瓦西尔·克鲁巴

这篇关于需要在条目元素中创建@namest和@nameend属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆