<?xml version="1.0" encoding="UTF-8"?>
<!--

Stylesheet for text formatting templates
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:urn="urn:hl7-org:v3" xmlns:ext="urn:hl7-EE-DL-Ext:v1">
	<!--

	Template for formatting date
	-->
	<xsl:template name="dateFormat">
		<xsl:param name="date"/>
		<xsl:param name="format"/>
		<xsl:if test="$date">
			<xsl:value-of select="substring($date,7,2)"/>.<xsl:value-of select="substring($date,5,2)"/>.<xsl:value-of select="substring($date,1,4)"/>
			<xsl:if test="($format='time' or $format='millis') and string-length($date) >= 14">
				<xsl:text> </xsl:text>
				<xsl:value-of select="substring($date,9,2)"/>:<xsl:value-of select="substring($date,11,2)"/>:<xsl:value-of select="substring($date,13,2)"/>
			</xsl:if>
			<xsl:if test="$format='millis' and string-length($date) >14">
				<xsl:value-of select="substring($date,15)"/>
			</xsl:if>
			<xsl:if test="$format='break'">
				<br/>
				<xsl:value-of select="substring($date,9,2)"/>:<xsl:value-of select="substring($date,11,2)"/>:<xsl:value-of select="substring($date,13,2)"/>
			</xsl:if>
		</xsl:if>
	</xsl:template>
	<!--

	Template for mapping date units to locale
	-->
	<xsl:template name="dateUnit">
		<xsl:param name="unit"/>
		<xsl:param name="mode"/>
		<xsl:choose>
			<xsl:when test="$mode='per'">
				<xsl:choose>
					<xsl:when test="$unit='a'">aastas</xsl:when>
					<xsl:when test="$unit='y'">aastas</xsl:when>
					<xsl:when test="$unit='k'">kuus</xsl:when>
					<xsl:when test="$unit='mo'">kuus</xsl:when>
					<xsl:when test="$unit='p'">päevas</xsl:when>
					<xsl:when test="$unit='d'">päevas</xsl:when>
					<xsl:when test="$unit='n'">nädalas</xsl:when>
					<xsl:when test="$unit='wk'">nädalas</xsl:when>
					<xsl:when test="$unit!='a' and $unit!='k' and $unit!='p' and $unit!='y' and $unit!='mo' and $unit!='d' and $unit!='wk'"><xsl:value-of select="$unit"/></xsl:when>
				</xsl:choose>
			</xsl:when>	
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="$unit='a'">aastat</xsl:when>
					<xsl:when test="$unit='y'">aastat</xsl:when>
					<xsl:when test="$unit='k'">kuud</xsl:when>
					<xsl:when test="$unit='mo'">kuud</xsl:when>
					<xsl:when test="$unit='p'">päeva</xsl:when>
					<xsl:when test="$unit='d'">päeva</xsl:when>
					<xsl:when test="$unit='n'">nädalat</xsl:when>
					<xsl:when test="$unit='wk'">nädalat</xsl:when>
					<xsl:when test="$unit!='a' and $unit!='k' and $unit!='p' and $unit!='n' and $unit!='y' and $unit!='mo' and $unit!='d' and $unit!='wk'"><xsl:value-of select="$unit"/></xsl:when>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!--

	Template for person name
	-->
	<xsl:template name="name" match="urn:name">
		<xsl:choose>
			<xsl:when test="urn:given or urn:family">
				<xsl:value-of select="urn:given"/>
					<xsl:if test="urn:given/text()">
						<xsl:text> </xsl:text>
					</xsl:if>
				<xsl:value-of select="urn:family"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

<!--

	Template for extPerson name
	-->
	<xsl:template name="extName" match="ext:name">
		<xsl:choose>
			<xsl:when test="ext:given or ext:family">
				<xsl:value-of select="ext:given"/>
					<xsl:if test="ext:given/text()">
						<xsl:text> </xsl:text>
					</xsl:if>
				<xsl:value-of select="ext:family"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!--

	Template for displaying urn:code - @displayName for urn:value
	-->
	<xsl:template name="value.formatting" match="urn:value" mode="formatting">
		<xsl:value-of select="@code"/>
		<xsl:text> - </xsl:text>
		<xsl:value-of select="@displayName"/>
	</xsl:template>

	<!--Template for displaying ext:code - @displayName for ext:value
	-->
	<xsl:template name="extvalue.formatting" match="ext:value" mode="formatting">
		<xsl:value-of select="@code"/>
		<xsl:text> - </xsl:text>
		<xsl:value-of select="@displayName"/>
	</xsl:template>


	<!--

	Template for displaying urn:extension - @assigningAuthorityName for urn:templateId
	-->
	<xsl:template name="templateId.formatting" match="urn:templateId" mode="formatting">
		<xsl:value-of select="@extension"/>
		<xsl:text> - </xsl:text>
		<xsl:value-of select="@assigningAuthorityName"/>
	</xsl:template>
	<!--

	Template for displaying urn:code - @displayName pairs for urn:code
	optionally codeSystemName will be shown if param $showCodeSystemName is true
	-->
	<xsl:template name="code.formatting" match="urn:code" mode="formatting">
		<xsl:param name="showCodeSystemName"/>
		<xsl:if test="$showCodeSystemName">
			<xsl:value-of select="@codeSystemName"/>
			<xsl:text> </xsl:text>
		</xsl:if>
		<xsl:value-of select="@code"/>
		<xsl:if test="@code">
			<xsl:text> - </xsl:text>
		</xsl:if>
		<xsl:value-of select="@displayName"/>
	</xsl:template>
	
	<!--
	Template for displaying urn:translation 
	optionally codeSystemName will be shown if param $showCodeSystemName is true
	-->
	<xsl:template name="translation.formatting" match="urn:translation" mode="formatting">
		<xsl:param name="showCodeSystemName"/>
		<xsl:value-of select="@code"/>
		<xsl:if test="@code">
			<xsl:text> - </xsl:text>
		</xsl:if>
		<xsl:value-of select="@displayName"/>
		<xsl:if test="$showCodeSystemName">
			<xsl:text> (</xsl:text>
			<xsl:value-of select="@codeSystemName"/>
			<xsl:text>)</xsl:text>
		</xsl:if>
	</xsl:template>


	<!--
	Template for displaying urn:value as diagnose 
	-->
	<xsl:template name="diagnose.formatting" match="urn:value" mode="diagnoses">
		<xsl:value-of select="@code"/>
		<xsl:text> - </xsl:text>
		<xsl:value-of select="@displayName"/>
		<br/>
		<xsl:value-of select="urn:originalText"/>
	</xsl:template>


