title: Trying to use XSLT to create HTML table from XML
body: <p>I'm struggling with XSLT.  I'm stuck in procedural land.
  Basically I have some XML that gets generated from a database that looks a bit like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
  &lt;report&gt;
      &lt;generated_dtm&gt;2013-03-08T18:57:26+00:00&lt;/generated_dtm&gt;
  
      &lt;range&gt;
          &lt;start_dtm&gt;2013-02-21T17:52:00+00:00&lt;/start_dtm&gt;
          &lt;end_dtm&gt;2013-03-08T17:52:00+00:00&lt;/end_dtm&gt;
      &lt;/range&gt;
  
      &lt;sensor site_code="A0001" unit_no="1" sensor_no="1"&gt;
          &lt;name&gt;Food&lt;/name&gt;
          &lt;mu_symbol&gt;°C&lt;/mu_symbol&gt;
      &lt;/sensor&gt;
  
      &lt;sensor site_code="A0001" unit_no="1" sensor_no="2"&gt;
          &lt;name&gt;Air&lt;/name&gt;
          &lt;mu_symbol&gt;°C&lt;/mu_symbol&gt;
      &lt;/sensor&gt;
  
      &lt;readings&gt;
          &lt;slot slot_dtm="2013-02-21T17:50:00+00:00"&gt;
              &lt;sensor sensor_no="1"&gt;
                  &lt;v&gt;10&lt;/v&gt;
                  &lt;status_code&gt;IR&lt;/status_code&gt;
                  &lt;status_desc&gt;In Range&lt;/status_desc&gt;
              &lt;/sensor&gt;
              &lt;sensor sensor_no="2"&gt;
                  &lt;v&gt;20&lt;/v&gt;
                  &lt;status_code&gt;Lo&lt;/status_code&gt;
                  &lt;status_desc&gt;Low&lt;/status_desc&gt;
                  &lt;/sensor&gt;
          &lt;/slot&gt;
  
          &lt;slot slot_dtm="2013-02-21T18:00:00+00:00"&gt;
              &lt;sensor sensor_no="2"&gt;
                  &lt;v&gt;21&lt;/v&gt;
                  &lt;status_code&gt;Lo&lt;/status_code&gt;
                  &lt;status_desc&gt;Low&lt;/status_desc&gt;
              &lt;/sensor&gt;
              &lt;sensor sensor_no="1"&gt;
                  &lt;v&gt;11&lt;/v&gt;
                  &lt;status_code&gt;IR&lt;/status_code&gt;
                  &lt;status_desc&gt;In Range&lt;/status_desc&gt;
              &lt;/sensor&gt;
          &lt;/slot&gt;
      &lt;/readings&gt;
  &lt;/report&gt;
  </code></pre>
  
  <p>I'm trying to end up with the readings in a HTML table, with each sensor being a column, and each row with the time down the left hand side like this:</p>
  
  <pre><code>Time                      | Food | Air
  -------------------------------------
  2013-02-21T17:50:00+00:00 | 10   | 11
  2013-02-21T18:00:00+00:00 | 20   | 22
  </code></pre>
  
  <p>Although the order of the time-slots is guaranteed to be ascending so I don't need to sort them (there could be 1000's), the problem is that within each time-slot the order of the sensors cannot be guaranteed, so I thought I would loop through the sensors I used to create the table headers each time and select the correct sensor from each slot as I iterate through the slots.  Although this doesn't work you'll probably get what I tried to do (I realise now why it doesn't work.. variables do not behave how I expected!) : -</p>
  
  <pre><code>&lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:template match="report"&gt;
    &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;Report&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;table border="0" width="100%" bgcolor="#ffffff" cellspacing="0" cellpadding="2"&gt;
          &lt;tr&gt;
            &lt;td class="column_head_above" width="70"&gt;Time&lt;/td&gt;
            &lt;xsl:for-each select="sensor"&gt;
              &lt;td class="column_head_above"&gt;&lt;xsl:value-of select="name"/&gt;&lt;xsl:text&gt; &lt;/xsl:text&gt;&lt;xsl:value-of select="mu_symbol"/&gt;&lt;/td&gt;
            &lt;/xsl:for-each&gt;
          &lt;/tr&gt;
  
          &lt;!-- go through each time slot --&gt;
  
          &lt;xsl:for-each select="readings/slot"&gt;
            &lt;tr&gt;
              &lt;xsl:variable name="sdtm" select="@slot_dtm" /&gt; 
  
              &lt;td class="table_data"&gt;&lt;xsl:value-of select="$sdtm"/&gt;&lt;/td&gt;
  
              &lt;!-- go through each sensor header --&gt;
  
              &lt;xsl:for-each select="../sensor"&gt;
                &lt;xsl:variable name="sno" select="@sensor_no" /&gt; 
                &lt;td&gt;
                  &lt;xsl:value-of select="../readings/slot[@slot_dtm=$sdtm]/sensor[@sensor_no=$sno]/v"/&gt;
                  &lt;xsl:value-of select="../readings/slot[@slot_dtm=$sdtm]/sensor[@sensor_no=$sno]/status_desc"/&gt;
                &lt;/td&gt;
              &lt;/xsl:for-each&gt;
  
            &lt;/tr&gt;
          &lt;/xsl:for-each&gt;
  
          &lt;!-- end: go through each time slot --&gt;
  
        &lt;/table&gt;
    &lt;/body&gt;
    &lt;/html&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>There can be 100's or even 1000's of time-slots, this is just a small example.
  I can adjust the hierarchy of the XML if it helps, but I cannot put the sensors in order within each time-slot without some serious rework of the database query.  I'm hoping that isn't necessary.</p>
  
  <p>Originally I had XML where the slots were separated out like this:</p>
  
  <pre><code>&lt;readings&gt;
      &lt;slot slot_dtm="2013-02-21T17:50:00+00:00"&gt;
          &lt;sensor sensor_no="1"&gt;
              &lt;v&gt;10&lt;/v&gt;
              &lt;status_code&gt;IR&lt;/status_code&gt;
              &lt;status_desc&gt;In Range&lt;/status_desc&gt;
          &lt;/sensor&gt;
      &lt;/slot&gt;
  
      &lt;slot slot_dtm="2013-02-21T17:50:00+00:00"&gt;
          &lt;sensor sensor_no="2"&gt;
              &lt;v&gt;20&lt;/v&gt;
              &lt;status_code&gt;Lo&lt;/status_code&gt;
              &lt;status_desc&gt;Low&lt;/status_desc&gt;
              &lt;/sensor&gt;
      &lt;/slot&gt;
  
      &lt;slot slot_dtm="2013-02-21T18:00:00+00:00"&gt;
          &lt;sensor sensor_no="1"&gt;
              &lt;v&gt;11&lt;/v&gt;
              &lt;status_code&gt;IR&lt;/status_code&gt;
              &lt;status_desc&gt;In Range&lt;/status_desc&gt;
          &lt;/sensor&gt;
      &lt;/slot&gt;
  
      &lt;slot slot_dtm="2013-02-21T18:00:00+00:00"&gt;
          &lt;sensor sensor_no="2"&gt;
              &lt;v&gt;21&lt;/v&gt;
              &lt;status_code&gt;Lo&lt;/status_code&gt;
              &lt;status_desc&gt;Low&lt;/status_desc&gt;
          &lt;/sensor&gt;
      &lt;/slot&gt;
  &lt;/readings&gt;
  </code></pre>
  
  <p>Which involved a much simpler database query!  Here I could guarantee the order, but the XQuery processor I'm using (Qt's QXmlQuery) does not support for-each-group so I could not find a way to group based on time.</p>
  
  <p>Sorry this is so long, I hope someone can help at least point me in the right direction.</p>
  
  <p>Thanks.</p>