Hi All,

I am trying to get the maximum no of column in a table through XSLT code. Below i had placed the sample table format. Here column tagged as 'th' and 'td'. Some of the column having 'colspan' attribute. I am struggling how to add colspan value in a variable recursively and get the maximum no of column in this table.

Please any one guide me on this. Thanks.

<table>
<tbody>
<tr>
<th></th>
<th colspan="3" align="center" rowspan="1">Institutional care</th>
<th></th>
<th colspan="2" align="center" rowspan="1">&#x2003;Bed use (days)</th>
</tr>

<tr>
<th>Control group</th>
<th align="center">Day hospital</th>
<th align="center">Control</th>
<th align="center">Odds ratio (95&#x0025; CI)</th>
<th align="center">Day hospital</th>
</tr>

<tr>
<td colspan="2">Comprehensive care (5 trials)</td>
<td align="center">151/597<xref ref-type="table-fn" rid="TF1-150"></xref></td>
<td align="center">159/584</td>
<td align="center">0.91 (0.70 to 1.19)</td>
<td align="center">20.5</td>
<td align="center">21.4</td></tr>

<tr>
<td>Domiciliary care (4 trials)</td>
<td align="center">20/216</td>
<td align="center">&#x2002;19/227</td>
<td align="center">1.61 (0.30 to 8.55)</td>
<td></td>
<td align="center">&#x2002;7.7</td>
<td align="center">11.1</td>
</tr>

<tr>
<td>No comprehensive care (3 trials)</td>
<td align="center">37/411</td>
<td align="center">&#x2002;66/403</td>
<td align="center">0.50</td>
<td></td>
<td align="center">11.2</td>
<td align="center">11.7</td>
</tr>

<tr>
<td>Total</td>
<td align="center">208/1224</td>
<td align="center">244/1214</td>
<td align="center">0.77 (0.52 to 1.13)</td>
<td></td>
<td></td>
<td align="center">15.0</td><td align="center">16.4</td>
</tr>

</tbody>
</table>

Hi All,

I am trying to get the maximum no of column in a table through XSLT code. Below i had placed the sample table format. Here column tagged as 'th' and 'td'. Some of the column having 'colspan' attribute. I am struggling how to add colspan value in a variable recursively and get the maximum no of column in this table.

Please any one guide me on this. Thanks.

<table>
<tbody>
<tr>
<th></th>
<th colspan="3" align="center" rowspan="1">Institutional care</th>
<th></th>
<th colspan="2" align="center" rowspan="1">&#x2003;Bed use (days)</th>
</tr>

<tr>
<th>Control group</th>
<th align="center">Day hospital</th>
<th align="center">Control</th>
<th align="center">Odds ratio (95&#x0025; CI)</th>
<th align="center">Day hospital</th>
</tr>

<tr>
<td colspan="2">Comprehensive care (5 trials)</td>
<td align="center">151/597<xref ref-type="table-fn" rid="TF1-150"></xref></td>
<td align="center">159/584</td>
<td align="center">0.91 (0.70 to 1.19)</td>
<td align="center">20.5</td>
<td align="center">21.4</td></tr>

<tr>
<td>Domiciliary care (4 trials)</td>
<td align="center">20/216</td>
<td align="center">&#x2002;19/227</td>
<td align="center">1.61 (0.30 to 8.55)</td>
<td></td>
<td align="center">&#x2002;7.7</td>
<td align="center">11.1</td>
</tr>

<tr>
<td>No comprehensive care (3 trials)</td>
<td align="center">37/411</td>
<td align="center">&#x2002;66/403</td>
<td align="center">0.50</td>
<td></td>
<td align="center">11.2</td>
<td align="center">11.7</td>
</tr>

<tr>
<td>Total</td>
<td align="center">208/1224</td>
<td align="center">244/1214</td>
<td align="center">0.77 (0.52 to 1.13)</td>
<td></td>
<td></td>
<td align="center">15.0</td><td align="center">16.4</td>
</tr>

</tbody>
</table>

I'll solve this problem but little confused with the question. What do u want to find out,
1- The total no. of column in 1 table or
2- The row which has the maximum number of column in this table???

Hi All,

I am trying to get the maximum no of column in a table through XSLT code. Below i had placed the sample table format. Here column tagged as 'th' and 'td'. Some of the column having 'colspan' attribute. I am struggling how to add colspan value in a variable recursively and get the maximum no of column in this table.

Please any one guide me on this. Thanks.

<table>
<tbody>
<tr>
<th></th>
<th colspan="3" align="center" rowspan="1">Institutional care</th>
<th></th>
<th colspan="2" align="center" rowspan="1">&#x2003;Bed use (days)</th>
</tr>

<tr>
<th>Control group</th>
<th align="center">Day hospital</th>
<th align="center">Control</th>
<th align="center">Odds ratio (95&#x0025; CI)</th>
<th align="center">Day hospital</th>
</tr>

<tr>
<td colspan="2">Comprehensive care (5 trials)</td>
<td align="center">151/597<xref ref-type="table-fn" rid="TF1-150"></xref></td>
<td align="center">159/584</td>
<td align="center">0.91 (0.70 to 1.19)</td>
<td align="center">20.5</td>
<td align="center">21.4</td></tr>

<tr>
<td>Domiciliary care (4 trials)</td>
<td align="center">20/216</td>
<td align="center">&#x2002;19/227</td>
<td align="center">1.61 (0.30 to 8.55)</td>
<td></td>
<td align="center">&#x2002;7.7</td>
<td align="center">11.1</td>
</tr>

<tr>
<td>No comprehensive care (3 trials)</td>
<td align="center">37/411</td>
<td align="center">&#x2002;66/403</td>
<td align="center">0.50</td>
<td></td>
<td align="center">11.2</td>
<td align="center">11.7</td>
</tr>

<tr>
<td>Total</td>
<td align="center">208/1224</td>
<td align="center">244/1214</td>
<td align="center">0.77 (0.52 to 1.13)</td>
<td></td>
<td></td>
<td align="center">15.0</td><td align="center">16.4</td>
</tr>

</tbody>
</table>

I'll solve this problem but little confused with the question. What do u want to find out,
1- The total no. of column in 1 table or
2- The row which has the maximum number of column in this table???

I'll solve this problem but little confused with the question. What do u want to find out,
1- The total no. of column in 1 table or
2- The row which has the maximum number of column in this table???

Hi Varun,

Thanks for your reply, I am trying to get the second option ( The row which has the maximum number of column in this table ).

Thanks in advance.

The row <tr> which has more number of <tr> or <td>, will have the maximum number of columns. Use :-


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="table">
<xsl:variable name="demo-xml">
<doc>
<xsl:for-each select=".//tr">
<tr>
<xsl:attribute name="columnNumber" select="count(./*)"/>
</tr>
</xsl:for-each>
</doc>
</xsl:variable>
<xsl:for-each select="$demo-xml/doc/tr">
<xsl:variable name="number" select="number(@columnNumber)"/>
<xsl:choose>
<xsl:when
test="count(preceding-sibling::tr[number(@columnNumber) &gt; $number]) + count(following-sibling::tr[number(@columnNumber) &gt; 0])"> </xsl:when>
<xsl:otherwise>
<xsl:value-of
select="concat('The row number ' ,position(),' has maximum number of columns.')"
/>
</xsl:otherwise>
</xsl:choose>

</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


output:
The row number 6 has maximum number of columns.

Hi Varun,

Thanks a lot for you solution. I got what i expect.

Regards,
Mani Muthu

Welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.