需要在 'entry' 元素中创建 @namest 和 @nameend 属性

Need to create @namest and @nameend attributes in the 'entry' element

如果我们得到 <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>

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

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

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

<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 的输出将如下所示:

<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都可以添加到任何单元格,所以如果你的 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>