'Need to create @namest and @nameend attributes in the 'entry' element
if we get <gridSpan val="4"/>
create the @namest
and @nameend
attributes in the 'entry
' element.
NOTE: 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>
Output:
<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>
Expected output:
<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>
Solution 1:[1]
In your template that transforms tc elements into entry to get the namest value you can count preceding-sibling cells and add 1.
<xsl:attribute name="namest" select="count(preceding-sibling::tc) + 1"/>
To get the nameend value you need to add span value to your current position
<xsl:attribute name="nameend" select="count(preceding-sibling::tc) + tcPr/gridSpan/@val"/>
So template that matches tc can be following:
<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>
Please note that your template can be different and contain namespaces.
So output with table provided in example will look like this:
<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>
namest and nameend can be added to any cell so if your table will look like this:
<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>
You will get this in the output:
<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>
Update:
For more complex cases, when preceding tc elements contain gridSpan you will need to count also gridSpan of preceding elements. So template can look like so:
<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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dharman |