<xsl:stylesheet version="1.0" 
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:exslt="http://exslt.org/common"
		xmlns:msxsl="urn:schemas-microsoft-com:xslt"
		exclude-result-prefixes="exslt msxsl">
  

<msxsl:script language="JScript" implements-prefix="exslt">
 this['node-set'] =  function (x) {
  return x;
  }
</msxsl:script> 


<xsl:template match="/">
	<html>
		<head>
			<style type="text/css">
			table {
				width:100%;
				border-width: 1px 1px 1px 1px;
				border-spacing: 2px;
				border-style: outset outset outset outset;
				border-color: blue blue blue blue;
				border-collapse: separate;
				background-color: white;
			}
			th {
				min-width: 100px;
				height: 40px;
				border-width: 1px 1px 1px 1px;
				padding: 1px 1px 1px 1px;
				border-style: inset inset inset inset;
				border-color: gray gray gray gray;
				color: white;
				-moz-border-radius: 0px 0px 0px 0px;
			}
			th.secFilter {
				background-color: rgb(56, 114, 171);
			}
			th,th.mainFilter,th.gene {
				background-color: rgb(51, 102, 153);
			}
			td {
				text-align:center;
				border-width: 1px 1px 1px 1px;
				padding: 1px 1px 1px 1px;
				border-style: inset inset inset inset;
				border-color: gray gray gray gray;
				background-color: white;
				-moz-border-radius: 0px 0px 0px 0px;
			}
			</style>
	</head>
	<body>
		<xsl:apply-templates select="./geneValorizationXMLData"/>
	</body>
	</html>
</xsl:template>


<!-- _____________________________________GVXML_____________________________________ -->
<xsl:template match="geneValorizationXMLData">
	
	
	<center>
		<h2>GeneValorization Data</h2>
		<xsl:if test="./mainFilter[string(@value)]">
			<h3>working on <xsl:value-of select="./mainFilter/@value"/></h3>
		</xsl:if>
	</center>
	
	<xsl:apply-templates select="./genes"/>
	
	<xsl:apply-templates select="./secondaryFilters"/>
	
	<!-- <xsl:apply-templates select="./results" mode="modeNodeSet"/> -->
	<xsl:apply-templates select="./results" mode="modePath"/>
</xsl:template>

<!-- ___________________________________RESULTATS____________________________________ -->

<xsl:template match="results" mode="modePath">
	<xsl:variable name="secondaryFilters"	select="../secondaryFilters/secondaryFilter"/>
	<xsl:variable name="mainFilter"			select="../mainFilter/@value"/>
	<xsl:variable name="results"			select="./result[@mainFilter=$mainFilter]"/>

	<table>
		<tr>
			<th class="mainFilter"> </th>
			<th class="mainFilter"><xsl:value-of select="$mainFilter"/></th>
			<xsl:for-each select="../secondaryFilters/secondaryFilter">
				<th class="secFilter"><xsl:value-of select="@value"/></th>
			</xsl:for-each>
		</tr>
		<xsl:for-each select="../genes/gene">
			<xsl:variable name="geneName" select="substring-before(concat(@value,'(#',substring-before(concat(@ids,';'),';'),')(#)'),'(#)')"/>
			<tr>
				<th class="gene"><xsl:value-of select="$geneName"/></th>
				<td>
					<xsl:for-each select="../../results/result[@mainFilter=$mainFilter and @gene=$geneName and not(@secondaryFilter)]">
						<xsl:choose>
							<xsl:when test="starts-with(./@urlComputed,'cache://')">
								<xsl:value-of select="count(./item)"/>
							</xsl:when>
							<xsl:otherwise>
								<a target="blank" href="{./@urlComputed}">
									<xsl:value-of select="count(./item)"/>
								</a>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each>
				</td>
				<xsl:for-each select="../../secondaryFilters/secondaryFilter">
					<xsl:variable name="secondaryFilter" select="@value"/>
					<td>
						<xsl:for-each select="../../results/result[@mainFilter=$mainFilter and @gene=$geneName and @secondaryFilter=$secondaryFilter]">
							<xsl:choose>
								<xsl:when test="starts-with(./@urlComputed,'cache://')">
									<xsl:value-of select="count(./item)"/>
								</xsl:when>
								<xsl:otherwise>
									<a target="blank" href="{./@urlComputed}">
										<xsl:value-of select="count(./item)"/>
									</a>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:for-each>
					</td>
				</xsl:for-each>
			</tr>
		</xsl:for-each>
	</table>
