<commit_before><?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright SemanticBits, Northwestern University and Akaza Research Distributed 
	under the OSI-approved BSD 3-Clause License. See http://ncip.github.com/caaers/LICENSE.txt 
	for details. -->
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
	xmlns:cct="http://schema.integration.caaers.cabig.nci.nih.gov/common"
	xmlns:ae="http://schema.integration.caaers.cabig.nci.nih.gov/aereport">
	<xsl:output method="xml" encoding="UTF-8" indent="yes" />

	<xsl:variable name="map" select="document('lookup.xml')" />
	<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
	<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

	<xsl:template name="lookup">
		<xsl:param name="_map" />
		<xsl:param name="_code" />
		<xsl:value-of select="$_map//code[text() = $_code]/parent::node()/value" />
	</xsl:template>

	<xsl:template match="/">
		<xsl:call-template name="safetyreport" />
	</xsl:template>


	<xsl:template match="safetyreport" name="safetyreport">
		<soapenv:Envelope>
			<soapenv:Body>
				<ae:submitSafetyReport>
					<ae:AdverseEventReport>
						<ae:createdAt>
							<xsl:call-template name="dateTimeConverter">
								<xsl:with-param name="date"
									select="/ichicsr/ichicsrmessageheader/messagedate" />
							</xsl:call-template>
						</ae:createdAt>
						<xsl:call-template name="responseDescription" />
						<ae:adverseEventReportingPeriod>
							<xsl:if test="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/ drugindication">
								<ae:treatmentAssignmentDescription>
									<xsl:value-of select="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugindication" />
								</ae:treatmentAssignmentDescription>
							</xsl:if>
							<xsl:if test="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugstartperiod">
								<ae:cycleNumber>
									<xsl:value-of select="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugstartperiod" />
								</ae:cycleNumber>
							</xsl:if>
							<xsl:if test="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugenddate">
								<ae:startDate>
									<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
										<xsl:with-param name="date" select="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugenddate" />
									</xsl:call-template>
								</ae:startDate>								
							</xsl:if>
							<ae:studyParticipantAssignmentRef>
								<ae:studySiteRef>
									<ae:studyRef>
										<ae:identifiers>
											<ae:identifier>
												<ae:type>Protocol Authority Identifier</ae:type>
												<ae:value>
													<xsl:value-of
														select="/ichicsr/safetyreport/primarysource/sponsorstudynumb" />
												</ae:value>
											</ae:identifier>
										</ae:identifiers>
									</ae:studyRef>
									<ae:nciInstituteCode>
										<xsl:value-of select="/ichicsr/safetyreport/patient/subjectstudysiteid" />
									</ae:nciInstituteCode>
								</ae:studySiteRef>
								<ae:studySubjectIdentifier>
									<xsl:value-of
										select="/ichicsr/safetyreport/patient/patientinvestigationnumb" />
								</ae:studySubjectIdentifier>
							</ae:studyParticipantAssignmentRef>
							<ae:treatmentAssignment>
								<ae:code>
									<xsl:value-of select="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/medicinalproduct" />
								</ae:code>
							</ae:treatmentAssignment>
						</ae:adverseEventReportingPeriod>	
						
						
    					<xsl:if test="/ichicsr/safetyreport/physiciangivename">
							<ae:physician>
								<ae:firstName>
									<xsl:value-of select="/ichicsr/safetyreport/physiciangivename" />
								</ae:firstName>
								<ae:lastName>
									<xsl:value-of select="/ichicsr/safetyreport/physicianfamilyname" />
								</ae:lastName>
								<xsl:if test="/ichicsr/safetyreport/physicianmiddlename">
									<ae:middleName>
										<xsl:value-of select="/ichicsr/safetyreport/physicianmiddlename" />
									</ae:middleName>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/physicianemail">
									<ae:contactMechanism>
										<ae:type>e-mail</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/physicianemail" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/physicianphone">
									<ae:contactMechanism>
										<ae:type>phone</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/physicianphone" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/physicianfax">
									<ae:contactMechanism>
										<ae:type>fax</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/physicianfax" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
							</ae:physician>
						</xsl:if>

						
						<xsl:if test="/ichicsr/safetyreport/primarysource/reportergivename">
							<ae:reporter>
								<ae:firstName>
									<xsl:value-of select="/ichicsr/safetyreport/primarysource/reportergivename" />
								</ae:firstName>
								<ae:lastName>
									<xsl:value-of select="/ichicsr/safetyreport/primarysource/reporterfamilyname" />
								</ae:lastName>
								<xsl:if test="/ichicsr/safetyreport/primarysource/reportermiddlename">
									<ae:middleName>
										<xsl:value-of select="/ichicsr/safetyreport/primarysource/reportermiddlename" />
									</ae:middleName>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/reporteremail">
									<ae:contactMechanism>
										<ae:type>e-mail</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/reporteremail" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/reporterphone">
									<ae:contactMechanism>
										<ae:type>phone</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/reporterphone" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/reporterfax">
									<ae:contactMechanism>
										<ae:type>fax</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/reporterfax" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
							</ae:reporter>
						</xsl:if>	
						
						
						<xsl:if test="/ichicsr/safetyreport/patient/summary/senderfirstname">
							<xsl:call-template name="submitter" />
						</xsl:if>
						<!--Optional: -->
						<ae:diseaseHistory>
							<!--Optional: -->
							<xsl:if
								test="/ichicsr/safetyreport/patient/medicalhistoryepisode[patientmedicalcomment = 'Study Disease']/patientmedicalstartdate != ''">
								<ae:diagnosisDate>
									<xsl:call-template name="splitDateYYYYMMDD">
										<xsl:with-param name="date"
											select="/ichicsr/safetyreport/patient/medicalhistoryepisode[patientmedicalcomment = 'Study Disease']/patientmedicalstartdate" />
									</xsl:call-template>
								</ae:diagnosisDate>
							</xsl:if>
							<!--Zero or more repetitions: -->
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Study Disease']">
									<xsl:call-template name="studyDiseaseTemplate" />
							</xsl:for-each>
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Other Study Disease']">
								<xsl:call-template name="otherStudyDiseaseTemplate" />
							</xsl:for-each>
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Disease Site']">
								<xsl:call-template name="diseaseSiteTemplate" />
							</xsl:for-each>
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Other Disease Site']">
								<xsl:call-template name="otherDiseaseSiteTemplate" />
							</xsl:for-each>
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Other Metastatic Site']">
								<xsl:call-template name="otherMetastaticSiteTemplate" />
							</xsl:for-each>
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Metastatic Site']">
								<xsl:call-template name="metastaticSiteTemplate" />
							</xsl:for-each>
						</ae:diseaseHistory>
						<xsl:call-template name="participantHistory"/>
						<!--Zero or more repetitions: -->
						<xsl:for-each select="//drug[drugadditional = 'Radiation']">
							<xsl:call-template name="radiationIntervention" />
						</xsl:for-each>
						<!--Zero or more repetitions: -->
						<xsl:for-each select="//drug[drugadditional = 'Surgery']">
							<xsl:call-template name="surgeryIntervention" />
						</xsl:for-each>
						<!--Zero or more repetitions: -->
						<xsl:for-each select="//drug[drugadditional = 'Device']">
							<xsl:call-template name="medicalDevice" />
						</xsl:for-each>
						<!--Zero or more repetitions: -->

						<xsl:for-each
							select="//drug[drugcharacterization = '2']">
							<xsl:call-template name="concomitantMedication" />
						</xsl:for-each>
						<!--1 or more repetitions: -->
						<xsl:for-each select="//reaction[not(attribution)]">
							<xsl:if test="aeexternalid">
								<xsl:call-template name="adverseEvent" />
							</xsl:if>
						</xsl:for-each>

						<xsl:for-each select="//test">
							<xsl:call-template name="lab" />
						</xsl:for-each>

						<!--Zero or more repetitions: -->

						<xsl:for-each
							select="//medicalhistoryepisode[patientmedicalcomment = 'Prior Therapy']">
							<xsl:call-template name="priorTherapy" />
						</xsl:for-each>
						<ae:treatmentInformation>
							<xsl:if test="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugstartdate">
								<ae:firstCourseDate>
									<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
										<xsl:with-param name="date" select="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugstartdate" />
									</xsl:call-template>
								</ae:firstCourseDate>
							</xsl:if>
							<xsl:if test="/ichicsr/safetyreport/patient/summary/indadminflag">
								<ae:investigationalAgentAdministered>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/patient/summary/indadminflag" />
									</xsl:call-template>
								</ae:investigationalAgentAdministered>
							</xsl:if>
							<xsl:for-each
								select="//drug[drugadditional != 'Radiation' and  drugadditional != 'Surgery' and drugadditional != 'Device' and drugcharacterization = '1']">
								<xsl:call-template name="courseAgent" />
							</xsl:for-each>
						</ae:treatmentInformation>

						<!--Zero or more repetitions: -->
						<xsl:for-each
							select="//medicalhistoryepisode[patientmedicalcomment = 'Pre-existing Condition' or patientmedicalcomment = 'Other Pre-existing Condition']">
							<xsl:call-template name="preexistingCondition" />
						</xsl:for-each>
						<!--Zero or more repetitions: -->
						<xsl:for-each
							select="//medicalhistoryepisode[patientmedicalcomment = 'Other Cause']">
							<xsl:call-template name="otherCause" />
						</xsl:for-each>
						
						<ae:additionalInformation>
						   <xsl:if test="/ichicsr/safetyreport/autopsyreportflag">
							   <ae:autopsyReport>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/autopsyreportflag" />
									</xsl:call-template>
							   </ae:autopsyReport>
							</xsl:if>
						  <xsl:if test="/ichicsr/safetyreport/consultsflag">
							   <ae:consults>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/consultsflag" />
									</xsl:call-template>
							   </ae:consults>
							</xsl:if>
						   <xsl:if test="/ichicsr/safetyreport/dischargesummaryflag">
							   <ae:dischargeSummary>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/dischargesummaryflag" />
									</xsl:call-template>
							   </ae:dischargeSummary>
							</xsl:if>
						   <xsl:if test="/ichicsr/safetyreport/flowsheetscrfsflag">
							   <ae:flowCharts>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/flowsheetscrfsflag" />
									</xsl:call-template>
							   </ae:flowCharts>
							</xsl:if>
						   <xsl:if test="/ichicsr/safetyreport/labreportsflag">
							   <ae:labReports>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/labreportsflag" />
									</xsl:call-template>
							   </ae:labReports>
							</xsl:if>
						   <xsl:if test="/ichicsr/safetyreport/obaformsflag">
							   <ae:obaForm>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/obaformsflag" />
									</xsl:call-template>
							   </ae:obaForm>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/otheradditionalflag">
							   <ae:other>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/otheradditionalflag" />
									</xsl:call-template>
							   </ae:other>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/pathologyreportflag">
							   <ae:pathologyReport>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/pathologyreportflag" />
									</xsl:call-template>
							   </ae:pathologyReport>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/progressnotesflag">
							   <ae:progressNotes>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/progressnotesflag" />
									</xsl:call-template>
							   </ae:progressNotes>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/radiologyreportflag">
							   <ae:radiologyReports>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/radiologyreportflag" />
									</xsl:call-template>
							   </ae:radiologyReports>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/referrallettersflag">
							   <ae:referralLetters>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/referrallettersflag" />
									</xsl:call-template>
							   </ae:referralLetters>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/irbsummaryreportflag">
							   <ae:irbReport>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/irbsummaryreportflag" />
									</xsl:call-template>
							   </ae:irbReport>
							</xsl:if>						  
						    <xsl:if test="/ichicsr/safetyreport/documentlist">
							   <ae:otherInformation><xsl:value-of select="/ichicsr/safetyreport/documentlist" /></ae:otherInformation>
							</xsl:if>
						</ae:additionalInformation>

						<!--1 or more repetitions: -->
						<ae:report>
							<ae:caseNumber>
								<xsl:value-of select="/ichicsr/safetyreport/safetyreportid" />
							</ae:caseNumber>
							<ae:aeReportDefinition>
								<ae:name>
									<xsl:value-of select="/ichicsr/safetyreport/patient/summary/nameofreport" />
								</ae:name>
							</ae:aeReportDefinition>
							<!--Zero or more repetitions: -->
							<ae:aeReportVersion>
								<xsl:if test="/ichicsr/safetyreport/safetyreportversion">
									<ae:reportVersionId>
										<xsl:value-of select="/ichicsr/safetyreport/safetyreportversion" />
									</ae:reportVersionId>
								</xsl:if>
								 <xsl:if test="/ichicsr/safetyreport/sender/recipientemails">
									<ae:ccEmails>
										<xsl:value-of select="/ichicsr/safetyreport/sender/recipientemails" />
									</ae:ccEmails>
								</xsl:if>
							</ae:aeReportVersion>
						</ae:report>
					</ae:AdverseEventReport>
				</ae:submitSafetyReport>
			</soapenv:Body>
		</soapenv:Envelope>
	</xsl:template>


	<xsl:template name="studyDiseaseTemplate">
		<xsl:if test="./patientmedicalcomment = 'Study Disease' ">
			<ae:primaryDisease>
				<xsl:value-of select="patientepisodename" />
			</ae:primaryDisease>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="diseaseSiteTemplate">
		<xsl:if test="./patientmedicalcomment = 'Disease Site' ">
			<ae:codedPrimaryDiseaseSite>
				<xsl:value-of select="patientepisodename" />
			</ae:codedPrimaryDiseaseSite>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="otherDiseaseSiteTemplate">
		<xsl:if test="./patientmedicalcomment = 'Other Disease Site' ">
			<ae:otherPrimaryDiseaseSite>
				<xsl:value-of select="patientepisodename" />
			</ae:otherPrimaryDiseaseSite>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="otherStudyDiseaseTemplate">
		<xsl:if test="./patientmedicalcomment = 'Other Study Disease' ">
			<ae:otherPrimaryDisease>
				<xsl:value-of select="patientepisodename" />
			</ae:otherPrimaryDisease>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="metastaticSiteTemplate">
		<xsl:if test="./patientmedicalcomment = 'Metastatic Site' and ./patientepisodename != 'Other, specify'">
			<ae:metastaticDiseaseSite>
				<ae:codedSite>
					<ae:name>
						<xsl:value-of select="patientepisodename" />
					</ae:name>
				</ae:codedSite>
			</ae:metastaticDiseaseSite>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="otherMetastaticSiteTemplate">
		<xsl:if test="./patientmedicalcomment = 'Other Metastatic Site' ">
			<ae:metastaticDiseaseSite>
				<ae:otherSite>
					<xsl:value-of select="patientepisodename" />
				</ae:otherSite>
			</ae:metastaticDiseaseSite>
		</xsl:if>
	</xsl:template>

	<xsl:template name="priorTherapy">
		<ae:sAEReportPriorTherapy>
			<ae:priorTherapy>
				<ae:text>
					<xsl:value-of select="patientepisodename" />
				</ae:text>				
			</ae:priorTherapy>
			
			<ae:other>
				<xsl:value-of select="priortherapycomment" />
			</ae:other>
			
			<xsl:if test="./patientmedicalstartdate != '' ">
				<ae:startDate>
					<xsl:call-template name="splitDateYYYYMMDD">
						<xsl:with-param name="date" select="./patientmedicalstartdate" />
					</xsl:call-template>
				</ae:startDate>
			</xsl:if>
			<xsl:if test="./patientmedicalenddate != '' ">
				<ae:endDate>
					<xsl:call-template name="splitDateYYYYMMDD">
						<xsl:with-param name="date" select="./patientmedicalenddate" />
					</xsl:call-template>
				</ae:endDate>
			</xsl:if>
			
			<xsl:for-each select="priortherapyagent">
				<ae:priorTherapyAgent>
					<ae:agent>
						<ae:nscNumber>
							<xsl:value-of select="." />
						</ae:nscNumber>
					</ae:agent>
				</ae:priorTherapyAgent>
			</xsl:for-each>
		</ae:sAEReportPriorTherapy>
	</xsl:template>

	<xsl:template name="preexistingCondition">
		<ae:sAEReportPreExistingCondition>
			<xsl:if test="./patientmedicalcomment = 'Pre-existing Condition' ">
				<ae:preExistingCondition>
					<text>
						<xsl:value-of select="patientepisodename" />
					</text>
				</ae:preExistingCondition>
			</xsl:if>
			<xsl:if test="./patientmedicalcomment = 'Other Pre-existing Condition' ">
				<ae:other>
					<xsl:value-of select="patientepisodename" />
				</ae:other>
			</xsl:if>
		</ae:sAEReportPreExistingCondition>
	</xsl:template>

	<xsl:template name="splitDateYYYYMMDD">
		<xsl:param name="date" />
		<xsl:variable name="valueLength" select="string-length($date)-1" />
		<xsl:if test="$valueLength > 6">
			<ae:day>
				<xsl:value-of select='substring($date,7,2)' />
			</ae:day>
		</xsl:if>
		<ae:month>
			<xsl:value-of select='substring($date,5,2)' />
		</ae:month>
		<ae:year>
			<xsl:value-of select='substring($date,1,4)' />
		</ae:year>
	</xsl:template>

	<xsl:template name="dateTimeConverter">
		<xsl:param name="date" />
		<xsl:variable name="valueLength" select="string-length($date)-1" />
		<xsl:variable name="vDay" select='substring($date,7,2)' />
		<xsl:variable name="vMonth" select='substring($date,5,2)' />
		<xsl:variable name="vYear" select='substring($date,1,4)' />

		<xsl:variable name="vHour" select='substring($date,9,2)' />
		<xsl:variable name="vMin" select='substring($date,11,2)' />
		<xsl:variable name="vSec" select='substring($date,13,2)' />

		<xsl:variable name="dateTime"
			select="concat($vYear,'-',$vMonth,'-',$vDay,'T',$vHour,':',$vMin,':',$vSec)" />
		<xsl:value-of select="$dateTime" />
	</xsl:template>

	<xsl:template name="concomitantMedication">
		<ae:concomitantMedication>
			<ae:name>
				<xsl:value-of select="medicinalproduct" />
			</ae:name>
			<xsl:if test="./drugstartdate != '' ">
				<ae:startDate>
					<xsl:call-template name="splitDateYYYYMMDD">
						<xsl:with-param name="date" select="./drugstartdate" />
					</xsl:call-template>
				</ae:startDate>
			</xsl:if>
			<xsl:if test="./drugenddate != '' ">
				<ae:endDate>
					<xsl:call-template name="splitDateYYYYMMDD">
						<xsl:with-param name="date" select="./drugenddate" />
					</xsl:call-template>
				</ae:endDate>
			</xsl:if>
		</ae:concomitantMedication>
	</xsl:template>
	<xsl:template name="courseAgent">
		<ae:courseAgent>
			<!--Optional: -->
			<xsl:if test="drugenddate != '' ">
				<ae:lastAdministeredDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="drugenddate" />
					</xsl:call-template>
				</ae:lastAdministeredDate>
			</xsl:if>
			<xsl:if test="drugtreatmentduration and drugtreatmentduration != '0'">
				<ae:administrationDelayAmount>
						<xsl:value-of select="drugtreatmentduration" />
				</ae:administrationDelayAmount>
			</xsl:if>
			<xsl:if test="drugtreatmentdurationunit and drugtreatmentduration != '0'">
				<ae:administrationDelayUnits>
					<xsl:call-template name="lookup">
						<xsl:with-param name="_map" select="$map//delayuoms" />
						<xsl:with-param name="_code" select='drugtreatmentdurationunit' />
					</xsl:call-template>
				</ae:administrationDelayUnits>
			</xsl:if>
			<ae:dose>
				<ae:amount>
					<xsl:value-of select="drugcumulativedosagenumb" />
				</ae:amount>
				<xsl:if test="drugcumulativedosageunitextended">
					<ae:units>
						<xsl:value-of select="drugcumulativedosageunitextended" />
					</ae:units>
				</xsl:if>
			</ae:dose>
			<ae:studyAgent>
				<!--You have a CHOICE of the next 2 items at this level -->
				<ae:Agent>
					<ae:nscNumber>
						<xsl:value-of select="medicinalproduct" />
					</ae:nscNumber>
				</ae:Agent>
			</ae:studyAgent>
			<ae:lotNumber>
				<xsl:value-of select="drugbatchnumb" />
			</ae:lotNumber>
			<ae:comments>
				<xsl:value-of select="drugadditional" />
			</ae:comments>
			<xsl:if test="actiondrug">
				<ae:agentAdjustment>
					<xsl:call-template name="lookup">
						<xsl:with-param name="_map" select="$map//agent-dose-modifications" />
						<xsl:with-param name="_code" select='actiondrug' />
					</xsl:call-template>
				</ae:agentAdjustment>
			</xsl:if>
		</ae:courseAgent>
	</xsl:template>
	<xsl:template name="medicalDevice">
		<ae:medicalDevice>
			<!--Optional: -->
			<ae:brandName>
				<xsl:value-of select="devicenamebrand" />
			</ae:brandName>
			<!--Optional: -->
			<ae:commonName>
				<xsl:value-of select="medicinalproduct" />
			</ae:commonName>
			<!--Optional: -->
			<ae:deviceType>
				<xsl:value-of select="devicetype" />
			</ae:deviceType>
			<!--Optional: -->
			<ae:manufacturerName>
				<xsl:value-of select="devicemanufacturername" />
			</ae:manufacturerName>
			<!--Optional: -->
			<ae:manufacturerCity>
				<xsl:value-of select="devicemanufacturercity" />
			</ae:manufacturerCity>
			<!--Optional: -->
			<ae:manufacturerState>
				<xsl:value-of select="devicemanufacturerstate" />
			</ae:manufacturerState>
			<!--Optional: -->
			<ae:modelNumber>
				<xsl:value-of select="devicenumbermodel" />
			</ae:modelNumber>
			<!--Optional: -->
			<ae:catalogNumber>
				<xsl:value-of select="devicenumbercatalog" />
			</ae:catalogNumber>
			<!--Optional: -->
			<xsl:if test="devicedateexpiration">
				<ae:expirationDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="devicedateexpiration" />
					</xsl:call-template>
				</ae:expirationDate>
			</xsl:if>
			<!--Optional: -->
			<ae:serialNumber>
				<xsl:value-of select="devicenumberserial" />
			</ae:serialNumber>
			<!--Optional: -->
			<ae:otherNumber>
				<xsl:value-of select="devicenumberother" />
			</ae:otherNumber>
			<xsl:if test="drugenddate">
				<ae:explantedDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="drugenddate" />
					</xsl:call-template>
				</ae:explantedDate>
			</xsl:if>
			<xsl:if test="drugstartdate">
				<ae:implantedDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="drugstartdate" />
					</xsl:call-template>
				</ae:implantedDate>
			</xsl:if>
			<!--Optional: -->
			<ae:deviceOperator>
				<xsl:value-of select="deviceoperator" />
			</ae:deviceOperator>
			<!--Optional: -->
			<xsl:if test="devicereturndate">
				<ae:returnedDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="devicereturndate" />
					</xsl:call-template>
				</ae:returnedDate>
			</xsl:if>

			<xsl:if test="devicereprocessedflag = '1'">
				<ae:deviceReprocessed>YES</ae:deviceReprocessed>
				<ae:reprocessedName>
					<xsl:value-of select="devicereprocessorname" />
				</ae:reprocessedName>
				<ae:reprocessedAddress>
					<xsl:value-of select="devicereprocessoraddress" />
				</ae:reprocessedAddress>
			</xsl:if>
			<xsl:if test="devicereprocessedflag = '2'">
				<ae:deviceReprocessed>NO</ae:deviceReprocessed>
			</xsl:if>
	        
	        <xsl:if test="deviceavailableflag">
				<ae:evaluationAvailability>
					<xsl:value-of select="deviceavailableflag" />
				</ae:evaluationAvailability>
			</xsl:if>
			
			<ae:studyDevice>
				<ae:otherBrandName>
					<xsl:value-of select="devicenamebrand" />
				</ae:otherBrandName>
				<ae:otherCommonName>
					<xsl:value-of select="medicinalproduct" />
				</ae:otherCommonName>
				<!--Optional: -->
				<ae:otherDeviceType>
					<xsl:value-of select="devicetype" />
				</ae:otherDeviceType>
			</ae:studyDevice>
			<ae:otherDeviceOperator>
				<xsl:value-of select="deviceoperatorother" />
			</ae:otherDeviceOperator>
			<ae:lotNumber>
				<xsl:value-of select="drugbatchnumb" />
			</ae:lotNumber>
		</ae:medicalDevice>
	</xsl:template>
	<xsl:template name="radiationIntervention">
		<ae:radiationIntervention>
			<ae:dosage>
				<xsl:value-of select="drugcumulativedosagenumb" />
			</ae:dosage>
			<ae:dosageUnit>
				<xsl:value-of select="radiationdoseunit" />
			</ae:dosageUnit>
			<xsl:if test="./drugenddate != '' ">
				<ae:lastTreatmentDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="drugenddate" />
					</xsl:call-template>
				</ae:lastTreatmentDate>
			</xsl:if>
			<ae:fractionNumber>
				<xsl:value-of select="drugseparatedosagenumb" />
			</ae:fractionNumber>
			<ae:daysElapsed>
				<xsl:value-of select="drugintervaldosageunitnumb" />
			</ae:daysElapsed>
			<ae:adjustment>
				<xsl:call-template name="lookup">
					<xsl:with-param name="_map" select="$map//adjustments" />
					<xsl:with-param name="_code" select='actiondrug' />
				</xsl:call-template>
			</ae:adjustment>
			<ae:administration>
				<xsl:value-of select="medicinalproduct" />
			</ae:administration>
		</ae:radiationIntervention>
	</xsl:template>
	<xsl:template name="surgeryIntervention">
		<ae:surgeryIntervention>
			<xsl:if test="drugstartdate != '' ">
				<ae:interventionDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="drugstartdate" />
					</xsl:call-template>
				</ae:interventionDate>
			</xsl:if>
			<ae:InterventionSite>
				<ae:name>
					<xsl:value-of select="medicinalproduct" />
				</ae:name>
			</ae:InterventionSite>
		</ae:surgeryIntervention>
	</xsl:template>
	<xsl:template name="lab">
		<ae:lab>
			<!--Optional: -->
			<ae:labTerm>
				<ae:term>
					<xsl:value-of select="testname" />
				</ae:term>
			</ae:labTerm>
			<!--Optional: -->
			<ae:units>
				<xsl:call-template name="lookup">
					<xsl:with-param name="_map" select="$map//uoms" />
					<xsl:with-param name="_code" select='testunit' />
				</xsl:call-template>
			</ae:units>
			<!--Optional: -->
			<xsl:if test="testtype = 'Baseline'">
				<ae:baseline>
					<ae:value>
						<xsl:value-of select="testresult" />
					</ae:value>
					<xsl:if test="./testdate != '' ">
						<ae:date>
							<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
								<xsl:with-param name="date" select="./testdate" />
							</xsl:call-template>
						</ae:date>
					</xsl:if>
				</ae:baseline>
			</xsl:if>
			<!--Optional: -->
			<xsl:if test="testtype = 'Worst'">
				<ae:nadir>
					<ae:value>
						<xsl:value-of select="testresult" />
					</ae:value>
					<xsl:if test="./testdate != '' ">
						<ae:date>
							<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
								<xsl:with-param name="date" select="./testdate" />
							</xsl:call-template>
						</ae:date>
					</xsl:if>
				</ae:nadir>
			</xsl:if>
			<!--Optional: -->
			<xsl:if test="testtype = 'Recovery'">
				<ae:recovery>
					<ae:value>
						<xsl:value-of select="testresult" />
					</ae:value>
					<xsl:if test="./testdate != '' ">
						<ae:date>
							<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
								<xsl:with-param name="date" select="./testdate" />
							</xsl:call-template>
						</ae:date>
					</xsl:if>
				</ae:recovery>
			</xsl:if>
			<xsl:if
				test="testname = 'Bacterial infection NOS' or testname = 'Fungal infection NOS' or testname = 'Viral infection NOS'">
				<ae:infectiousAgent>
					<xsl:value-of select="testresult" />
				</ae:infectiousAgent>
				<ae:site>
					<xsl:value-of select="infectionsite" /><commit_msg>CAAERS-6905 fix consuming lab category microbiology<commit_after><?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright SemanticBits, Northwestern University and Akaza Research Distributed 
	under the OSI-approved BSD 3-Clause License. See http://ncip.github.com/caaers/LICENSE.txt 
	for details. -->
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
	xmlns:cct="http://schema.integration.caaers.cabig.nci.nih.gov/common"
	xmlns:ae="http://schema.integration.caaers.cabig.nci.nih.gov/aereport">
	<xsl:output method="xml" encoding="UTF-8" indent="yes" />

	<xsl:variable name="map" select="document('lookup.xml')" />
	<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
	<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

	<xsl:template name="lookup">
		<xsl:param name="_map" />
		<xsl:param name="_code" />
		<xsl:value-of select="$_map//code[text() = $_code]/parent::node()/value" />
	</xsl:template>

	<xsl:template match="/">
		<xsl:call-template name="safetyreport" />
	</xsl:template>


	<xsl:template match="safetyreport" name="safetyreport">
		<soapenv:Envelope>
			<soapenv:Body>
				<ae:submitSafetyReport>
					<ae:AdverseEventReport>
						<ae:createdAt>
							<xsl:call-template name="dateTimeConverter">
								<xsl:with-param name="date"
									select="/ichicsr/ichicsrmessageheader/messagedate" />
							</xsl:call-template>
						</ae:createdAt>
						<xsl:call-template name="responseDescription" />
						<ae:adverseEventReportingPeriod>
							<xsl:if test="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/ drugindication">
								<ae:treatmentAssignmentDescription>
									<xsl:value-of select="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugindication" />
								</ae:treatmentAssignmentDescription>
							</xsl:if>
							<xsl:if test="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugstartperiod">
								<ae:cycleNumber>
									<xsl:value-of select="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugstartperiod" />
								</ae:cycleNumber>
							</xsl:if>
							<xsl:if test="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugenddate">
								<ae:startDate>
									<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
										<xsl:with-param name="date" select="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugenddate" />
									</xsl:call-template>
								</ae:startDate>								
							</xsl:if>
							<ae:studyParticipantAssignmentRef>
								<ae:studySiteRef>
									<ae:studyRef>
										<ae:identifiers>
											<ae:identifier>
												<ae:type>Protocol Authority Identifier</ae:type>
												<ae:value>
													<xsl:value-of
														select="/ichicsr/safetyreport/primarysource/sponsorstudynumb" />
												</ae:value>
											</ae:identifier>
										</ae:identifiers>
									</ae:studyRef>
									<ae:nciInstituteCode>
										<xsl:value-of select="/ichicsr/safetyreport/patient/subjectstudysiteid" />
									</ae:nciInstituteCode>
								</ae:studySiteRef>
								<ae:studySubjectIdentifier>
									<xsl:value-of
										select="/ichicsr/safetyreport/patient/patientinvestigationnumb" />
								</ae:studySubjectIdentifier>
							</ae:studyParticipantAssignmentRef>
							<ae:treatmentAssignment>
								<ae:code>
									<xsl:value-of select="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/medicinalproduct" />
								</ae:code>
							</ae:treatmentAssignment>
						</ae:adverseEventReportingPeriod>	
						
						
    					<xsl:if test="/ichicsr/safetyreport/physiciangivename">
							<ae:physician>
								<ae:firstName>
									<xsl:value-of select="/ichicsr/safetyreport/physiciangivename" />
								</ae:firstName>
								<ae:lastName>
									<xsl:value-of select="/ichicsr/safetyreport/physicianfamilyname" />
								</ae:lastName>
								<xsl:if test="/ichicsr/safetyreport/physicianmiddlename">
									<ae:middleName>
										<xsl:value-of select="/ichicsr/safetyreport/physicianmiddlename" />
									</ae:middleName>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/physicianemail">
									<ae:contactMechanism>
										<ae:type>e-mail</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/physicianemail" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/physicianphone">
									<ae:contactMechanism>
										<ae:type>phone</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/physicianphone" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/physicianfax">
									<ae:contactMechanism>
										<ae:type>fax</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/physicianfax" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
							</ae:physician>
						</xsl:if>

						
						<xsl:if test="/ichicsr/safetyreport/primarysource/reportergivename">
							<ae:reporter>
								<ae:firstName>
									<xsl:value-of select="/ichicsr/safetyreport/primarysource/reportergivename" />
								</ae:firstName>
								<ae:lastName>
									<xsl:value-of select="/ichicsr/safetyreport/primarysource/reporterfamilyname" />
								</ae:lastName>
								<xsl:if test="/ichicsr/safetyreport/primarysource/reportermiddlename">
									<ae:middleName>
										<xsl:value-of select="/ichicsr/safetyreport/primarysource/reportermiddlename" />
									</ae:middleName>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/reporteremail">
									<ae:contactMechanism>
										<ae:type>e-mail</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/reporteremail" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/reporterphone">
									<ae:contactMechanism>
										<ae:type>phone</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/reporterphone" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
								<xsl:if test="/ichicsr/safetyreport/reporterfax">
									<ae:contactMechanism>
										<ae:type>fax</ae:type>
										<ae:value>
											<xsl:value-of select="/ichicsr/safetyreport/reporterfax" />
										</ae:value>
									</ae:contactMechanism>
								</xsl:if>
							</ae:reporter>
						</xsl:if>	
						
						
						<xsl:if test="/ichicsr/safetyreport/patient/summary/senderfirstname">
							<xsl:call-template name="submitter" />
						</xsl:if>
						<!--Optional: -->
						<ae:diseaseHistory>
							<!--Optional: -->
							<xsl:if
								test="/ichicsr/safetyreport/patient/medicalhistoryepisode[patientmedicalcomment = 'Study Disease']/patientmedicalstartdate != ''">
								<ae:diagnosisDate>
									<xsl:call-template name="splitDateYYYYMMDD">
										<xsl:with-param name="date"
											select="/ichicsr/safetyreport/patient/medicalhistoryepisode[patientmedicalcomment = 'Study Disease']/patientmedicalstartdate" />
									</xsl:call-template>
								</ae:diagnosisDate>
							</xsl:if>
							<!--Zero or more repetitions: -->
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Study Disease']">
									<xsl:call-template name="studyDiseaseTemplate" />
							</xsl:for-each>
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Other Study Disease']">
								<xsl:call-template name="otherStudyDiseaseTemplate" />
							</xsl:for-each>
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Disease Site']">
								<xsl:call-template name="diseaseSiteTemplate" />
							</xsl:for-each>
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Other Disease Site']">
								<xsl:call-template name="otherDiseaseSiteTemplate" />
							</xsl:for-each>
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Other Metastatic Site']">
								<xsl:call-template name="otherMetastaticSiteTemplate" />
							</xsl:for-each>
							<xsl:for-each select="//medicalhistoryepisode[patientmedicalcomment = 'Metastatic Site']">
								<xsl:call-template name="metastaticSiteTemplate" />
							</xsl:for-each>
						</ae:diseaseHistory>
						<xsl:call-template name="participantHistory"/>
						<!--Zero or more repetitions: -->
						<xsl:for-each select="//drug[drugadditional = 'Radiation']">
							<xsl:call-template name="radiationIntervention" />
						</xsl:for-each>
						<!--Zero or more repetitions: -->
						<xsl:for-each select="//drug[drugadditional = 'Surgery']">
							<xsl:call-template name="surgeryIntervention" />
						</xsl:for-each>
						<!--Zero or more repetitions: -->
						<xsl:for-each select="//drug[drugadditional = 'Device']">
							<xsl:call-template name="medicalDevice" />
						</xsl:for-each>
						<!--Zero or more repetitions: -->

						<xsl:for-each
							select="//drug[drugcharacterization = '2']">
							<xsl:call-template name="concomitantMedication" />
						</xsl:for-each>
						<!--1 or more repetitions: -->
						<xsl:for-each select="//reaction[not(attribution)]">
							<xsl:if test="aeexternalid">
								<xsl:call-template name="adverseEvent" />
							</xsl:if>
						</xsl:for-each>

						<xsl:for-each select="//test">
							<xsl:call-template name="lab" />
						</xsl:for-each>

						<!--Zero or more repetitions: -->

						<xsl:for-each
							select="//medicalhistoryepisode[patientmedicalcomment = 'Prior Therapy']">
							<xsl:call-template name="priorTherapy" />
						</xsl:for-each>
						<ae:treatmentInformation>
							<xsl:if test="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugstartdate">
								<ae:firstCourseDate>
									<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
										<xsl:with-param name="date" select="/ichicsr/safetyreport/patient/drug[drugcharacterization = '3']/drugstartdate" />
									</xsl:call-template>
								</ae:firstCourseDate>
							</xsl:if>
							<xsl:if test="/ichicsr/safetyreport/patient/summary/indadminflag">
								<ae:investigationalAgentAdministered>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/patient/summary/indadminflag" />
									</xsl:call-template>
								</ae:investigationalAgentAdministered>
							</xsl:if>
							<xsl:for-each
								select="//drug[drugadditional != 'Radiation' and  drugadditional != 'Surgery' and drugadditional != 'Device' and drugcharacterization = '1']">
								<xsl:call-template name="courseAgent" />
							</xsl:for-each>
						</ae:treatmentInformation>

						<!--Zero or more repetitions: -->
						<xsl:for-each
							select="//medicalhistoryepisode[patientmedicalcomment = 'Pre-existing Condition' or patientmedicalcomment = 'Other Pre-existing Condition']">
							<xsl:call-template name="preexistingCondition" />
						</xsl:for-each>
						<!--Zero or more repetitions: -->
						<xsl:for-each
							select="//medicalhistoryepisode[patientmedicalcomment = 'Other Cause']">
							<xsl:call-template name="otherCause" />
						</xsl:for-each>
						
						<ae:additionalInformation>
						   <xsl:if test="/ichicsr/safetyreport/autopsyreportflag">
							   <ae:autopsyReport>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/autopsyreportflag" />
									</xsl:call-template>
							   </ae:autopsyReport>
							</xsl:if>
						  <xsl:if test="/ichicsr/safetyreport/consultsflag">
							   <ae:consults>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/consultsflag" />
									</xsl:call-template>
							   </ae:consults>
							</xsl:if>
						   <xsl:if test="/ichicsr/safetyreport/dischargesummaryflag">
							   <ae:dischargeSummary>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/dischargesummaryflag" />
									</xsl:call-template>
							   </ae:dischargeSummary>
							</xsl:if>
						   <xsl:if test="/ichicsr/safetyreport/flowsheetscrfsflag">
							   <ae:flowCharts>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/flowsheetscrfsflag" />
									</xsl:call-template>
							   </ae:flowCharts>
							</xsl:if>
						   <xsl:if test="/ichicsr/safetyreport/labreportsflag">
							   <ae:labReports>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/labreportsflag" />
									</xsl:call-template>
							   </ae:labReports>
							</xsl:if>
						   <xsl:if test="/ichicsr/safetyreport/obaformsflag">
							   <ae:obaForm>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/obaformsflag" />
									</xsl:call-template>
							   </ae:obaForm>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/otheradditionalflag">
							   <ae:other>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/otheradditionalflag" />
									</xsl:call-template>
							   </ae:other>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/pathologyreportflag">
							   <ae:pathologyReport>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/pathologyreportflag" />
									</xsl:call-template>
							   </ae:pathologyReport>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/progressnotesflag">
							   <ae:progressNotes>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/progressnotesflag" />
									</xsl:call-template>
							   </ae:progressNotes>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/radiologyreportflag">
							   <ae:radiologyReports>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/radiologyreportflag" />
									</xsl:call-template>
							   </ae:radiologyReports>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/referrallettersflag">
							   <ae:referralLetters>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/referrallettersflag" />
									</xsl:call-template>
							   </ae:referralLetters>
							</xsl:if>
						    <xsl:if test="/ichicsr/safetyreport/irbsummaryreportflag">
							   <ae:irbReport>
									<xsl:call-template name="convertOneTwotoBoolean">
										<xsl:with-param name="oneTwoType" select="/ichicsr/safetyreport/irbsummaryreportflag" />
									</xsl:call-template>
							   </ae:irbReport>
							</xsl:if>						  
						    <xsl:if test="/ichicsr/safetyreport/documentlist">
							   <ae:otherInformation><xsl:value-of select="/ichicsr/safetyreport/documentlist" /></ae:otherInformation>
							</xsl:if>
						</ae:additionalInformation>

						<!--1 or more repetitions: -->
						<ae:report>
							<ae:caseNumber>
								<xsl:value-of select="/ichicsr/safetyreport/safetyreportid" />
							</ae:caseNumber>
							<ae:aeReportDefinition>
								<ae:name>
									<xsl:value-of select="/ichicsr/safetyreport/patient/summary/nameofreport" />
								</ae:name>
							</ae:aeReportDefinition>
							<!--Zero or more repetitions: -->
							<ae:aeReportVersion>
								<xsl:if test="/ichicsr/safetyreport/safetyreportversion">
									<ae:reportVersionId>
										<xsl:value-of select="/ichicsr/safetyreport/safetyreportversion" />
									</ae:reportVersionId>
								</xsl:if>
								 <xsl:if test="/ichicsr/safetyreport/sender/recipientemails">
									<ae:ccEmails>
										<xsl:value-of select="/ichicsr/safetyreport/sender/recipientemails" />
									</ae:ccEmails>
								</xsl:if>
							</ae:aeReportVersion>
						</ae:report>
					</ae:AdverseEventReport>
				</ae:submitSafetyReport>
			</soapenv:Body>
		</soapenv:Envelope>
	</xsl:template>


	<xsl:template name="studyDiseaseTemplate">
		<xsl:if test="./patientmedicalcomment = 'Study Disease' ">
			<ae:primaryDisease>
				<xsl:value-of select="patientepisodename" />
			</ae:primaryDisease>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="diseaseSiteTemplate">
		<xsl:if test="./patientmedicalcomment = 'Disease Site' ">
			<ae:codedPrimaryDiseaseSite>
				<xsl:value-of select="patientepisodename" />
			</ae:codedPrimaryDiseaseSite>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="otherDiseaseSiteTemplate">
		<xsl:if test="./patientmedicalcomment = 'Other Disease Site' ">
			<ae:otherPrimaryDiseaseSite>
				<xsl:value-of select="patientepisodename" />
			</ae:otherPrimaryDiseaseSite>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="otherStudyDiseaseTemplate">
		<xsl:if test="./patientmedicalcomment = 'Other Study Disease' ">
			<ae:otherPrimaryDisease>
				<xsl:value-of select="patientepisodename" />
			</ae:otherPrimaryDisease>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="metastaticSiteTemplate">
		<xsl:if test="./patientmedicalcomment = 'Metastatic Site' and ./patientepisodename != 'Other, specify'">
			<ae:metastaticDiseaseSite>
				<ae:codedSite>
					<ae:name>
						<xsl:value-of select="patientepisodename" />
					</ae:name>
				</ae:codedSite>
			</ae:metastaticDiseaseSite>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="otherMetastaticSiteTemplate">
		<xsl:if test="./patientmedicalcomment = 'Other Metastatic Site' ">
			<ae:metastaticDiseaseSite>
				<ae:otherSite>
					<xsl:value-of select="patientepisodename" />
				</ae:otherSite>
			</ae:metastaticDiseaseSite>
		</xsl:if>
	</xsl:template>

	<xsl:template name="priorTherapy">
		<ae:sAEReportPriorTherapy>
			<ae:priorTherapy>
				<ae:text>
					<xsl:value-of select="patientepisodename" />
				</ae:text>				
			</ae:priorTherapy>
			
			<ae:other>
				<xsl:value-of select="priortherapycomment" />
			</ae:other>
			
			<xsl:if test="./patientmedicalstartdate != '' ">
				<ae:startDate>
					<xsl:call-template name="splitDateYYYYMMDD">
						<xsl:with-param name="date" select="./patientmedicalstartdate" />
					</xsl:call-template>
				</ae:startDate>
			</xsl:if>
			<xsl:if test="./patientmedicalenddate != '' ">
				<ae:endDate>
					<xsl:call-template name="splitDateYYYYMMDD">
						<xsl:with-param name="date" select="./patientmedicalenddate" />
					</xsl:call-template>
				</ae:endDate>
			</xsl:if>
			
			<xsl:for-each select="priortherapyagent">
				<ae:priorTherapyAgent>
					<ae:agent>
						<ae:nscNumber>
							<xsl:value-of select="." />
						</ae:nscNumber>
					</ae:agent>
				</ae:priorTherapyAgent>
			</xsl:for-each>
		</ae:sAEReportPriorTherapy>
	</xsl:template>

	<xsl:template name="preexistingCondition">
		<ae:sAEReportPreExistingCondition>
			<xsl:if test="./patientmedicalcomment = 'Pre-existing Condition' ">
				<ae:preExistingCondition>
					<text>
						<xsl:value-of select="patientepisodename" />
					</text>
				</ae:preExistingCondition>
			</xsl:if>
			<xsl:if test="./patientmedicalcomment = 'Other Pre-existing Condition' ">
				<ae:other>
					<xsl:value-of select="patientepisodename" />
				</ae:other>
			</xsl:if>
		</ae:sAEReportPreExistingCondition>
	</xsl:template>

	<xsl:template name="splitDateYYYYMMDD">
		<xsl:param name="date" />
		<xsl:variable name="valueLength" select="string-length($date)-1" />
		<xsl:if test="$valueLength > 6">
			<ae:day>
				<xsl:value-of select='substring($date,7,2)' />
			</ae:day>
		</xsl:if>
		<ae:month>
			<xsl:value-of select='substring($date,5,2)' />
		</ae:month>
		<ae:year>
			<xsl:value-of select='substring($date,1,4)' />
		</ae:year>
	</xsl:template>

	<xsl:template name="dateTimeConverter">
		<xsl:param name="date" />
		<xsl:variable name="valueLength" select="string-length($date)-1" />
		<xsl:variable name="vDay" select='substring($date,7,2)' />
		<xsl:variable name="vMonth" select='substring($date,5,2)' />
		<xsl:variable name="vYear" select='substring($date,1,4)' />

		<xsl:variable name="vHour" select='substring($date,9,2)' />
		<xsl:variable name="vMin" select='substring($date,11,2)' />
		<xsl:variable name="vSec" select='substring($date,13,2)' />

		<xsl:variable name="dateTime"
			select="concat($vYear,'-',$vMonth,'-',$vDay,'T',$vHour,':',$vMin,':',$vSec)" />
		<xsl:value-of select="$dateTime" />
	</xsl:template>

	<xsl:template name="concomitantMedication">
		<ae:concomitantMedication>
			<ae:name>
				<xsl:value-of select="medicinalproduct" />
			</ae:name>
			<xsl:if test="./drugstartdate != '' ">
				<ae:startDate>
					<xsl:call-template name="splitDateYYYYMMDD">
						<xsl:with-param name="date" select="./drugstartdate" />
					</xsl:call-template>
				</ae:startDate>
			</xsl:if>
			<xsl:if test="./drugenddate != '' ">
				<ae:endDate>
					<xsl:call-template name="splitDateYYYYMMDD">
						<xsl:with-param name="date" select="./drugenddate" />
					</xsl:call-template>
				</ae:endDate>
			</xsl:if>
		</ae:concomitantMedication>
	</xsl:template>
	<xsl:template name="courseAgent">
		<ae:courseAgent>
			<!--Optional: -->
			<xsl:if test="drugenddate != '' ">
				<ae:lastAdministeredDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="drugenddate" />
					</xsl:call-template>
				</ae:lastAdministeredDate>
			</xsl:if>
			<xsl:if test="drugtreatmentduration and drugtreatmentduration != '0'">
				<ae:administrationDelayAmount>
						<xsl:value-of select="drugtreatmentduration" />
				</ae:administrationDelayAmount>
			</xsl:if>
			<xsl:if test="drugtreatmentdurationunit and drugtreatmentduration != '0'">
				<ae:administrationDelayUnits>
					<xsl:call-template name="lookup">
						<xsl:with-param name="_map" select="$map//delayuoms" />
						<xsl:with-param name="_code" select='drugtreatmentdurationunit' />
					</xsl:call-template>
				</ae:administrationDelayUnits>
			</xsl:if>
			<ae:dose>
				<ae:amount>
					<xsl:value-of select="drugcumulativedosagenumb" />
				</ae:amount>
				<xsl:if test="drugcumulativedosageunitextended">
					<ae:units>
						<xsl:value-of select="drugcumulativedosageunitextended" />
					</ae:units>
				</xsl:if>
			</ae:dose>
			<ae:studyAgent>
				<!--You have a CHOICE of the next 2 items at this level -->
				<ae:Agent>
					<ae:nscNumber>
						<xsl:value-of select="medicinalproduct" />
					</ae:nscNumber>
				</ae:Agent>
			</ae:studyAgent>
			<ae:lotNumber>
				<xsl:value-of select="drugbatchnumb" />
			</ae:lotNumber>
			<ae:comments>
				<xsl:value-of select="drugadditional" />
			</ae:comments>
			<xsl:if test="actiondrug">
				<ae:agentAdjustment>
					<xsl:call-template name="lookup">
						<xsl:with-param name="_map" select="$map//agent-dose-modifications" />
						<xsl:with-param name="_code" select='actiondrug' />
					</xsl:call-template>
				</ae:agentAdjustment>
			</xsl:if>
		</ae:courseAgent>
	</xsl:template>
	<xsl:template name="medicalDevice">
		<ae:medicalDevice>
			<!--Optional: -->
			<ae:brandName>
				<xsl:value-of select="devicenamebrand" />
			</ae:brandName>
			<!--Optional: -->
			<ae:commonName>
				<xsl:value-of select="medicinalproduct" />
			</ae:commonName>
			<!--Optional: -->
			<ae:deviceType>
				<xsl:value-of select="devicetype" />
			</ae:deviceType>
			<!--Optional: -->
			<ae:manufacturerName>
				<xsl:value-of select="devicemanufacturername" />
			</ae:manufacturerName>
			<!--Optional: -->
			<ae:manufacturerCity>
				<xsl:value-of select="devicemanufacturercity" />
			</ae:manufacturerCity>
			<!--Optional: -->
			<ae:manufacturerState>
				<xsl:value-of select="devicemanufacturerstate" />
			</ae:manufacturerState>
			<!--Optional: -->
			<ae:modelNumber>
				<xsl:value-of select="devicenumbermodel" />
			</ae:modelNumber>
			<!--Optional: -->
			<ae:catalogNumber>
				<xsl:value-of select="devicenumbercatalog" />
			</ae:catalogNumber>
			<!--Optional: -->
			<xsl:if test="devicedateexpiration">
				<ae:expirationDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="devicedateexpiration" />
					</xsl:call-template>
				</ae:expirationDate>
			</xsl:if>
			<!--Optional: -->
			<ae:serialNumber>
				<xsl:value-of select="devicenumberserial" />
			</ae:serialNumber>
			<!--Optional: -->
			<ae:otherNumber>
				<xsl:value-of select="devicenumberother" />
			</ae:otherNumber>
			<xsl:if test="drugenddate">
				<ae:explantedDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="drugenddate" />
					</xsl:call-template>
				</ae:explantedDate>
			</xsl:if>
			<xsl:if test="drugstartdate">
				<ae:implantedDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="drugstartdate" />
					</xsl:call-template>
				</ae:implantedDate>
			</xsl:if>
			<!--Optional: -->
			<ae:deviceOperator>
				<xsl:value-of select="deviceoperator" />
			</ae:deviceOperator>
			<!--Optional: -->
			<xsl:if test="devicereturndate">
				<ae:returnedDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="devicereturndate" />
					</xsl:call-template>
				</ae:returnedDate>
			</xsl:if>

			<xsl:if test="devicereprocessedflag = '1'">
				<ae:deviceReprocessed>YES</ae:deviceReprocessed>
				<ae:reprocessedName>
					<xsl:value-of select="devicereprocessorname" />
				</ae:reprocessedName>
				<ae:reprocessedAddress>
					<xsl:value-of select="devicereprocessoraddress" />
				</ae:reprocessedAddress>
			</xsl:if>
			<xsl:if test="devicereprocessedflag = '2'">
				<ae:deviceReprocessed>NO</ae:deviceReprocessed>
			</xsl:if>
	        
	        <xsl:if test="deviceavailableflag">
				<ae:evaluationAvailability>
					<xsl:value-of select="deviceavailableflag" />
				</ae:evaluationAvailability>
			</xsl:if>
			
			<ae:studyDevice>
				<ae:otherBrandName>
					<xsl:value-of select="devicenamebrand" />
				</ae:otherBrandName>
				<ae:otherCommonName>
					<xsl:value-of select="medicinalproduct" />
				</ae:otherCommonName>
				<!--Optional: -->
				<ae:otherDeviceType>
					<xsl:value-of select="devicetype" />
				</ae:otherDeviceType>
			</ae:studyDevice>
			<ae:otherDeviceOperator>
				<xsl:value-of select="deviceoperatorother" />
			</ae:otherDeviceOperator>
			<ae:lotNumber>
				<xsl:value-of select="drugbatchnumb" />
			</ae:lotNumber>
		</ae:medicalDevice>
	</xsl:template>
	<xsl:template name="radiationIntervention">
		<ae:radiationIntervention>
			<ae:dosage>
				<xsl:value-of select="drugcumulativedosagenumb" />
			</ae:dosage>
			<ae:dosageUnit>
				<xsl:value-of select="radiationdoseunit" />
			</ae:dosageUnit>
			<xsl:if test="./drugenddate != '' ">
				<ae:lastTreatmentDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="drugenddate" />
					</xsl:call-template>
				</ae:lastTreatmentDate>
			</xsl:if>
			<ae:fractionNumber>
				<xsl:value-of select="drugseparatedosagenumb" />
			</ae:fractionNumber>
			<ae:daysElapsed>
				<xsl:value-of select="drugintervaldosageunitnumb" />
			</ae:daysElapsed>
			<ae:adjustment>
				<xsl:call-template name="lookup">
					<xsl:with-param name="_map" select="$map//adjustments" />
					<xsl:with-param name="_code" select='actiondrug' />
				</xsl:call-template>
			</ae:adjustment>
			<ae:administration>
				<xsl:value-of select="medicinalproduct" />
			</ae:administration>
		</ae:radiationIntervention>
	</xsl:template>
	<xsl:template name="surgeryIntervention">
		<ae:surgeryIntervention>
			<xsl:if test="drugstartdate != '' ">
				<ae:interventionDate>
					<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
						<xsl:with-param name="date" select="drugstartdate" />
					</xsl:call-template>
				</ae:interventionDate>
			</xsl:if>
			<ae:InterventionSite>
				<ae:name>
					<xsl:value-of select="medicinalproduct" />
				</ae:name>
			</ae:InterventionSite>
		</ae:surgeryIntervention>
	</xsl:template>
	<xsl:template name="lab">
		<ae:lab>
			<!--Optional: -->
			<ae:labTerm>
				<ae:term>
					<xsl:value-of select="testname" />
				</ae:term>
			</ae:labTerm>
			<!--Optional: -->
			<ae:units>
				<xsl:call-template name="lookup">
					<xsl:with-param name="_map" select="$map//uoms" />
					<xsl:with-param name="_code" select='testunit' />
				</xsl:call-template>
			</ae:units>
			<!--Optional: -->
			<xsl:if test="testtype = 'Baseline'">
				<ae:baseline>
					<ae:value>
						<xsl:value-of select="testresult" />
					</ae:value>
					<xsl:if test="./testdate != '' ">
						<ae:date>
							<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
								<xsl:with-param name="date" select="./testdate" />
							</xsl:call-template>
						</ae:date>
					</xsl:if>
				</ae:baseline>
			</xsl:if>
			<!--Optional: -->
			<xsl:if test="testtype = 'Worst'">
				<ae:nadir>
					<ae:value>
						<xsl:value-of select="testresult" />
					</ae:value>
					<xsl:if test="./testdate != '' ">
						<ae:date>
							<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
								<xsl:with-param name="date" select="./testdate" />
							</xsl:call-template>
						</ae:date>
					</xsl:if>
				</ae:nadir>
			</xsl:if>
			<!--Optional: -->
			<xsl:if test="testtype = 'Recovery'">
				<ae:recovery>
					<ae:value>
						<xsl:value-of select="testresult" />
					</ae:value>
					<xsl:if test="./testdate != '' ">
						<ae:date>
							<xsl:call-template name="dateConverterYYYYMMDDtoYY-MM-DD">
								<xsl:with-param name="date" select="./testdate" />
							</xsl:call-template>
						</ae:date>
					</xsl:if>
				</ae:recovery>
			</xsl:if>
			<xsl:if
				test="testname = 'Bacterial infection NOS' or testname = 'Fungal infection NOS' or testname = 'Viral infection NOS' 
				or testname = 'Blood Culture'  or testname = 'Stool Culture'  or testname = 'Urine Culture'">
				<ae:infectiousAgent>
					<xsl:value-of select="testresult" />
				</ae:infectiousAgent>
				<ae:site>
					<xsl:value-of select="infectionsite" />
{'fraction_non_alphanumeric': 0.16719659541163234, 'fraction_numerical': 0.0017936049829607526, 'mean_word_length': 3.1163836499093898, 'pattern_counts': {'":': 0, '<': 1599, '<?xml version=': 2, '>': 1601, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '836802', 'n_tokens_mistral': 29496, 'n_tokens_neox': 20439, 'n_words': 2631}