<?xml version="1.0" encoding="UTF-8"?>
<COSD:COSD
    xmlns:COSD="http://www.datadictionary.nhs.uk/messages/COSDPathology-v8-1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Id root="[% data.Id.root %]"/>
  <OrganisationIdentifierCodeOfSubmittingOrganisation extension="[%
    data.OrganisationIdentifierCodeOfSubmittingOrganisation.extension %]"/>
  <RecordCount value="[% data.RecordCount.value %]"/>
  <ReportingPeriodStartDate>[% data.ReportingPeriodStartDate %]</ReportingPeriodStartDate>
  <ReportingPeriodEndDate>[% data.ReportingPeriodEndDate %]</ReportingPeriodEndDate>
  <FileCreationDateTime>[% data.FileCreationDateTime %]</FileCreationDateTime>
  [%- FOREACH record IN data.COSDRecord;
		r = record.Haematological.HaematologicalCore %]
  <COSDRecord>
    <Id root="[% record.Id.root %]"/>
    <Haematological>
      <HaematologicalCore>
		[%- patient = r.HaematologicalCoreLinkagePatientId %]
        <HaematologicalCoreLinkagePatientId>
          [%- IF patient.NHSNumber.extension %]
          <NHSNumber extension="[% patient.NHSNumber.extension %]"/>
          [%- END %]
          <LocalPatientIdExtended>[% patient.LocalPatientIdExtended %]</LocalPatientIdExtended>
          <NHSNumberStatusIndicator code="[% patient.NHSNumberStatusIndicator.code %]"/>
          <Birthdate>[% patient.Birthdate %]</Birthdate>
          <OrganisationIdentifierCodeOfProvider extension="[%
            patient.OrganisationIdentifierCodeOfProvider.extension %]"/>
        </HaematologicalCoreLinkagePatientId>
		[%- demographics = r.HaematologicalCoreDemographics %]
        <HaematologicalCoreDemographics>
          <PersonFamilyName>
            <family>[% demographics.PersonFamilyName.family %]</family>
          </PersonFamilyName>
          <PersonGivenName>
            <given>[% demographics.PersonGivenName.given %]</given>
          </PersonGivenName>
		  [%- address = demographics.Address.UnstructuredAddress.streetAddressLine;
            IF address; # will automatically also have post-code %]
          <Address>
            <UnstructuredAddress>
              <streetAddressLine>[% address %]</streetAddressLine>
            </UnstructuredAddress>
          </Address>
          <Postcode>
            <postalCode>[% demographics.Postcode.postalCode %]</postalCode>
          </Postcode>
		  [%- END %]
          <Gender code="[% demographics.Gender.code %]"/>
        </HaematologicalCoreDemographics>
		[%- pathology = r.HaematologicalCorePathology %]
        <HaematologicalCorePathology>
          <InvestigationResultDate>[% pathology.InvestigationResultDate %]</InvestigationResultDate>
          <ServiceReportId extension="[% pathology.ServiceReportId.extension %]"/>
          <ServiceReportStatus code="[% pathology.ServiceReportStatus.code %]"/>
          <PathTestReqCareProfCode extension="[% pathology.PathTestReqCareProfCode.extension %]"/>
          <OrganisationSiteIdentifierOfPathologyTestRequest extension="[%
			pathology.OrganisationSiteIdentifierOfPathologyTestRequest.extension%]"/>
          [%- IF pathology.SampleCollectionDate %]
          <SampleCollectionDate>[% pathology.SampleCollectionDate %]</SampleCollectionDate>
          [%- ELSE %]
          <SampleReceiptDate>[% pathology.SampleReceiptDate %]</SampleReceiptDate>
          [%- END %]
          <OrganisationIdentifierOfReportingPathologist extension="[%
			pathology.OrganisationIdentifierOfReportingPathologist.extension %]"/>
          <PathologistConsultantCode extension="[% pathology.PathologistConsultantCode.extension %]"/>
          [%- IF pathology.TopographySNOMEDPathology # suspended %]
          <TopographySNOMEDPathology code="[% pathology.TopographySNOMEDPathology.code %]"/>
          [%- END %]
		  [%- icdo3 = pathology.MorphologySNOMEDPathology.code;
			IF icdo3 %]
		  <MorphologySNOMEDPathology code="[% icdo3 %]"/>
		  [%- END %]
          <PathologyReportText><![CDATA[[%
            pathology.PathologyReportText.textContent %]]]></PathologyReportText>
        </HaematologicalCorePathology>
      </HaematologicalCore>
    </Haematological>
  </COSDRecord>
  [%- END %]
</COSD:COSD>
