title: Converting Table with colspan to DataTables
body: <p>My ultimate goal is to transform HTML table with <code>colspan</code> to Dita docbook format <code>table</code> with XSLT stylesheet.</p>
  
  <p>For those who are not familiar with DITA table, it uses <code>namest</code> and <code>nameend</code> attributes to achieve <code>colspan</code>; <code>namest</code> and <code>nameend</code> are the start and end column's name respectively. The column name is specified by <code>colspecs</code> element. </p>
  
  <pre><code> &lt;colspec colname="c1" colnum="1"&gt; 
  </code></pre>
  
  <p>I have the following difficulties:</p>
  
  <p>1) I want to generate these <code>colspec</code> elements automatically.</p>
  
  <p>Because I'm process many tables, I don't have a fixed number of columns. I'm doing a count with XSLT function like below:</p>
  
  <pre><code>&lt;xsl:variable name="colNum" select="count(tbody/tr[td][1]/td[not(@*)]) + sum(tbody/tr[td][1]/td/@colspan)" /&gt;
  </code></pre>
  
  <p>then I use the following to generate <code>$colNum</code> of colspec elements, with <code>colname</code> and <code>column</code> incremented from 1 to <code>$colNum</code>.</p>
  
  <pre><code>&lt;xsl:for-each select="1 to $colNum"&gt;
      &lt;colspec colname="{concat('c', .)}" column="{.}" /&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>When I ran this stylesheet, I get error complaining <code>$colNum</code> isn't a integer. I tried to convert <code>$colNum</code> with a number of xslt functions to integers, such as: <code>ceiling(number($colNum))</code>, but the xml process insist <code>$colNum</code> is a double!</p>
  
  <p>2) find correct position of starting column of <code>colspan</code>.</p>
  
  <p>Take this table for example:</p>
  
  <pre><code>&lt;table&gt;
     &lt;tbody&gt;
        &lt;tr&gt;
           &lt;th colspan="3"&gt;
              &lt;p&gt;Name&lt;/p&gt;
           &lt;/th&gt;
           &lt;th&gt;
              &lt;p&gt;Description&lt;/p&gt;
           &lt;/th&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
           &lt;td rowspan="6"/&gt;
           &lt;td colspan="2"&gt;country-name&lt;/td&gt;
           &lt;td&gt;Country&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
           &lt;td colspan="2"&gt;extended-address&lt;/td&gt;
           &lt;td colspan="1"&gt;&lt;p&gt;Extra field for address information. &lt;/p&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>For the 3rd row, 2nd td, it's in fact the 4th column! My xslt won't be able to tell by studying just that row. I get as far as this in sudo code</p>
  
  <pre><code>position() of current td + sum(preceding-sibling[colspan])
  </code></pre>
  
  <p>This should give me 3 in this case, but I don't know how to account for the rowspan that is in fact pushing the td 1 column back.</p>
  
  <p>Let me know if there are better alternative to do this!</p>
  
  <p>I know this is a complicated problem. Any bit of help is appreciated! Thanks a million in advance!</p>