<?xml version="1.0"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns="http://www.w3.org/1999/xhtml"
  version="1.0">

<xsl:output method="html" indent="yes" />

<!-- create an html page from / --> 
<xsl:template match="/">
<html>
<head>
<title>SQIL Log Browser</title>
<link rel="stylesheet" type="text/css" href="log.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>

<!-- replace main with a headline and table --> 
<xsl:template match="main">
<h2>Log Browser</h2>
<a href="about.html">About</a> |
<b>Rowcount: <xsl:apply-templates select="./count" mode="move" /></b> |
<xsl:apply-templates select="../navigation" mode="nav" />
<xsl:if test="row[20]/line">
  <form method="get" action="log.sqil" class="inline">
  <xsl:call-template name="all-except">
    <xsl:with-param name="except">line_ub</xsl:with-param>
  </xsl:call-template>
  <input type="hidden" name="line_ub" >
    <xsl:attribute name="value"><xsl:value-of select="row[last()]/line" /></xsl:attribute>
  </input>
  <button class="inline">
  <b>Previous &gt;&gt;</b>
  </button>
  </form>
</xsl:if>
<br />
<!-- BAUSTELLE -->
<form method="get" action="log.sqil" 
  onsubmit="this.filter_value.name=this.filter.value; return true;" 
  class="inline">
<xsl:call-template name="all-except">
  <xsl:with-param name="except">filter</xsl:with-param>
</xsl:call-template>
Filter:
<select name="filter">
   <xsl:for-each select="/log/filter">
     <option>
    	<xsl:attribute name="value"><xsl:value-of select="@name" /></xsl:attribute>
	<xsl:value-of select="."/>
     </option>
   </xsl:for-each>
</select>
<input type="text" name="filter_value" />
<button class="inline">
<b>Add</b>
</button>
</form><br />
<!-- /BAUSTELLE -->
Remove Filter: <xsl:apply-templates select="/log/parameter" mode="remove" />
<br />
<table>
  <xsl:apply-templates />
</table>
</xsl:template>

<!-- remove filter tags -->
<xsl:template match="filter">
</xsl:template>


<!-- the forms to remove just one parameter -->
<xsl:template match="parameter" mode="remove">
<form method="get" action="log.sqil" class="inline">
<xsl:call-template name="all-except">
  <xsl:with-param name="except"><xsl:value-of select="@name" /></xsl:with-param>
</xsl:call-template>
<button class="inline">
<b><xsl:value-of select="@name" />:</b><xsl:value-of select="@value" />
</button>
</form>
</xsl:template>

<!-- List all parameters as hidden tags -->
<xsl:template name="all-except">
  <!-- except this one -->
  <xsl:param name="except" />
  <!--  && not starts-with(@name, 'line_') -->
  <xsl:apply-templates select="/log/parameter[@name!=$except and not(starts-with(@name, 'line_'))]" 
  	mode="hidden" /> 
</xsl:template>

<!-- turn a parameter into a hidden input tag -->
<xsl:template match="parameter" mode="hidden">
<input type="hidden" name="{@name}" value="{@value}" />
</xsl:template>

<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- navigation bar -->
<!-- xsl:template match="navigation" mode="nav"> 
<xsl:if test="count">
  <xsl:apply-templates  mode="nav"/>
</xsl:if>
</xsl:template -->

<!-- navigation "links" -->
<xsl:template match="navigation/row" mode="nav"> 
  <form method="get" action="log.sqil" class="inline">
  <xsl:call-template name="all-except">
    <xsl:with-param name="except">line_ub</xsl:with-param>
  </xsl:call-template>
  <input type="hidden" name="line_ub" >
    <xsl:attribute name="value"><xsl:value-of select="line_ub" /></xsl:attribute>
  </input>
  <button class="inline">
  <b>[<xsl:value-of select="idx" />]</b>
  </button>
  </form>
</xsl:template>

<!-- remove count -->
<xsl:template match="count" mode="nav"> 
</xsl:template>

<!-- remove original -->
<xsl:template match="navigation"> 
</xsl:template>

<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- main table -->
<!-- create a header row from the head row -->
<xsl:template match="main/head"> 
  <tr>
    <xsl:apply-templates/>
  </tr>
</xsl:template>

<!-- create ordinary table rows from all other rows -->
<xsl:template match="main/row">
  <tr>
    <xsl:apply-templates />
  </tr>
</xsl:template>

<!-- the header row is filled with th -->
<xsl:template match="main/head/*">
  <!-- a variable is needed because [@name=name(.)] does not work -->
  <xsl:variable name="name" >  
    <xsl:value-of select="name(.)" />
  </xsl:variable>
  <xsl:if test="count(/log/parameter[@name=$name])=0"> 
    <th class="{$name}">
      <xsl:value-of select="$name" />
    </th>
  </xsl:if>
</xsl:template>

<!-- fill a fields td tag -->
<xsl:template match="main/row/*">
  <xsl:variable name="name" >  
    <xsl:value-of select="name(.)" />
  </xsl:variable>
  <xsl:if test="count(/log/parameter[@name=$name])=0"> 
    <td class="{$name}">
      <xsl:choose>
        <!-- the active tags become td with a clickable area -->
        <xsl:when test="count(/log/active[@name=$name])>0">
          <form method="get" action="log.sqil" class="inline">
          <xsl:call-template name="all-except" />
          <input name="{$name}" value="{text()}" type="hidden" />
          <button class="inline">
          <xsl:apply-templates />
          </button>
          </form>
        </xsl:when>
        <!-- all other tags inside row become td -->
        <xsl:otherwise> 
          <xsl:apply-templates />
        </xsl:otherwise> 
      </xsl:choose>
    </td>
  </xsl:if>
</xsl:template>

<!-- count is moved to the top -->
<xsl:template match="count" mode="move">
  <xsl:apply-templates />
</xsl:template>

<!-- count is removed at the original location -->
<xsl:template match="count">
</xsl:template>

</xsl:stylesheet>


