<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="gotoelement" match="element" use="@id"/>
<xsl:key name="gotoprocess" match="process" use="@id"/>

<xsl:template match="/">
	<html>
		<head>
			<link rel="stylesheet" type="text/css" href="style.css" />
		</head>
		<body>
			<xsl:apply-templates select="patplot"/>  
		</body>
	</html>
</xsl:template>

<xsl:template match="patplot">
	<div class="studybox">
		<xsl:apply-templates select="study"/>
	
		<!-- available in XSLT 2.0 -->
		<!-- <xsl:value-of select="current_date()"/> -->
				
		<xsl:apply-templates select="steps/step"/>
	</div>
</xsl:template>

<xsl:template match="study">
	<div class="studytitle"><xsl:value-of select="title"/></div>
	<table class="fields">
		<xsl:apply-templates select="fields/field"/>
	</table>
</xsl:template>

<xsl:template match="steps/step">
    <div class="stepbox">
		<div class="steptitle"><xsl:apply-templates select="title"/></div>
		<table class="nofields">
			<xsl:apply-templates select="fields/field"/>
		</table>
	
		<xsl:apply-templates select="elements"/>
	
		<xsl:apply-templates select="processes">
			<xsl:with-param name="currentstep" select="@id"/>
		</xsl:apply-templates>
	</div>
</xsl:template>

<xsl:template match="fields/field">
	<xsl:if test="label!='Icon'">
		<tr>	
			<td><xsl:value-of select="label"/></td>
			<td>
				<!-- <xsl:value-of select="value"/> -->
				<!-- call template repNL to add a <br/> for New Line on the text -->
				<xsl:call-template name="repNL">
					<xsl:with-param name="pText" select="value"/>
				</xsl:call-template>
			</td>
		</tr>
	</xsl:if>
</xsl:template>

<xsl:template match="text()" name="repNL">
	<xsl:param name="pText" select='.'/>
	
	<xsl:copy-of select="substring-before(concat($pText, '&#xA;'), '&#xA;')"/>
	
	<xsl:if test="contains($pText, '&#xA;')">
		<br/>
		<xsl:call-template name="repNL">
			<xsl:with-param name="pText" select="substring-after($pText, '&#xA;')"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

<xsl:template match="elements">
	<div class="elementsbox">
		<div class="elementslabel">Elements:</div>
		<xsl:apply-templates select="key('gotoelement', element)"/>
	</div>
</xsl:template>

<xsl:template match="element">
	<div class="elementtitle"><xsl:value-of select="title"/></div>
	<table class="nofields">
		<xsl:apply-templates select="fields/field"/>
	</table>
</xsl:template>

<xsl:template match="processes">
    <xsl:param name="currentstep"/>
	<div class="processeslabel">Processes:</div>
	
	<!-- Display Processes only when exist -->
	<xsl:if test="count(process) &gt; 0">
		<table class="grouptable">
			<tr>
				<xsl:apply-templates select="//groups/group">
					<xsl:with-param name="currentstep" select="$currentstep"/>
				</xsl:apply-templates>
			</tr>
		</table>
	</xsl:if>
</xsl:template>

<xsl:template match="//groups/group">
	<xsl:param name="currentstep"/>
	<xsl:variable name="currentgroup" select="@id"/>
	<td class="ontop">
		<div class="grouptitle"><xsl:value-of select="title"/></div>	
		<xsl:apply-templates select="key('gotoprocess', //steps/step[@id=$currentstep]/processes/process[@groupid=$currentgroup])"/>
	</td>
</xsl:template>

<xsl:template match="process">
	<div class="processbox">
		<div class="processtitle"><xsl:value-of select="title"/></div>
		<table class="nofields">
			<xsl:apply-templates select="fields/field"/>
		</table>
		<xsl:apply-templates select="elements"/>
	</div>
</xsl:template>

</xsl:stylesheet>


<!--		
<xsl:for-each select="steps/step">
	<xsl:sort select="@sort" data-type="number"/>
	<h2><xsl:value-of select="title"/></h2>
	<xsl:for-each select="child::*">
		<p><xsl:value-of select="self::*"/></p>
	</xsl:for-each>
</xsl:for-each>
-->