<!--
	Template for address info
	-->
	<xsl:template name="addr" match="urn:addr">
		<xsl:if test="urn:streetAddressLine/text()">
			<xsl:value-of select="urn:streetAddressLine"/>
			<xsl:if test="urn:city or urn:county or urn:postalCode or urn:direction or urn:country">
				<xsl:text>, </xsl:text>
			</xsl:if>	
		</xsl:if>
		<xsl:if test="urn:streetName/text()">
			<xsl:value-of select="urn:streetName"/>
			<xsl:if test="urn:houseNumber">
			<xsl:text> </xsl:text>
			</xsl:if>
			<xsl:value-of select="urn:houseNumber"/>	
			<xsl:choose>
				<xsl:when test="urn:additionalLocator">
					<xsl:text>-</xsl:text>
				</xsl:when>
				<xsl:otherwise>				
					<xsl:text>, </xsl:text>
				</xsl:otherwise>
			</xsl:choose>	
		</xsl:if>
		<xsl:if test="urn:additionalLocator/text()">
			<xsl:value-of select="urn:additionalLocator"/>
			<xsl:if test="urn:city or urn:county or urn:postalCode or urn:direction or urn:country">
				<xsl:text>, </xsl:text>
			</xsl:if>	
		</xsl:if>
		<xsl:if test="urn:city/text()">
			<xsl:value-of select="urn:city"/>
			<xsl:if test="urn:county or urn:postalCode or urn:direction or urn:country">
				<xsl:text>, </xsl:text>
			</xsl:if>
		</xsl:if>
		<xsl:if test="urn:county">
			<xsl:value-of select="urn:county"/>
				<xsl:if test="urn:postalCode or urn:direction or urn:country">
					<xsl:text>, </xsl:text>
				</xsl:if>	
		</xsl:if>
			<xsl:if test="urn:postalCode/text()">
		<xsl:value-of select="urn:postalCode"/>
			<xsl:if test="urn:direction or urn:country">
				<xsl:text>, </xsl:text>
			</xsl:if>
		</xsl:if>
		<xsl:if test="urn:direction/text()">
			<xsl:text>  </xsl:text>
			<span class="rowTitle">GPS koordinaadid:</span>
			<xsl:text>  </xsl:text>
			<xsl:value-of select="urn:direction/text()"/>
				<xsl:if test="urn:country">
					<xsl:text>, </xsl:text>
				</xsl:if>
		</xsl:if>
		<xsl:if test="urn:country">
			<xsl:choose>
				<xsl:when test="urn:country='EST'">
					<xsl:text>Eesti</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="urn:country"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>
	
	<!--
		Template for extension address info
	-->
	<xsl:template name="extaddr" match="ext:addr">
		<xsl:if test="ext:streetAddressLine/text()">
			<xsl:value-of select="ext:streetAddressLine"/>
		</xsl:if>
		<xsl:if test="ext:streetName/text()">
			<xsl:value-of select="ext:streetName"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="ext:houseNumber"/>
		</xsl:if>
		<xsl:if test="ext:additionalLocator/text()">
			<xsl:text>, </xsl:text>
			<xsl:value-of select="ext:additionalLocator"/>
		</xsl:if>
		<xsl:if test="ext:city/text()">
			<xsl:text>, </xsl:text>
			<xsl:value-of select="ext:city"/>
		</xsl:if>
		<xsl:if test="ext:county">
			<xsl:text>, </xsl:text>
			<xsl:value-of select="ext:county"/>
		</xsl:if>
		<xsl:if test="ext:postalCode/text()">
			<xsl:text>, </xsl:text>
			<xsl:value-of select="ext:postalCode"/>
		</xsl:if>
		<xsl:if test="ext:direction/text()">
			<xsl:text>,  </xsl:text>
			<span class="rowTitle">GPS koordinaadid:</span>
			<xsl:text>  </xsl:text>
			<xsl:value-of select="ext:direction/text()"/>
		</xsl:if>
		<xsl:if test="ext:country">
			<xsl:text>, </xsl:text>
			<xsl:choose>
				<xsl:when test="ext:country='EST'">
					<xsl:text>Eesti</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="ext:country"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

