My source XML provides data column-by-column; the cells have varying heights. Using XSLT 1.0, I need to pivot this around to match HTML’s row-by-row table format.
Example: I need to convert input data like this
<source>
<column>
<cell height="1">col A row 1</cell>
<cell height="2">col A rows 2-3</cell>
</column>
<column>
<cell height="1">col B row 1</cell>
<cell height="1">col B row 2</cell>
<cell height="1">col B row 3</cell>
</column>
<column>
<cell height="3">col C rows 1-3</cell>
</column>
</source>
into an HTML table like this
<table>
<tr>
<td rowspan="1">col A row 1</td>
<td rowspan="1">col B row 1</td>
<td rowspan="3">col C rows 1-3</td>
</tr>
<tr>
<td rowspan="2">col A rows 2-3</td>
<td rowspan="1">col B row 2</td>
</tr>
<tr>
<td rowspan="1">col B row 3</td>
</tr>
</table>
How?
Edit: Here is another example, one where no single column has as many cells as there are table rows.
<source>
<column id="A">
<cell height="1">col A row 1</cell>
<cell height="2">col A rows 2-3</cell>
</column>
<column id="B">
<cell height="2">col B row 1-2</cell>
<cell height="1">col B row 3</cell>
</column>
<column id="C">
<cell height="3">col C rows 1-3</cell>
</column>
</source>
into an HTML table like this
<table>
<tr>
<td rowspan="1">col A row 1</td>
<td rowspan="2">col B row 1-2</td>
<td rowspan="3">col C rows 1-3</td>
</tr>
<tr>
<td rowspan="2">col A rows 2-3</td>
</tr>
<tr>
<td rowspan="1">col B row 3</td>
</tr>
</table>
I. XSLT 1.0 solution
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
II. XSLT 2.0 solution:
This is similar to the XSLT 1.0 solution, but we use the
max()function and also avoid the recursion by usig thetooperator.