I have an html table that needs rotating / axis swaping. It can be done off line. Any one know of a tool I can use to do it?
Asked
Active
Viewed 1,860 times
3
-
1What's the input sample? Without @colspan and @rowspan the answer is simple, otherwise the algorithm will be complex. Maybe for optimization purpose, one could built a temporaly result with nothing more than calculated cardinality (fine grained traversal passing ocuppated cells), and then output cardinatlty inversion. – Dec 10 '10 at 15:51
-
Good question, +1. See my answer for a complete and short solution. – Dimitre Novatchev Dec 10 '10 at 17:11
-
@Dimitre: With the assumption that the table has regular structure. – Dec 10 '10 at 20:01
-
@Alejandro: Yes, I think that this is what @richard wants. – Dimitre Novatchev Dec 10 '10 at 20:47
1 Answers
8
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="table">
<xsl:for-each select="tr[1]/td">
<xsl:variable name="vRowPos" select="position()"/>
<tr>
<xsl:for-each select="/table/tr">
<xsl:variable name="vColPos" select="position()"/>
<xsl:copy-of select="/table/tr[$vColPos]/td[$vRowPos]"/>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<table>
<tr>
<td>A11</td>
<td>A12</td>
<td>A13</td>
<td>A14</td>
<td>A15</td>
</tr>
<tr>
<td>A21</td>
<td>A22</td>
<td>A23</td>
<td>A24</td>
<td>A25</td>
</tr>
<tr>
<td>A31</td>
<td>A32</td>
<td>A33</td>
<td>A34</td>
<td>A35</td>
</tr>
</table>
produces the wanted, correct results:
<table>
<tr>
<td>A11</td>
<td>A21</td>
<td>A31</td>
</tr>
<tr>
<td>A12</td>
<td>A22</td>
<td>A32</td>
</tr>
<tr>
<td>A13</td>
<td>A23</td>
<td>A33</td>
</tr>
<tr>
<td>A14</td>
<td>A24</td>
<td>A34</td>
</tr>
<tr>
<td>A15</td>
<td>A25</td>
<td>A35</td>
</tr>
</table>
Do note: The assumption is that the table has regular structure.

Dimitre Novatchev
- 240,661
- 26
- 293
- 431
-
The two variables, vNumRows and vNumCols, are defined - but never used. – michael.hor257k Oct 10 '14 at 11:21
-
@michael.hor257k: Thank you for noticing this. Must be the result of incomplete edit of a previous version. Will investigate and edit. – Dimitre Novatchev Oct 10 '14 at 14:06