</xsl:template>

<!-- ___________________________________RESULTATS____________________________________ -->

<xsl:template match="results" mode="modeNodeSet">
	<xsl:variable name="genes"				select="../genes/gene"/>
	<xsl:variable name="secondaryFilters"	select="../secondaryFilters/secondaryFilter"/>
	<xsl:variable name="mainFilter"			select="../mainFilter/@value"/>
	<xsl:variable name="results"			select="./result[@mainFilter=$mainFilter]"/>

	<table>
		<tr>
			<th class="mainFilter"> </th>
			<th class="mainFilter"><xsl:value-of select="$mainFilter"/></th>
			<xsl:for-each select="exslt:node-set($secondaryFilters)">
				<th class="secFilter"><xsl:value-of select="@value"/></th>
			</xsl:for-each>
		</tr>
		<xsl:for-each select="exslt:node-set($genes)">
			<xsl:variable name="geneName" select="substring-before(concat(@value,'(#',substring-before(concat(@ids,';'),';'),')(#)'),'(#)')"/>
			<xsl:variable name="resultsForAGene" select="$results[@gene=$geneName]"/>
			<tr>
				<th class="gene"><xsl:value-of select="$geneName"/></th>
				<td>
					<xsl:for-each select="$resultsForAGene[not(@secondaryFilter)]">
						<xsl:choose>
							<xsl:when test="starts-with(@urlComputed,'cache://')">
								<xsl:value-of select="count(./item)"/>
							</xsl:when>
							<xsl:otherwise>
								<a target="blank" href="{@urlComputed}">
									<xsl:value-of select="count(./item)"/>
								</a>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each>
				</td>
				<xsl:for-each select="exslt:node-set($secondaryFilters)">
					<xsl:variable name="secondaryFilter" select="@value"/>
					<td>
						<xsl:for-each select="$resultsForAGene[@secondaryFilter=$secondaryFilter]">
							<xsl:variable name="urlComputed" select="@urlComputed"/>
							<xsl:choose>
								<xsl:when test="starts-with(@urlComputed,'cache://')">
									<xsl:value-of select="count(./item)"/>
								</xsl:when>
								<xsl:otherwise>
									<a target="blank" href="{@urlComputed}">
										<xsl:value-of select="count(./item)"/>
									</a>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:for-each>
					</td>
				</xsl:for-each>
			</tr>
		</xsl:for-each>
	</table>
</xsl:template>


<!-- _____________________________________GENES_____________________________________ -->
<xsl:template match="genes">
	<table>
		<tr>
			<th>Genes (<xsl:value-of select="count(./gene)"/>)</th>
		</tr>
		<tr>
			<td>
				<xsl:for-each select="./gene">
					<xsl:value-of select="@value"/>, 
				</xsl:for-each>
			</td>
		</tr>
	</table>
	<br/>
</xsl:template>

<!-- _______________________________SECONDARY FILTERS_______________________________ -->
<xsl:template match="secondaryFilters">
	<table>
		<tr>
			<th>Secondary Filters (<xsl:value-of select="count(./secondaryFilter)"/>)</th>
		</tr>
		<tr>
			<td>
				<xsl:for-each select="./secondaryFilter">
					<xsl:value-of select="@value"/>, 
				</xsl:for-each>
			</td>
		</tr>
	</table>
	<br/>
</xsl:template>
</xsl:stylesheet> 

