<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:ns0="urn:hl7-org:v2xml" exclude-result-prefixes="xs fn ns0" xmlns:saxon="http://saxon.sf.net/" xmlns:hthsurvey="HTHSurvey">
	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
		<xsl:variable name="var1_MDM_T02" as="node()?" select="ns0:MDM_T02"/>
		<ClinicalData xmlns="Clinicaldata" xmlns:basedatatypes="Basedatatypes" xmlns:common="Common" xmlns:surveys="Surveys">

			<xsl:variable name="fullSurvey" select="saxon:parse-survey($var1_MDM_T02/ns0:OBX/ns0:OBX.5)"/>
			<xsl:variable name="survey" select="$fullSurvey/hthsurvey:hthSurvey"/>
								
			<xsl:choose>
				<xsl:when test="$var1_MDM_T02/ns0:MSH/ns0:MSH.5/ns0:HD.1 = 'HDR HTADL'">
					<templateId xmlns="">
						<xsl:sequence select="'ADLSurveyCreate40021'"/>
					</templateId>
					
					<patient xmlns="">
						<adlSurveyResponse>
				 
						<xsl:for-each select="$var1_MDM_T02/ns0:PID/ns0:PID.3[ns0:CX.5 = 'PI']">
							<xsl:call-template name="getPatientLite"/>
						</xsl:for-each>
				
						<xsl:for-each select="$survey/adlSurveyResponse">
					 		<xsl:call-template name="getSurveyCommon"/> 
					 		<xsl:for-each select="$var1_MDM_T02//ns0:MSH">
								<xsl:call-template name="getRecordInformation"/>
							</xsl:for-each>
							<xsl:call-template name="getSurveyType"/>
							<xsl:call-template name="getSurveyAdlSurveyQuestionsAndAnswers"/>
							<xsl:call-template name="getSurveyScores"/>
						</xsl:for-each>
					
						</adlSurveyResponse>
					</patient>
			</xsl:when>
			<xsl:otherwise>
							
					<templateId xmlns="">
						<xsl:choose>
							<xsl:when test="$var1_MDM_T02/ns0:TXA/ns0:TXA.13/ns0:EI.1 = '1.0'">
								<xsl:sequence select="'PSSurveyCreate40021'"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:sequence select="'PSSurveyCreate40022'"/>
							</xsl:otherwise>
						</xsl:choose>
					</templateId>
					
					<patient xmlns="">
						<psSurveyResponse>
				 	
						<xsl:for-each select="$var1_MDM_T02/ns0:PID/ns0:PID.3[ns0:CX.5 = 'PI']">
							<xsl:call-template name="getPatientLite"/>
						</xsl:for-each>
				
						<xsl:for-each select="$survey/psSurveyResponse">
					 		<xsl:call-template name="getSurveyCommon"/> 
					 		<xsl:for-each select="$var1_MDM_T02//ns0:MSH">
								<xsl:call-template name="getRecordInformation"/>
							</xsl:for-each>
							<xsl:call-template name="getSurveyType"/>
							<xsl:call-template name="getSurveyPsSurveyQuestionsAndAnswers"/>
						</xsl:for-each>
					
						</psSurveyResponse>
					</patient>
				</xsl:otherwise>
			</xsl:choose>

		</ClinicalData>
	</xsl:template>
	
	<xsl:template name="getSurveyCommon">
		<surveyPatient>
			<icn>
				<xsl:sequence select="fn:string(patient/icn)"/>
			</icn>
			<ssn>
				<xsl:sequence select="fn:string(patient/ssn)"/>
			</ssn>
			<name>
				<xsl:for-each select="patient/name/family">
					<family>
						<xsl:sequence select="fn:string(.)"/>
					</family>
				</xsl:for-each>
				<xsl:for-each select="patient/name/middle">
					<middle>
						<xsl:sequence select="fn:string(.)"/>
					</middle>
				</xsl:for-each>
				<xsl:for-each select="patient/name/given">
					<given>
						<xsl:sequence select="fn:string(.)"/>
					</given>
				</xsl:for-each>
			</name>
			<birthdate>
				<xsl:sequence select="xs:string(xs:date(fn:string(patient/birthdate)))"/>
			</birthdate>
		</surveyPatient>
		<xsl:for-each select="enrollmentDate">
			<enrollmentDate>
				<xsl:sequence select="xs:string(xs:date(fn:string(.)))"/>
			</enrollmentDate>
		</xsl:for-each>
		<xsl:if test="(disenrollmentDate != '')">
			<xsl:for-each select="disenrollmentDate">
				<disenrollmentDate>
					<xsl:sequence select="xs:string(xs:date(fn:string(.)))"/>
				</disenrollmentDate>
			</xsl:for-each>
		</xsl:if>
		<xsl:if test="(activationDate != '')">
			<xsl:for-each select="activationDate">
				<activationDate>
					<xsl:sequence select="xs:string(xs:date(fn:string(.)))"/>
				</activationDate>
			</xsl:for-each>
		</xsl:if>
		<xsl:if test="(inactivationDate != '')">
			<xsl:for-each select="inactivationDate">
				<inactivationDate>
					<xsl:sequence select="xs:string(xs:date(fn:string(.)))"/>
				</inactivationDate>
			</xsl:for-each>
		</xsl:if>
		<facility>
			<facilityNumber>
				<xsl:sequence select="fn:string(facilityNumber)"/>
			</facilityNumber>
		</facility>
		<vendor>
			<vendorNumber>
				<xsl:sequence select="fn:string(vendorNumber)"/>
			</vendorNumber>
			<vendorName>
				<xsl:sequence select="fn:string(vendorName)"/>
			</vendorName>
		</vendor>
		<vendorMRN>
			<xsl:sequence select="fn:string(vendorMRN)"/>
		</vendorMRN>
		<careCoordinator>
			<xsl:for-each select="careCoordinator">
				<id>
					<xsl:sequence select="fn:string(id)"/>
				</id>
			</xsl:for-each>
			<name>
				<xsl:for-each select="careCoordinator/name/family">
					<family>
						<xsl:sequence select="fn:string(.)"/>
					</family>
				</xsl:for-each>
				<xsl:for-each select="careCoordinator/name/middle">
					<middle>
						<xsl:sequence select="fn:string(.)"/>
					</middle>
				</xsl:for-each>
				<xsl:for-each select="careCoordinator/name/given">
					<given>
						<xsl:sequence select="fn:string(.)"/>
					</given>
				</xsl:for-each>
			</name>
		</careCoordinator>
		<xsl:if test="(scheduledDate != '')">
			<xsl:for-each select="scheduledDate">
				<scheduledDate>
					<xsl:sequence select="xs:string(xs:date(fn:string(.)))"/>
				</scheduledDate>
			</xsl:for-each>
		</xsl:if>
			<completedDate>
				<xsl:sequence select="xs:string(xs:dateTime(fn:string(completedDate)))"/>
			</completedDate>
		<xsl:for-each select="currentDialogueName">
			<currentDialogueName>
				<xsl:sequence select="fn:string(.)"/>
			</currentDialogueName>
		</xsl:for-each>
		<xsl:for-each select="programName">
			<programName>
				<xsl:sequence select="fn:string(.)"/>
			</programName>
		</xsl:for-each>
		<xsl:for-each select="deviceName">
			<deviceName>
				<xsl:sequence select="fn:string(.)"/>
			</deviceName>
		</xsl:for-each>
		<xsl:for-each select="surveyAcceptedStatus">
			<surveyAcceptedStatus>
				<xsl:sequence select="fn:string(.)"/>
			</surveyAcceptedStatus>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template name="getSurveyType">
		<survey>
			<surveyTitle>
				<xsl:sequence select="fn:string(surveyTitle)"/>
			</surveyTitle>
			<surveyVersion>
				<xsl:sequence select="fn:string(surveyVersion)"/>
			</surveyVersion>
		</survey>
	</xsl:template>
	
	<xsl:template name="getSurveyPsSurveyQuestionsAndAnswers">
		<psSurveyQuestionsAndAnswers>
			<xsl:for-each select="surveyQuestionsAndAnswers/surveyQuestionAndAnswer">
				<psSurveyQuestionAndAnswer>
					<questionNumber>
						<xsl:sequence select="xs:string(xs:integer(fn:string(questionNumber)))"/>
					</questionNumber>
					<answer>
						<xsl:sequence select="xs:string(xs:integer(fn:string(answer)))"/>
					</answer>
				</psSurveyQuestionAndAnswer>
			</xsl:for-each>
		</psSurveyQuestionsAndAnswers>
	</xsl:template>
	
	<xsl:template name="getSurveyAdlSurveyQuestionsAndAnswers">
		<adlSurveyQuestionsAndAnswers>
			<xsl:for-each select="surveyQuestionsAndAnswers/surveyQuestionAndAnswer">
				<adlSurveyQuestionAndAnswer>
					<questionNumber>
						<xsl:sequence select="xs:string(xs:integer(fn:string(questionNumber)))"/>
					</questionNumber>
					<answer>
						<xsl:sequence select="xs:string(xs:integer(fn:string(answer)))"/>
					</answer>
				</adlSurveyQuestionAndAnswer>
			</xsl:for-each>
		</adlSurveyQuestionsAndAnswers>
	</xsl:template>
	
	<xsl:template name="getSurveyScores">
		<surveyScores>
			<xsl:for-each select="surveyScores/mentalScore">
				<mentalScore>
					<xsl:sequence select="xs:string(xs:decimal(fn:string(.)))"/>
				</mentalScore>
			</xsl:for-each>
			<xsl:for-each select="surveyScores/physicalScore">
				<physicalScore>
					<xsl:sequence select="xs:string(xs:decimal(fn:string(.)))"/>
				</physicalScore>
			</xsl:for-each>
		</surveyScores>
	</xsl:template>
	
	<!--
		##################################################################################################################
		############################################### COMMON TEMPLATES ###############################################

		getRecordIdentifier template
		applies to any path ending in EI and transforming EI.1 and EI.2 into identity and namespaceId respectively

	-->
	<xsl:template name="getRecordIdentifier">
		<xsl:if test="((ns0:EI.1 != '') and (ns0:EI.2 != ''))">
			<recordIdentifier>
				<identity>
					<xsl:value-of select="ns0:EI.1"/>
				</identity>
				<namespaceId>
					<xsl:value-of select="ns0:EI.2"/>
				</namespaceId>
			</recordIdentifier>
		</xsl:if>
	</xsl:template>
	
	<!--
		##################################################################################################################

		getPatientLite template
		applies to PID/ns0:PID.3[ns0:CX.5 = 'PI'] path

	-->
	<xsl:template name="getPatientLite">
		<xsl:if test="((ns0:CX.1 != '') and (ns0:CX.6/ns0:HD.2 != '') and (ns0:CX.4/ns0:HD.1 != ''))">
			<patient>
				<identifier>
					<identity>
						<xsl:value-of select="ns0:CX.1"/>
					</identity>
					<assigningFacility>
						<xsl:value-of select="ns0:CX.6/ns0:HD.2"/>
					</assigningFacility>
					<assigningAuthority>
						<xsl:value-of select="ns0:CX.4/ns0:HD.1"/>
					</assigningAuthority>
				</identifier>
				<!-- get  'L' (legal) name of this patient when possible otherwise take the first occurrence-->
				<xsl:choose>
					<xsl:when test="../ns0:PID.5[ns0:XPN.7 = 'L']">
						<name>
							<prefix>
								<xsl:value-of select="../ns0:PID.5[ns0:XPN.7 = 'L']/ns0:XPN.5"/>
							</prefix>
							<given>
								<xsl:value-of select="../ns0:PID.5[ns0:XPN.7 = 'L']/ns0:XPN.2"/>
							</given>
							<middle>
								<xsl:value-of select="../ns0:PID.5[ns0:XPN.7 = 'L']/ns0:XPN.3"/>
							</middle>
							<family>
								<xsl:value-of select="../ns0:PID.5[ns0:XPN.7 = 'L']/ns0:XPN.1/ns0:FN.1"/>
							</family>
							<suffix>
								<xsl:value-of select="../ns0:PID.5[ns0:XPN.7 = 'L']/ns0:XPN.4"/>
							</suffix>
							<title>
								<xsl:value-of select="../ns0:PID.5[ns0:XPN.7 = 'L']/ns0:XPN.6"/>
							</title>
						</name>
					</xsl:when>
					<xsl:otherwise>
						<xsl:if test="../ns0:PID.5[1]/ns0:XPN.1/ns0:FN.1">
							<name>
								<prefix>
									<xsl:value-of select="../ns0:PID.5[1]/ns0:XPN.5"/>
								</prefix>
								<given>
									<xsl:value-of select="../ns0:PID.5[1]/ns0:XPN.2"/>
								</given>
								<middle>
									<xsl:value-of select="../ns0:PID.5[1]/ns0:XPN.3"/>
								</middle>
								<family>
									<xsl:value-of select="../ns0:PID.5[1]/ns0:XPN.1/ns0:FN.1"/>
								</family>
								<suffix>
									<xsl:value-of select="../ns0:PID.5[1]/ns0:XPN.4"/>
								</suffix>
								<title>
									<xsl:value-of select="../ns0:PID.5[1]/ns0:XPN.6"/>
								</title>
							</name>
						</xsl:if>
					</xsl:otherwise>
				</xsl:choose>
			</patient>
		</xsl:if>
	</xsl:template>
	
	<!--
		##################################################################################################################

		getRecordInformation template
		applies to MSH path

	-->
	<xsl:template name="getRecordInformation">
		<xsl:if test="(ns0:MSH.4/ns0:HD.1 != '')">
			<recordSource>
				<namespaceId>
					<xsl:value-of select="ns0:MSH.4/ns0:HD.1"/>
				</namespaceId>
				<universalId>
					<xsl:value-of select="ns0:MSH.4/ns0:HD.2"/>
				</universalId>
				<universalIdType>
					<xsl:value-of select="ns0:MSH.4/ns0:HD.3"/>
				</universalIdType>
			</recordSource>
		</xsl:if>
		<xsl:if test="(ns0:MSH.7/ns0:TS.1 != '')">
			<recordUpdateTime>
				<literal>
					<xsl:value-of select="ns0:MSH.7/ns0:TS.1"/>
				</literal>
			</recordUpdateTime>
		</xsl:if>
	</xsl:template>
	
</xsl:stylesheet>
