<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- <xsl:output method="html" /> -->
  <xsl:template name="heading">
      <head>
        <title>
	  <xsl:value-of select="/report/reportdef/title" />
	</title>
	<xsl:call-template name="callStyleSheet" />
      </head>
  </xsl:template>

  <xsl:template name="callStyleSheet">
        <link rel="StyleSheet" 
	      href="tableViewStyle.css" 
	      type="text/css" 
	      media="screen,print" />
  </xsl:template>

  <xsl:template match="dataset" >
    <table cellspacing="0">
      <!-- CALL HEADING TEMPLATE 
           to print the first heading in the table -->
      <xsl:if test="not(row[1]/dataset)">	   
        <thead>	
          <xsl:apply-templates select="." mode="head" />
        </thead>
      </xsl:if>
      <tbody>
      
      <!-- LOOP OVER EACH ROW -->
      <xsl:for-each select="row">
      
        <!-- REPEAT HEADING on subsequent rows if 
	     this row contains a sub-dataset -->
        <!--<xsl:if test="dataset and self::previous-sibling"> -->
        <xsl:if test="dataset">
           <xsl:apply-templates select="../." mode="head" />
	</xsl:if>
	
	<!-- OUTPUT ALL FIELDS IN ROW -->
        <tr>
          <xsl:apply-templates select="field" />
        </tr>

        <!-- TEST FOR SUBDATASET and output it, in a seperate row -->
	<xsl:if test="dataset">
	  <tr>
	    <xsl:element name="td">
	      <xsl:attribute name="colspan">
	        <xsl:value-of select="count(field)" />
	      </xsl:attribute>
	      <xsl:apply-templates select="dataset" />

	    </xsl:element>
	  </tr>
	</xsl:if>
      </xsl:for-each>
      <xsl:apply-templates select="." mode="total" />
      </tbody>
    </table>
  </xsl:template>
  
  <!-- DATASET HEADING TEMPLATE -->
  
  <xsl:template match="dataset" mode="head">
      <!-- TITLE -->
      <tr>
        <xsl:element name="th">
	  <xsl:attribute name="colspan">
	    <xsl:value-of select="count(row[1]/field)" />
	  </xsl:attribute>
	  <xsl:attribute name="class">
	    <xsl:text>main</xsl:text>
	  </xsl:attribute>
	  <h2><xsl:value-of select="@name" /></h2>
	  <xsl:apply-templates select="." mode="heading-text" />
	</xsl:element>
      </tr>

      <!-- FIELD HEADINGS -->
      <tr>
        <xsl:for-each select="row[1]/field">
          <th>
  	    <xsl:value-of select="@name" />
	  </th>
        </xsl:for-each>
      </tr>
  </xsl:template>

  <xsl:template match="dataset" mode="total">
  </xsl:template>
  
  <xsl:template match="field">
    <td>
      <xsl:value-of select="." />
    </td>
  </xsl:template>
</xsl:stylesheet>  
