I want to add @morerows attribute in the 'entry' element. If you see in the input has <vmerge val="restart"/>
and in the next <tr>/<tc>
has <vmerge/>
then total number of <vmerge>
should be @morerows
attribute value, Please look expected output.
Please help me on this and it's very grateful for me.
Input:
<tbl>
<tblGrid>
<gridCol/>
<gridCol/>
</tblGrid>
<tr>
<tc>
<p>Content Here</p>
</tc>
<tc>
<tcPr>
<vmerge val="restart"/>
</tcPr>
<p>Content here</p>
</tc>
</tr>
<tr>
<tc>
<p>Contenet here</p>
</tc>
<tc>
<tcPr>
<vmerge/>
</tcPr>
<p>Content here</p>
</tc>
</tr>
</tbl>
Expected Output:
<table>
<tgroup rowsep="0" cols="2">
<colspec colname="c1" colnum="1" colwidth="24.94*"/>
<colspec colname="c2" colnum="2" colwidth="25.06*"/>
<tbody>
<row>
<entry align="left">
<p>content here</p>
</entry>
<entry morerows="" align="right">
<p>Contenet here</p>
</entry>
</row>
<row>
<entry align="left">
<p></p>
</entry>
<entry align="left">
<p></p>
</entry>
</row>
</tbody>
</tgroup>
</table>
CodePudding user response:
If I understood the rule correctly - you need to count tc elements with tcPr/vmerge in the same row that have preceding vmerge val="restart".
The following code should work for you:
<xsl:template match="tc">
<entry>
<!-- morerows rule -->
<xsl:if test="tcPr/vmerge[@val='restart']">
<xsl:variable name="currentTcPosition" select="count(preceding-sibling::tc) 1"/>
<xsl:variable name="vmergeId" select="generate-id(tcPr/vmerge)"/>
<xsl:variable name="followingTcRow" select="parent::tr/following-sibling::tr/tc[position() = $currentTcPosition]"/>
<xsl:variable name="morerowsValue" select="count($followingTcRow/tcPr/vmerge[not(@val='restart')][preceding::vmerge[@val='restart'][1][generate-id(.) = $vmergeId]]) 1"/>
<xsl:attribute name="morerows" select="$morerowsValue"/>
</xsl:if>
<xsl:apply-templates/>
</entry>
</xsl:template>
This is output that I got with attached input:
<table>
<tgroup rowsep="0" cols="2">
<colspec colname="c1" colnum="1" colwidth="24.94*"/>
<colspec colname="c2" colnum="2" colwidth="25.06*"/>
<tbody>
<row>
<entry>Content Here</entry>
<entry morerows="2">Content here</entry>
</row>
<row>
<entry>Contenet here</entry>
<entry>Content here</entry>
</row>
</tbody>
</tgroup></table>
Please note that if you need to ignore empty entries with tcPr/vmerge (without @val='restart' attribute) you can create empty template for them:
<xsl:template match="tc[tcPr/vmerge[not(@val='restart')]]"/>
Below you can see an example with a more complex table (without ignoring entries with tcPr/vmerge without @val='restart' attribute).
Test input:
<tbl>
<tblGrid>
<gridCol/>
<gridCol/>
</tblGrid>
<tr>
<tc>
<p>Content Here</p>
</tc>
<tc>
<tcPr>
<vmerge val="restart"/>
</tcPr>
<p>Content here</p>
</tc>
</tr>
<tr>
<tc>
<p>Contenet here</p>
</tc>
<tc>
<tcPr>
<vmerge/>
</tcPr>
<p>Content here</p>
</tc>
</tr>
<tr>
<tc>
<p>Contenet here</p>
</tc>
<tc>
<p>Content here</p>
</tc>
</tr>
<tr>
<tc>
<p>Content Here</p>
</tc>
<tc>
<tcPr>
<vmerge val="restart"/>
</tcPr>
<p>Content here</p>
</tc>
</tr>
<tr>
<tc>
<p>Contenet here</p>
</tc>
<tc>
<tcPr>
<vmerge/>
</tcPr>
<p>Content here</p>
</tc>
</tr>
<tr>
<tc>
<p>Contenet here</p>
</tc>
<tc>
<tcPr>
<vmerge/>
</tcPr>
<p>Content here</p>
</tc>
</tr>
<tr>
<tc>
<p>Contenet here</p>
</tc>
<tc>
<p>Content here</p>
</tc>
</tr></tbl>
Test output:
<table>
<tgroup rowsep="0" cols="2">
<colspec colname="c1" colnum="1" colwidth="24.94*"/>
<colspec colname="c2" colnum="2" colwidth="25.06*"/>
<tbody>
<row>
<entry>
Content Here
</entry>
<entry morerows="2">
Content here
</entry>
</row>
<row>
<entry>
Contenet here
</entry>
<entry>
Content here
</entry>
</row>
<row>
<entry>
Contenet here
</entry>
<entry>
Content here
</entry>
</row>
<row>
<entry>
Content Here
</entry>
<entry morerows="3">
Content here
</entry>
</row>
<row>
<entry>
Contenet here
</entry>
<entry>
Content here
</entry>
</row>
<row>
<entry>
Contenet here
</entry>
<entry>
Content here
</entry>
</row>
<row>
<entry>
Contenet here
</entry>
<entry>
Content here
</entry>
</row>
</tbody>
</tgroup></table>