I have a problem where a table has 100s of rows. It causes an issue and needs to be split into several smaller tables with fewer rows each.
My html is valid xml as well.
How can I split the table every x rows into a new table?
And, how can I copy the table style, and first row (header) into each subsequent table.
So something like this
<table class="..." style="...">
<tr>
<td>head 1</td>
<td>head 2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<table>
Becomes
<table class="..." style="...">
<tr>
<td>head 1</td>
<td>head 2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
</table>
<table class="..." style="...">
<tr>
<td>head 1</td>
<td>head 2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
</table>
<table class="..." style="...">
<tr>
<td>head 1</td>
<td>head 2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
</table>
<table class="..." style="...">
<tr>
<td>head 1</td>
<td>head 2</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
</tr>
<table>
This XSLT 1.0 stylesheet:
Output:
Edit: Compact code.
Here's an XSLT2 solution using for-each-group. To change the number of items per table change the divisor in the group-adjacent attribute. Tested in Oxygen/XML with Saxon 9.2.
Explanation:
Note that if you had nested tables inside the rows you'd have to modify this slightly to avoid having the "table" template match the inner tables.
This is the classical XSLT 1.0 solution for such kind of problems: