SGHI FHIR Profile Implementation Guide
0.1.0 - ci-build

SGHI FHIR Profile Implementation Guide, published by Kathurima Kimathi. This guide is not an authorized publication; it is the continuous build for version 0.1.0 built by the FHIR (HL7® FHIR® Standard) CI Build. This version is based on the current content of https://github.com/savannahghi/sil_fhir_profile_ig/ and changes regularly. See the Directory of published versions

: Inpatient Discharge Note Extraction - TTL Representation

Active as of 2026-10-06

Raw ttl | Download

@prefix fhir: <http://hl7.org/fhir/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

# - resource -------------------------------------------------------------------

<http://hl7.org/fhir/StructureMap/ExtractInpatientDischargeNote> a fhir:StructureMap ;
  fhir:nodeRole fhir:treeRoot ;
  fhir:id [ fhir:v "ExtractInpatientDischargeNote"] ; # 
  fhir:text [
     fhir:status [ fhir:v "generated" ] ;
     fhir:div [ fhir:v "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p class=\"res-header-id\"><b>Generated Narrative: StructureMap ExtractInpatientDischargeNote</b></p><a name=\"ExtractInpatientDischargeNote\"> </a><a name=\"hcExtractInpatientDischargeNote\"> </a><pre class=\"fml\">\r\n<span style=\"color: #cc00cc\">/// <b>url</b> = </span><span style=\"color: blue\">'https://fhir.slade360.co.ke/fhir/StructureMap/ExtractInpatientDischargeNote'</span>\r\n<span style=\"color: #cc00cc\">/// <b>name</b> = </span><span style=\"color: blue\">'ExtractInpatientDischargeNote'</span>\r\n<span style=\"color: #cc00cc\">/// <b>title</b> = </span><span style=\"color: blue\">'Inpatient Discharge Note Extraction'</span>\r\n<span style=\"color: #cc00cc\">/// <b>status</b> = </span><span style=\"color: blue\">'active'</span>\r\n<span style=\"color: #cc00cc\">/// <b>description</b> = </span><span style=\"color: blue\">'Turns a response to the inpatient discharge note into a discharge-summary Composition and a ServiceRequest for the follow-up. Medication on discharge is referenced, never copied: those MedicationRequests already exist on the server -- the form picks them from it -- so they become section entries rather than new resources. The follow-up is intent `plan` rather than an Appointment because the form captures it as free text with no date to book against; give the item a date and an Appointment becomes the truer target. The Encounter is deliberately untouched: closing the stay -- Encounter.status and the discharge disposition -- belongs to the EMR\\'s discharge workflow, which knows about bed release, billing and the actual moment the patient left. Writing a note is not the same event as being discharged, and a map that conflated them would close stays early.'</span>\r\n\r\n<b>uses</b><span style=\"color: navy\"> &quot;</span><a href=\"http://hl7.org/fhir/R5/questionnaireresponse.html\" title=\"QuestionnaireResponse\">http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse</a><span style=\"color: navy\">&quot; </span><b>alias </b>QR <b>as </b><b>source</b>\r\n<b>uses</b><span style=\"color: navy\"> &quot;</span><a href=\"http://hl7.org/fhir/R5/bundle.html\" title=\"Bundle\">http://hl7.org/fhir/StructureDefinition/Bundle</a><span style=\"color: navy\">&quot; </span><b>alias </b>Bundle <b>as </b><b>target</b>\r\n<b>uses</b><span style=\"color: navy\"> &quot;</span><a href=\"http://hl7.org/fhir/R5/composition.html\" title=\"Composition\">http://hl7.org/fhir/StructureDefinition/Composition</a><span style=\"color: navy\">&quot; </span><b>alias </b>Composition <b>as </b><b>target</b>\r\n<b>uses</b><span style=\"color: navy\"> &quot;</span><a href=\"http://hl7.org/fhir/R5/servicerequest.html\" title=\"ServiceRequest\">http://hl7.org/fhir/StructureDefinition/ServiceRequest</a><span style=\"color: navy\">&quot; </span><b>alias </b>ServiceRequest <b>as </b><b>target</b>\r\n\r\n<b>group </b>ExtractInpatientDischargeNote<span style=\"color: navy\">(</span><b>source</b> <span style=\"color: maroon\">qr</span><span style=\"color: navy\"> : </span>QR, <b>target</b> <span style=\"color: maroon\">bundle</span><span style=\"color: navy\"> : </span>Bundle<span style=\"color: navy\">)</span><span style=\"color: navy\"> {\r\n</span>  qr<span style=\"color: navy\"><b> -&gt; </b></span>bundle.type = <span style=\"color: blue\">'transaction'</span> <i>&quot;setBundleType&quot;</i><span style=\"color: navy\">;</span>\r\n  qr<span style=\"color: navy\"><b> -&gt; </b></span> bundle.entry<b> as </b><span style=\"color: maroon\">entry</span><span style=\"color: navy\">, </span> entry.resource = <b>create</b><span style=\"color: navy\">(</span><span style=\"color: blue\">'Composition'</span><span style=\"color: navy\">)</span><b> as </b><span style=\"color: maroon\">comp</span><b> then</b><span style=\"color: navy\"> {\r\n</span>    qr<span style=\"color: navy\"><b> -&gt; </b></span>comp<b> then </b>CopyResponseTags<span style=\"color: navy\">(</span><span style=\"color: maroon\">qr</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">comp</span><span style=\"color: navy\">)</span> <i>&quot;responseTags&quot;</i><span style=\"color: navy\">;</span>\r\n    qr<span style=\"color: navy\"><b> -&gt; </b></span> comp<span style=\"color: navy\">, </span> entry<b> then </b>BuildNoteBase<span style=\"color: navy\">(</span><span style=\"color: maroon\">qr</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">comp</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">entry</span><span style=\"color: navy\">)</span> <i>&quot;noteBase&quot;</i><span style=\"color: navy\">;</span>\r\n    qr<span style=\"color: navy\"><b> -&gt; </b></span>comp.name = <span style=\"color: blue\">'InpatientDischargeNote'</span> <i>&quot;noteName&quot;</i><span style=\"color: navy\">;</span>\r\n    qr<span style=\"color: navy\"><b> -&gt; </b></span>comp.title = <span style=\"color: blue\">'Discharge note'</span> <i>&quot;noteTitle&quot;</i><span style=\"color: navy\">;</span>\r\n    qr<span style=\"color: navy\"><b> -&gt; </b></span>comp.type<b> as </b><span style=\"color: maroon\">ty</span><b> then</b><span style=\"color: navy\"> {\r\n</span>      qr<span style=\"color: navy\"><b> -&gt; </b></span>ty.coding<b> as </b><span style=\"color: maroon\">sghi</span><b> then</b><span style=\"color: navy\"> {\r\n</span>        qr<span style=\"color: navy\"><b> -&gt; </b></span>sghi.system = <span style=\"color: blue\">'https://fhir.slade360.co.ke/fhir/CodeSystem/inpatient-document-type-codesystem'</span> <i>&quot;sghiSystem&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<span style=\"color: navy\"><b> -&gt; </b></span>sghi.code = <span style=\"color: blue\">'discharge-note'</span> <i>&quot;sghiCode&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<span style=\"color: navy\"><b> -&gt; </b></span>sghi.display = <span style=\"color: blue\">'Discharge note'</span> <i>&quot;sghiDisplay&quot;</i><span style=\"color: navy\">;</span>\r\n      <span style=\"color: navy\">}</span> <i>&quot;sghiCoding&quot;</i><span style=\"color: navy\">;</span>\r\n      qr<span style=\"color: navy\"><b> -&gt; </b></span>ty.coding<b> as </b><span style=\"color: maroon\">loinc</span><b> then</b><span style=\"color: navy\"> {\r\n</span>        qr<span style=\"color: navy\"><b> -&gt; </b></span>loinc.system = <span style=\"color: blue\">'http://loinc.org'</span> <i>&quot;loincSystem&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<span style=\"color: navy\"><b> -&gt; </b></span>loinc.code = <span style=\"color: blue\">'18842-5'</span> <i>&quot;loincCode&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<span style=\"color: navy\"><b> -&gt; </b></span>loinc.display = <span style=\"color: blue\">'Discharge summary'</span> <i>&quot;loincDisplay&quot;</i><span style=\"color: navy\">;</span>\r\n      <span style=\"color: navy\">}</span> <i>&quot;loincCoding&quot;</i><span style=\"color: navy\">;</span>\r\n      qr<span style=\"color: navy\"><b> -&gt; </b></span>ty.text = <span style=\"color: blue\">'Discharge summary'</span> <i>&quot;typeText&quot;</i><span style=\"color: navy\">;</span>\r\n    <span style=\"color: navy\">}</span> <i>&quot;noteType&quot;</i><span style=\"color: navy\">;</span>\r\n    qr.item<b> as </b><span style=\"color: maroon\">it</span><b> where </b><span style=\"color: navy\">(</span>(linkId = 'summary') and answer.value.exists()<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>comp.section<b> as </b><span style=\"color: maroon\">sec</span><b> then</b><span style=\"color: navy\"> {\r\n</span>      it<span style=\"color: navy\"><b> -&gt; </b></span>sec.title = <span style=\"color: blue\">'Admission summary'</span> <i>&quot;summaryTitle&quot;</i><span style=\"color: navy\">;</span>\r\n      it<span style=\"color: navy\"><b> -&gt; </b></span>sec<b> then </b>AddSectionText<span style=\"color: navy\">(</span><span style=\"color: maroon\">it</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">sec</span><span style=\"color: navy\">)</span> <i>&quot;summaryText&quot;</i><span style=\"color: navy\">;</span>\r\n    <span style=\"color: navy\">}</span> <i>&quot;summaryRule&quot;</i><span style=\"color: navy\">;</span>\r\n    <span style=\"color: navy\">// </span><span style=\"color: green\">One section, one entry per medication. No MedicationRequest is created:</span>\r\n    <span style=\"color: navy\">// </span><span style=\"color: green\">the form picked these off the server, and minting copies would double the</span>\r\n    <span style=\"color: navy\">// </span><span style=\"color: green\">patient's prescription list.</span>\r\n    qr.item<b> as </b><span style=\"color: maroon\">itM</span><b> where </b><span style=\"color: navy\">(</span>(linkId = 'medsOnDischarge') and answer.value.exists()<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>comp.section<b> as </b><span style=\"color: maroon\">secM</span><b> then</b><span style=\"color: navy\"> {\r\n</span>      itM<span style=\"color: navy\"><b> -&gt; </b></span>secM.title = <span style=\"color: blue\">'Medication on discharge'</span> <i>&quot;medsTitle&quot;</i><span style=\"color: navy\">;</span>\r\n      itM.answer<b> as </b><span style=\"color: maroon\">aM</span><b> then</b><span style=\"color: navy\"> {\r\n</span>        aM.value<b> as </b><span style=\"color: maroon\">mv</span><b> where </b><span style=\"color: navy\">(</span>(reference.exists() and (reference.contains('://').not() and reference.startsWith('#').not() and reference.startsWith('urn:').not())) or reference.empty()<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>secM.entry = <span style=\"color: maroon\">mv</span> <i>&quot;medsEntry&quot;</i><span style=\"color: navy\">;</span>\r\n      <span style=\"color: navy\">}</span> <i>&quot;medsAnswers&quot;</i><span style=\"color: navy\">;</span>\r\n    <span style=\"color: navy\">}</span> <i>&quot;medsRule&quot;</i><span style=\"color: navy\">;</span>\r\n    qr.item<b> as </b><span style=\"color: maroon\">it</span><b> where </b><span style=\"color: navy\">(</span>(linkId = 'followUp') and answer.value.exists()<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>comp.section<b> as </b><span style=\"color: maroon\">sec</span><b> then</b><span style=\"color: navy\"> {\r\n</span>      it<span style=\"color: navy\"><b> -&gt; </b></span>sec.title = <span style=\"color: blue\">'Follow-up'</span> <i>&quot;followUpTitle&quot;</i><span style=\"color: navy\">;</span>\r\n      it<span style=\"color: navy\"><b> -&gt; </b></span>sec<b> then </b>AddSectionText<span style=\"color: navy\">(</span><span style=\"color: maroon\">it</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">sec</span><span style=\"color: navy\">)</span> <i>&quot;followUpText&quot;</i><span style=\"color: navy\">;</span>\r\n    <span style=\"color: navy\">}</span> <i>&quot;followUpSectionRule&quot;</i><span style=\"color: navy\">;</span>\r\n    qr.item<b> as </b><span style=\"color: maroon\">it</span><b> where </b><span style=\"color: navy\">(</span>(linkId = 'adviceGiven') and answer.value.exists()<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>comp.section<b> as </b><span style=\"color: maroon\">sec</span><b> then</b><span style=\"color: navy\"> {\r\n</span>      it<span style=\"color: navy\"><b> -&gt; </b></span>sec.title = <span style=\"color: blue\">'Advice given'</span> <i>&quot;adviceTitle&quot;</i><span style=\"color: navy\">;</span>\r\n      it<span style=\"color: navy\"><b> -&gt; </b></span>sec<b> then </b>AddSectionText<span style=\"color: navy\">(</span><span style=\"color: maroon\">it</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">sec</span><span style=\"color: navy\">)</span> <i>&quot;adviceText&quot;</i><span style=\"color: navy\">;</span>\r\n    <span style=\"color: navy\">}</span> <i>&quot;adviceRule&quot;</i><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">}</span> <i>&quot;composition&quot;</i><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">The follow-up also leaves the document, so it can be worked rather than read.</span>\r\n  qr.item<b> as </b><span style=\"color: maroon\">itF</span><b> where </b><span style=\"color: navy\">(</span>(linkId = 'followUp') and answer.value.exists()<span style=\"color: navy\">)</span><b> then</b><span style=\"color: navy\"> {\r\n</span>    itF.answer first<b> as </b><span style=\"color: maroon\">aF</span><b> then</b><span style=\"color: navy\"> {\r\n</span>      aF<span style=\"color: navy\"><b> -&gt; </b></span> bundle.entry<b> as </b><span style=\"color: maroon\">entryS</span><span style=\"color: navy\">, </span> entryS.resource = <b>create</b><span style=\"color: navy\">(</span><span style=\"color: blue\">'ServiceRequest'</span><span style=\"color: navy\">)</span><b> as </b><span style=\"color: maroon\">sr</span><b> then</b><span style=\"color: navy\"> {\r\n</span>        qr<span style=\"color: navy\"><b> -&gt; </b></span>sr<b> then </b>CopyResponseTags<span style=\"color: navy\">(</span><span style=\"color: maroon\">qr</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">sr</span><span style=\"color: navy\">)</span> <i>&quot;responseTags&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<span style=\"color: navy\"><b> -&gt; </b></span>sr.id = <b>uuid</b><span style=\"color: navy\">(</span><span style=\"color: navy\">)</span><b> then </b>SetServiceRequestFullUrl<span style=\"color: navy\">(</span><span style=\"color: maroon\">sr</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">entryS</span><span style=\"color: navy\">)</span> <i>&quot;idAndFullUrl&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<span style=\"color: navy\"><b> -&gt; </b></span>entryS.request<b> as </b><span style=\"color: maroon\">request</span><b> then</b><span style=\"color: navy\"> {\r\n</span>          qr<span style=\"color: navy\"><b> -&gt; </b></span>request.method = <span style=\"color: blue\">'POST'</span> <i>&quot;requestMethod&quot;</i><span style=\"color: navy\">;</span>\r\n          qr<span style=\"color: navy\"><b> -&gt; </b></span>request.url = <span style=\"color: blue\">'ServiceRequest'</span> <i>&quot;requestUrl&quot;</i><span style=\"color: navy\">;</span>\r\n        <span style=\"color: navy\">}</span> <i>&quot;entryRequest&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<span style=\"color: navy\"><b> -&gt; </b></span>sr.status = <span style=\"color: blue\">'active'</span> <i>&quot;status&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<span style=\"color: navy\"><b> -&gt; </b></span>sr.intent = <span style=\"color: blue\">'plan'</span> <i>&quot;intent&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<span style=\"color: navy\"><b> -&gt; </b></span>sr.priority = <span style=\"color: blue\">'routine'</span> <i>&quot;priority&quot;</i><span style=\"color: navy\">;</span>\r\n        <span style=\"color: navy\">// </span><span style=\"color: green\">R5 ServiceRequest.code is a CodeableReference, so the concept sits one</span>\r\n        <span style=\"color: navy\">// </span><span style=\"color: green\">level down.</span>\r\n        aF.value<b> as </b><span style=\"color: maroon\">v</span><span style=\"color: navy\"><b> -&gt; </b></span>sr.code<b> as </b><span style=\"color: maroon\">codeRef</span><b> then</b><span style=\"color: navy\"> {\r\n</span>          aF<span style=\"color: navy\"><b> -&gt; </b></span>codeRef.concept<b> as </b><span style=\"color: maroon\">concept</span><b> then</b><span style=\"color: navy\"> {\r\n</span>            aF<span style=\"color: navy\"><b> -&gt; </b></span>concept.coding<b> as </b><span style=\"color: maroon\">coding</span><b> then</b><span style=\"color: navy\"> {\r\n</span>              aF<span style=\"color: navy\"><b> -&gt; </b></span>coding.system = <span style=\"color: blue\">'https://fhir.slade360.co.ke/fhir/CodeSystem/inpatient-clinical-concept-codesystem'</span> <i>&quot;codeSystem&quot;</i><span style=\"color: navy\">;</span>\r\n              aF<span style=\"color: navy\"><b> -&gt; </b></span>coding.code = <span style=\"color: blue\">'discharge-follow-up'</span> <i>&quot;codeCode&quot;</i><span style=\"color: navy\">;</span>\r\n              aF<span style=\"color: navy\"><b> -&gt; </b></span>coding.display = <span style=\"color: blue\">'Follow-up arranged on discharge'</span> <i>&quot;codeDisplay&quot;</i><span style=\"color: navy\">;</span>\r\n            <span style=\"color: navy\">}</span> <i>&quot;codeCoding&quot;</i><span style=\"color: navy\">;</span>\r\n            v<span style=\"color: navy\"><b> -&gt; </b></span>concept.text = <span style=\"color: maroon\">v</span> <i>&quot;codeText&quot;</i><span style=\"color: navy\">;</span>\r\n          <span style=\"color: navy\">}</span> <i>&quot;codeConcept&quot;</i><span style=\"color: navy\">;</span>\r\n        <span style=\"color: navy\">}</span> <i>&quot;serviceRequestCode&quot;</i><span style=\"color: navy\">;</span>\r\n        qr.subject<b> as </b><span style=\"color: maroon\">s</span><b> where </b><span style=\"color: navy\">(</span>(reference.exists() and (reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient')))<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>sr.subject = <span style=\"color: maroon\">s</span><span style=\"color: navy\">;</span>\r\n        qr.encounter<b> as </b><span style=\"color: maroon\">e</span><b> where </b><span style=\"color: navy\">(</span>(reference.exists() and (reference.startsWith('Encounter/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Encounter')))<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>sr.encounter = <span style=\"color: maroon\">e</span><span style=\"color: navy\">;</span>\r\n        qr.authored<b> as </b><span style=\"color: maroon\">t</span><span style=\"color: navy\"><b> -&gt; </b></span>sr.authoredOn = <span style=\"color: maroon\">t</span> <i>&quot;authoredOn&quot;</i><span style=\"color: navy\">;</span>\r\n        qr.author<b> as </b><span style=\"color: maroon\">a</span><b> where </b><span style=\"color: navy\">(</span>(reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient')))<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>sr.requester = <span style=\"color: maroon\">a</span> <i>&quot;requesterFromAuthor&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<b> where </b><span style=\"color: navy\">(</span>author.where(((reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient'))))).exists().not()<span style=\"color: navy\">)</span><b> then</b><span style=\"color: navy\"> {\r\n</span>          qr.source<b> as </b><span style=\"color: maroon\">src</span><b> where </b><span style=\"color: navy\">(</span>(reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient')))<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>sr.requester = <span style=\"color: maroon\">src</span> <i>&quot;requesterFromSource&quot;</i><span style=\"color: navy\">;</span>\r\n        <span style=\"color: navy\">}</span> <i>&quot;requesterFallback&quot;</i><span style=\"color: navy\">;</span>\r\n        qr<span style=\"color: navy\"><b> -&gt; </b></span>sr.supportingInfo<b> as </b><span style=\"color: maroon\">si</span><b> then</b><span style=\"color: navy\"> {\r\n</span>          qr<span style=\"color: navy\"><b> -&gt; </b></span>si.reference = <b>create</b><span style=\"color: navy\">(</span><span style=\"color: blue\">'Reference'</span><span style=\"color: navy\">)</span><b> as </b><span style=\"color: maroon\">ref</span><b> then</b><span style=\"color: navy\"> {\r\n</span>            qr.id<b> as </b><span style=\"color: maroon\">qid</span><span style=\"color: navy\"><b> -&gt; </b></span>ref.reference = <span style=\"color: maroon\">qid</span> <i>&quot;supportingInfoRef&quot;</i><span style=\"color: navy\">;</span>\r\n          <span style=\"color: navy\">}</span> <i>&quot;supportingInfoTarget&quot;</i><span style=\"color: navy\">;</span>\r\n        <span style=\"color: navy\">}</span> <i>&quot;linkQuestionnaireResponse&quot;</i><span style=\"color: navy\">;</span>\r\n      <span style=\"color: navy\">}</span> <i>&quot;serviceRequest&quot;</i><span style=\"color: navy\">;</span>\r\n    <span style=\"color: navy\">}</span> <i>&quot;followUpAnswer&quot;</i><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">}</span> <i>&quot;followUpRequestRule&quot;</i><span style=\"color: navy\">;</span>\r\n<span style=\"color: navy\">}\r\n\r\n</span><span style=\"color: navy\">// </span><span style=\"color: green\">─────────────────────────────────────────────────────────────────────────────</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">Shared note scaffolding. Repeated in each note map rather than imported: the</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">transform engine resolves `imports` against whatever StructureMaps happen to</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">be on the server, and a note that stops extracting because a shared map was</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">not uploaded is a worse failure than a duplicated group.</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">─────────────────────────────────────────────────────────────────────────────</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">Two notes on what the publisher's StructureMap validator says about this file.</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">Source parameters below are left untyped on purpose. Writing `: Element` makes</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">the validator compare QuestionnaireResponse.item against the literal 'Element'</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">and report them as incompatible with each other, which is noise, not a finding.</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">Untyped, it says only that it cannot check the paths.</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">`evaluate(context, expression)` is flagged as &quot;takes 1 parameter but 2 were</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">found&quot;. Leave it. The FML parser in the same jar refuses the one-argument form</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">outright -- it wants a parameter, a comma, then the FHIRPath -- so the publisher</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">is objecting to what its own compiler emits. Two arguments is the only form that</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">compiles, and it is what the R5 transform engine executes.</span>\r\n<b>group </b>BuildNoteBase<span style=\"color: navy\">(</span><b>source</b> <span style=\"color: maroon\">qr</span><span style=\"color: navy\"> : </span>QR, <b>target</b> <span style=\"color: maroon\">comp</span><span style=\"color: navy\"> : </span>Composition, <b>target</b> <span style=\"color: maroon\">entry</span><span style=\"color: navy\">)</span><span style=\"color: navy\"> {\r\n</span>  qr<span style=\"color: navy\"><b> -&gt; </b></span>comp.id = <b>uuid</b><span style=\"color: navy\">(</span><span style=\"color: navy\">)</span><b> then </b>SetCompositionFullUrl<span style=\"color: navy\">(</span><span style=\"color: maroon\">comp</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">entry</span><span style=\"color: navy\">)</span> <i>&quot;idAndFullUrl&quot;</i><span style=\"color: navy\">;</span>\r\n  qr<span style=\"color: navy\"><b> -&gt; </b></span>entry.request<b> as </b><span style=\"color: maroon\">request</span><b> then</b><span style=\"color: navy\"> {\r\n</span>    qr<span style=\"color: navy\"><b> -&gt; </b></span>request.method = <span style=\"color: blue\">'POST'</span> <i>&quot;requestMethod&quot;</i><span style=\"color: navy\">;</span>\r\n    qr<span style=\"color: navy\"><b> -&gt; </b></span>request.url = <span style=\"color: blue\">'Composition'</span> <i>&quot;requestUrl&quot;</i><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">}</span> <i>&quot;entryRequest&quot;</i><span style=\"color: navy\">;</span>\r\n  qr<span style=\"color: navy\"><b> -&gt; </b></span>comp.status = <span style=\"color: blue\">'final'</span> <i>&quot;status&quot;</i><span style=\"color: navy\">;</span>\r\n  qr.subject<b> as </b><span style=\"color: maroon\">s</span><b> where </b><span style=\"color: navy\">(</span>(reference.exists() and (reference.contains('://').not() and reference.startsWith('#').not() and reference.startsWith('urn:').not())) or reference.empty()<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>comp.subject = <span style=\"color: maroon\">s</span><span style=\"color: navy\">;</span>\r\n  qr.encounter<b> as </b><span style=\"color: maroon\">e</span><b> where </b><span style=\"color: navy\">(</span>(reference.exists() and (reference.startsWith('Encounter/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Encounter')))<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>comp.encounter = <span style=\"color: maroon\">e</span><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">Composition.date is 1..1. `authored` is only 0..1 on the response, so when a</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">client sends none the extraction time stands in. It is a poor substitute for</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">when the note was written, but a document with no date at all is worse, and</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">extraction happens moments after submission in practice.</span>\r\n  qr.authored<b> as </b><span style=\"color: maroon\">t</span><span style=\"color: navy\"><b> -&gt; </b></span>comp.date = <span style=\"color: maroon\">t</span> <i>&quot;date&quot;</i><span style=\"color: navy\">;</span>\r\n  qr<b> where </b><span style=\"color: navy\">(</span>authored.exists().not()<span style=\"color: navy\">)</span><b> then</b><span style=\"color: navy\"> {\r\n</span>    qr<span style=\"color: navy\"><b> -&gt; </b></span>comp.date = evaluate<span style=\"color: navy\">(</span>qr<span style=\"color: navy\">, </span>now()<span style=\"color: navy\">)</span> <i>&quot;dateFromExtractionTime&quot;</i><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">}</span> <i>&quot;dateFallback&quot;</i><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">Composition.author is 1..1 minimum. R5 QuestionnaireResponse carries both</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">`author` (who recorded the answers) and `source` (who supplied them); the</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">author is the one who wrote the note, so it wins, and `source` only stands</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">in when the client did not send an author. If the response carries neither,</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">the note comes out unauthored -- there is nothing on a QuestionnaireResponse</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">to invent one from, and guessing at who wrote a clinical note is not a thing</span>\r\n  <span style=\"color: navy\">// </span><span style=\"color: green\">a map should do.</span>\r\n  qr.author<b> as </b><span style=\"color: maroon\">a</span><b> where </b><span style=\"color: navy\">(</span>(reference.exists() and (reference.startsWith('Patient/') or reference.startsWith('Organization/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient') or ('/' + type).endsWith('/Organization')))<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>comp.author = <span style=\"color: maroon\">a</span> <i>&quot;authorFromAuthor&quot;</i><span style=\"color: navy\">;</span>\r\n  qr<b> where </b><span style=\"color: navy\">(</span>author.where(((reference.exists() and (reference.startsWith('Patient/') or reference.startsWith('Organization/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient') or ('/' + type).endsWith('/Organization'))))).exists().not()<span style=\"color: navy\">)</span><b> then</b><span style=\"color: navy\"> {\r\n</span>    qr.source<b> as </b><span style=\"color: maroon\">src</span><b> where </b><span style=\"color: navy\">(</span>(reference.exists() and (reference.startsWith('Patient/') or reference.startsWith('Organization/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient') or ('/' + type).endsWith('/Organization')))<span style=\"color: navy\">)</span><span style=\"color: navy\"><b> -&gt; </b></span>comp.author = <span style=\"color: maroon\">src</span> <i>&quot;authorFromSource&quot;</i><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">}</span> <i>&quot;authorFallback&quot;</i><span style=\"color: navy\">;</span>\r\n  qr<span style=\"color: navy\"><b> -&gt; </b></span>comp.relatesTo<b> as </b><span style=\"color: maroon\">rel</span><b> then</b><span style=\"color: navy\"> {\r\n</span>    qr<span style=\"color: navy\"><b> -&gt; </b></span>rel.type = <span style=\"color: blue\">'derived-from'</span> <i>&quot;relatesToType&quot;</i><span style=\"color: navy\">;</span>\r\n    qr<span style=\"color: navy\"><b> -&gt; </b></span>rel.resourceReference = <b>create</b><span style=\"color: navy\">(</span><span style=\"color: blue\">'Reference'</span><span style=\"color: navy\">)</span><b> as </b><span style=\"color: maroon\">ref</span><b> then</b><span style=\"color: navy\"> {\r\n</span>      qr.id<b> as </b><span style=\"color: maroon\">qid</span><span style=\"color: navy\"><b> -&gt; </b></span>ref.reference = <span style=\"color: maroon\">qid</span> <i>&quot;relatesToRef&quot;</i><span style=\"color: navy\">;</span>\r\n    <span style=\"color: navy\">}</span> <i>&quot;relatesToTarget&quot;</i><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">}</span> <i>&quot;linkQuestionnaireResponse&quot;</i><span style=\"color: navy\">;</span>\r\n<span style=\"color: navy\">}\r\n\r\n</span><b>group </b>AddSectionText<span style=\"color: navy\">(</span><b>source</b> <span style=\"color: maroon\">it</span>, <b>target</b> <span style=\"color: maroon\">sec</span><span style=\"color: navy\">)</span><span style=\"color: navy\"> {\r\n</span>  it.answer first<b> as </b><span style=\"color: maroon\">a</span><b> then</b><span style=\"color: navy\"> {\r\n</span>    a.value<b> as </b><span style=\"color: maroon\">v</span><span style=\"color: navy\"><b> -&gt; </b></span>sec.text<b> as </b><span style=\"color: maroon\">narrative</span><b> then</b><span style=\"color: navy\"> {\r\n</span>      v<span style=\"color: navy\"><b> -&gt; </b></span>narrative.status = <span style=\"color: blue\">'generated'</span> <i>&quot;narrativeStatus&quot;</i><span style=\"color: navy\">;</span>\r\n      v<span style=\"color: navy\"><b> -&gt; </b></span>narrative.div = <b>create</b><span style=\"color: navy\">(</span><span style=\"color: blue\">'xhtml'</span><span style=\"color: navy\">)</span><b> as </b><span style=\"color: maroon\">xhtml</span><b> then</b><span style=\"color: navy\"> {\r\n</span>        v<span style=\"color: navy\"><b> -&gt; </b></span>xhtml.value = evaluate<span style=\"color: navy\">(</span>a<span style=\"color: navy\">, </span>'&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;p&gt;' + value.toString().escape('html') + '&lt;/p&gt;&lt;/div&gt;'<span style=\"color: navy\">)</span> <i>&quot;narrativeDiv&quot;</i><span style=\"color: navy\">;</span>\r\n      <span style=\"color: navy\">}</span> <i>&quot;narrativeXhtml&quot;</i><span style=\"color: navy\">;</span>\r\n    <span style=\"color: navy\">}</span> <i>&quot;sectionText&quot;</i><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">}</span> <i>&quot;sectionAnswer&quot;</i><span style=\"color: navy\">;</span>\r\n<span style=\"color: navy\">}\r\n\r\n</span><b>group </b>SetCompositionFullUrl<span style=\"color: navy\">(</span><b>source</b> <span style=\"color: maroon\">comp</span><span style=\"color: navy\"> : </span>Composition, <b>target</b> <span style=\"color: maroon\">entry</span><span style=\"color: navy\">)</span><span style=\"color: navy\"> {\r\n</span>  comp.id<b> as </b><span style=\"color: maroon\">id</span><span style=\"color: navy\"><b> -&gt; </b></span>entry.fullUrl = <b>append</b><span style=\"color: navy\">(</span><span style=\"color: blue\">'https://fhir.slade360.co.ke/fhir/Composition/'</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">id</span><span style=\"color: navy\">)</span> <i>&quot;assignFullUrl&quot;</i><span style=\"color: navy\">;</span>\r\n<span style=\"color: navy\">}\r\n\r\n</span><b>group </b>SetServiceRequestFullUrl<span style=\"color: navy\">(</span><b>source</b> <span style=\"color: maroon\">sr</span><span style=\"color: navy\"> : </span>ServiceRequest, <b>target</b> <span style=\"color: maroon\">entry</span><span style=\"color: navy\">)</span><span style=\"color: navy\"> {\r\n</span>  sr.id<b> as </b><span style=\"color: maroon\">id</span><span style=\"color: navy\"><b> -&gt; </b></span>entry.fullUrl = <b>append</b><span style=\"color: navy\">(</span><span style=\"color: blue\">'https://fhir.slade360.co.ke/fhir/ServiceRequest/'</span><span style=\"color: navy\">, </span><span style=\"color: maroon\">id</span><span style=\"color: navy\">)</span> <i>&quot;assignFullUrl&quot;</i><span style=\"color: navy\">;</span>\r\n<span style=\"color: navy\">}\r\n\r\n</span><span style=\"color: navy\">// </span><span style=\"color: green\">── Tenancy ──────────────────────────────────────────────────────────────────</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">Whose this is. The platform says which tenant a response belongs to with a</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">tag on the response, and every tag the response carries is copied onto</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">everything this map creates, as it is. Copied rather than named here, so the</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">map never has to know which tenant it is running for, nor which system the</span>\r\n<span style=\"color: navy\">// </span><span style=\"color: green\">tag is under: that system has moved more than once.</span>\r\n<b>group </b>CopyResponseTags<span style=\"color: navy\">(</span><b>source</b> <span style=\"color: maroon\">qr</span><span style=\"color: navy\"> : </span>QR, <b>target</b> <span style=\"color: maroon\">r</span><span style=\"color: navy\">)</span><span style=\"color: navy\"> {\r\n</span>  qr.meta<b> as </b><span style=\"color: maroon\">qm</span><b> then</b><span style=\"color: navy\"> {\r\n</span>    qm.tag<b> as </b><span style=\"color: maroon\">qt</span><span style=\"color: navy\"><b> -&gt; </b></span> r.meta<b> as </b><span style=\"color: maroon\">m</span><span style=\"color: navy\">, </span> m.tag = <span style=\"color: maroon\">qt</span> <i>&quot;copyTag&quot;</i><span style=\"color: navy\">;</span>\r\n  <span style=\"color: navy\">}</span> <i>&quot;tenantTags&quot;</i><span style=\"color: navy\">;</span>\r\n<span style=\"color: navy\">}\r\n\r\n</span></pre></div>"^^rdf:XMLLiteral ]
  ] ; # 
  fhir:url [ fhir:v "https://fhir.slade360.co.ke/fhir/StructureMap/ExtractInpatientDischargeNote"^^xsd:anyURI] ; # 
  fhir:version [ fhir:v "0.1.0"] ; # 
  fhir:name [ fhir:v "ExtractInpatientDischargeNote"] ; # 
  fhir:title [ fhir:v "Inpatient Discharge Note Extraction"] ; # 
  fhir:status [ fhir:v "active"] ; # 
  fhir:date [ fhir:v "2026-10-06T18:48:22+00:00"^^xsd:dateTime] ; # 
  fhir:publisher [ fhir:v "Kathurima Kimathi"] ; # 
  fhir:contact ( [
     fhir:name [ fhir:v "Kathurima Kimathi" ] ;
     fhir:telecom ( [
       fhir:system [ fhir:v "url" ] ;
       fhir:value [ fhir:v "https://www.linkedin.com/in/kathurima-kimathi/" ]
     ] [
       fhir:system [ fhir:v "email" ] ;
       fhir:value [ fhir:v "kathurimakimathi415@gmail.com" ]
     ] )
  ] [
     fhir:name [ fhir:v "Oscar John" ] ;
     fhir:telecom ( [
       fhir:system [ fhir:v "email" ] ;
       fhir:value [ fhir:v "oscarjohnotieno@gmail.com" ] ;
       fhir:use [ fhir:v "work" ]
     ] )
  ] [
     fhir:name [ fhir:v "Kennedy Omondi" ] ;
     fhir:telecom ( [
       fhir:system [ fhir:v "email" ] ;
       fhir:value [ fhir:v "kennankole@gmail.com" ] ;
       fhir:use [ fhir:v "work" ]
     ] )
  ] ) ; # 
  fhir:description [ fhir:v "Turns a response to the inpatient discharge note into a discharge-summary Composition and a ServiceRequest for the follow-up. Medication on discharge is referenced, never copied: those MedicationRequests already exist on the server -- the form picks them from it -- so they become section entries rather than new resources. The follow-up is intent `plan` rather than an Appointment because the form captures it as free text with no date to book against; give the item a date and an Appointment becomes the truer target. The Encounter is deliberately untouched: closing the stay -- Encounter.status and the discharge disposition -- belongs to the EMR's discharge workflow, which knows about bed release, billing and the actual moment the patient left. Writing a note is not the same event as being discharged, and a map that conflated them would close stays early."] ; # 
  fhir:structure ( [
     fhir:url [
       fhir:v "http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse"^^xsd:anyURI ;
       fhir:link <http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse>
     ] ;
     fhir:mode [ fhir:v "source" ] ;
     fhir:alias [ fhir:v "QR" ]
  ] [
     fhir:url [
       fhir:v "http://hl7.org/fhir/StructureDefinition/Bundle"^^xsd:anyURI ;
       fhir:link <http://hl7.org/fhir/StructureDefinition/Bundle>
     ] ;
     fhir:mode [ fhir:v "target" ] ;
     fhir:alias [ fhir:v "Bundle" ]
  ] [
     fhir:url [
       fhir:v "http://hl7.org/fhir/StructureDefinition/Composition"^^xsd:anyURI ;
       fhir:link <http://hl7.org/fhir/StructureDefinition/Composition>
     ] ;
     fhir:mode [ fhir:v "target" ] ;
     fhir:alias [ fhir:v "Composition" ]
  ] [
     fhir:url [
       fhir:v "http://hl7.org/fhir/StructureDefinition/ServiceRequest"^^xsd:anyURI ;
       fhir:link <http://hl7.org/fhir/StructureDefinition/ServiceRequest>
     ] ;
     fhir:mode [ fhir:v "target" ] ;
     fhir:alias [ fhir:v "ServiceRequest" ]
  ] ) ; # 
  fhir:group ( [
     fhir:name [ fhir:v "ExtractInpatientDischargeNote" ] ;
     fhir:input ( [
       fhir:name [ fhir:v "qr" ] ;
       fhir:type [ fhir:v "QR" ] ;
       fhir:mode [ fhir:v "source" ]
     ] [
       fhir:name [ fhir:v "bundle" ] ;
       fhir:type [ fhir:v "Bundle" ] ;
       fhir:mode [ fhir:v "target" ]
     ] ) ;
     fhir:rule ( [
       fhir:name [ fhir:v "setBundleType" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "bundle" ] ;
         fhir:element [ fhir:v "type" ] ;
         fhir:transform [ fhir:v "copy" ] ;
         fhir:parameter ( [
           fhir:value [
             a fhir:string ;
             fhir:v "transaction"
           ]
         ] )
       ] )
     ] [
       fhir:name [ fhir:v "composition" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "bundle" ] ;
         fhir:element [ fhir:v "entry" ] ;
         fhir:variable [ fhir:v "entry" ]
       ] [
         fhir:context [ fhir:v "entry" ] ;
         fhir:element [ fhir:v "resource" ] ;
         fhir:variable [ fhir:v "comp" ] ;
         fhir:transform [ fhir:v "create" ] ;
         fhir:parameter ( [
           fhir:value [
             a fhir:string ;
             fhir:v "Composition"
           ]
         ] )
       ] ) ;
       fhir:rule ( [
         fhir:name [ fhir:v "responseTags" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ]
         ] ) ;
         fhir:dependent ( [
           fhir:name [ fhir:v "CopyResponseTags" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:id ;
               fhir:v "qr"
             ]
           ] [
             fhir:value [
               a fhir:id ;
               fhir:v "comp"
             ]
           ] )
         ] )
       ] [
         fhir:name [ fhir:v "noteBase" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ]
         ] [
           fhir:context [ fhir:v "entry" ]
         ] ) ;
         fhir:dependent ( [
           fhir:name [ fhir:v "BuildNoteBase" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:id ;
               fhir:v "qr"
             ]
           ] [
             fhir:value [
               a fhir:id ;
               fhir:v "comp"
             ]
           ] [
             fhir:value [
               a fhir:id ;
               fhir:v "entry"
             ]
           ] )
         ] )
       ] [
         fhir:name [ fhir:v "noteName" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ] ;
           fhir:element [ fhir:v "name" ] ;
           fhir:transform [ fhir:v "copy" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:string ;
               fhir:v "InpatientDischargeNote"
             ]
           ] )
         ] )
       ] [
         fhir:name [ fhir:v "noteTitle" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ] ;
           fhir:element [ fhir:v "title" ] ;
           fhir:transform [ fhir:v "copy" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:string ;
               fhir:v "Discharge note"
             ]
           ] )
         ] )
       ] [
         fhir:name [ fhir:v "noteType" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ] ;
           fhir:element [ fhir:v "type" ] ;
           fhir:variable [ fhir:v "ty" ]
         ] ) ;
         fhir:rule ( [
           fhir:name [ fhir:v "sghiCoding" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "qr" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "ty" ] ;
             fhir:element [ fhir:v "coding" ] ;
             fhir:variable [ fhir:v "sghi" ]
           ] ) ;
           fhir:rule ( [
             fhir:name [ fhir:v "sghiSystem" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sghi" ] ;
               fhir:element [ fhir:v "system" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:string ;
                   fhir:v "https://fhir.slade360.co.ke/fhir/CodeSystem/inpatient-document-type-codesystem"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "sghiCode" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sghi" ] ;
               fhir:element [ fhir:v "code" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:string ;
                   fhir:v "discharge-note"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "sghiDisplay" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sghi" ] ;
               fhir:element [ fhir:v "display" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:string ;
                   fhir:v "Discharge note"
                 ]
               ] )
             ] )
           ] )
         ] [
           fhir:name [ fhir:v "loincCoding" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "qr" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "ty" ] ;
             fhir:element [ fhir:v "coding" ] ;
             fhir:variable [ fhir:v "loinc" ]
           ] ) ;
           fhir:rule ( [
             fhir:name [ fhir:v "loincSystem" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "loinc" ] ;
               fhir:element [ fhir:v "system" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:string ;
                   fhir:v "http://loinc.org"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "loincCode" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "loinc" ] ;
               fhir:element [ fhir:v "code" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:string ;
                   fhir:v "18842-5"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "loincDisplay" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "loinc" ] ;
               fhir:element [ fhir:v "display" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:string ;
                   fhir:v "Discharge summary"
                 ]
               ] )
             ] )
           ] )
         ] [
           fhir:name [ fhir:v "typeText" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "qr" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "ty" ] ;
             fhir:element [ fhir:v "text" ] ;
             fhir:transform [ fhir:v "copy" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:string ;
                 fhir:v "Discharge summary"
               ]
             ] )
           ] )
         ] )
       ] [
         fhir:name [ fhir:v "summaryRule" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ] ;
           fhir:element [ fhir:v "item" ] ;
           fhir:variable [ fhir:v "it" ] ;
           fhir:condition [ fhir:v "(linkId = 'summary') and answer.value.exists()" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ] ;
           fhir:element [ fhir:v "section" ] ;
           fhir:variable [ fhir:v "sec" ]
         ] ) ;
         fhir:rule ( [
           fhir:name [ fhir:v "summaryTitle" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "it" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "sec" ] ;
             fhir:element [ fhir:v "title" ] ;
             fhir:transform [ fhir:v "copy" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:string ;
                 fhir:v "Admission summary"
               ]
             ] )
           ] )
         ] [
           fhir:name [ fhir:v "summaryText" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "it" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "sec" ]
           ] ) ;
           fhir:dependent ( [
             fhir:name [ fhir:v "AddSectionText" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:id ;
                 fhir:v "it"
               ]
             ] [
               fhir:value [
                 a fhir:id ;
                 fhir:v "sec"
               ]
             ] )
           ] )
         ] )
       ] [
         fhir:name [ fhir:v "medsRule" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ] ;
           fhir:element [ fhir:v "item" ] ;
           fhir:variable [ fhir:v "itM" ] ;
           fhir:condition [ fhir:v "(linkId = 'medsOnDischarge') and answer.value.exists()" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ] ;
           fhir:element [ fhir:v "section" ] ;
           fhir:variable [ fhir:v "secM" ]
         ] ) ;
         fhir:rule ( [
           fhir:name [ fhir:v "medsTitle" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "itM" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "secM" ] ;
             fhir:element [ fhir:v "title" ] ;
             fhir:transform [ fhir:v "copy" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:string ;
                 fhir:v "Medication on discharge"
               ]
             ] )
           ] )
         ] [
           fhir:name [ fhir:v "medsAnswers" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "itM" ] ;
             fhir:element [ fhir:v "answer" ] ;
             fhir:variable [ fhir:v "aM" ]
           ] ) ;
           fhir:rule ( [
             fhir:name [ fhir:v "medsEntry" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "aM" ] ;
               fhir:element [ fhir:v "value" ] ;
               fhir:variable [ fhir:v "mv" ] ;
               fhir:condition [ fhir:v "(reference.exists() and (reference.contains('://').not() and reference.startsWith('#').not() and reference.startsWith('urn:').not())) or reference.empty()" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "secM" ] ;
               fhir:element [ fhir:v "entry" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:id ;
                   fhir:v "mv"
                 ]
               ] )
             ] )
           ] )
         ] ) ;
         fhir:documentation [ fhir:v "One section, one entry per medication. No MedicationRequest is created:\r\nthe form picked these off the server, and minting copies would double the\r\npatient's prescription list." ]
       ] [
         fhir:name [ fhir:v "followUpSectionRule" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ] ;
           fhir:element [ fhir:v "item" ] ;
           fhir:variable [ fhir:v "it" ] ;
           fhir:condition [ fhir:v "(linkId = 'followUp') and answer.value.exists()" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ] ;
           fhir:element [ fhir:v "section" ] ;
           fhir:variable [ fhir:v "sec" ]
         ] ) ;
         fhir:rule ( [
           fhir:name [ fhir:v "followUpTitle" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "it" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "sec" ] ;
             fhir:element [ fhir:v "title" ] ;
             fhir:transform [ fhir:v "copy" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:string ;
                 fhir:v "Follow-up"
               ]
             ] )
           ] )
         ] [
           fhir:name [ fhir:v "followUpText" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "it" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "sec" ]
           ] ) ;
           fhir:dependent ( [
             fhir:name [ fhir:v "AddSectionText" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:id ;
                 fhir:v "it"
               ]
             ] [
               fhir:value [
                 a fhir:id ;
                 fhir:v "sec"
               ]
             ] )
           ] )
         ] )
       ] [
         fhir:name [ fhir:v "adviceRule" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ] ;
           fhir:element [ fhir:v "item" ] ;
           fhir:variable [ fhir:v "it" ] ;
           fhir:condition [ fhir:v "(linkId = 'adviceGiven') and answer.value.exists()" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ] ;
           fhir:element [ fhir:v "section" ] ;
           fhir:variable [ fhir:v "sec" ]
         ] ) ;
         fhir:rule ( [
           fhir:name [ fhir:v "adviceTitle" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "it" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "sec" ] ;
             fhir:element [ fhir:v "title" ] ;
             fhir:transform [ fhir:v "copy" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:string ;
                 fhir:v "Advice given"
               ]
             ] )
           ] )
         ] [
           fhir:name [ fhir:v "adviceText" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "it" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "sec" ]
           ] ) ;
           fhir:dependent ( [
             fhir:name [ fhir:v "AddSectionText" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:id ;
                 fhir:v "it"
               ]
             ] [
               fhir:value [
                 a fhir:id ;
                 fhir:v "sec"
               ]
             ] )
           ] )
         ] )
       ] )
     ] [
       fhir:name [ fhir:v "followUpRequestRule" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ] ;
         fhir:element [ fhir:v "item" ] ;
         fhir:variable [ fhir:v "itF" ] ;
         fhir:condition [ fhir:v "(linkId = 'followUp') and answer.value.exists()" ]
       ] ) ;
       fhir:rule ( [
         fhir:name [ fhir:v "followUpAnswer" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "itF" ] ;
           fhir:element [ fhir:v "answer" ] ;
           fhir:listMode [ fhir:v "first" ] ;
           fhir:variable [ fhir:v "aF" ]
         ] ) ;
         fhir:rule ( [
           fhir:name [ fhir:v "serviceRequest" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "aF" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "bundle" ] ;
             fhir:element [ fhir:v "entry" ] ;
             fhir:variable [ fhir:v "entryS" ]
           ] [
             fhir:context [ fhir:v "entryS" ] ;
             fhir:element [ fhir:v "resource" ] ;
             fhir:variable [ fhir:v "sr" ] ;
             fhir:transform [ fhir:v "create" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:string ;
                 fhir:v "ServiceRequest"
               ]
             ] )
           ] ) ;
           fhir:rule ( [
             fhir:name [ fhir:v "responseTags" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ]
             ] ) ;
             fhir:dependent ( [
               fhir:name [ fhir:v "CopyResponseTags" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:id ;
                   fhir:v "qr"
                 ]
               ] [
                 fhir:value [
                   a fhir:id ;
                   fhir:v "sr"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "idAndFullUrl" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ] ;
               fhir:element [ fhir:v "id" ] ;
               fhir:transform [ fhir:v "uuid" ]
             ] ) ;
             fhir:dependent ( [
               fhir:name [ fhir:v "SetServiceRequestFullUrl" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:id ;
                   fhir:v "sr"
                 ]
               ] [
                 fhir:value [
                   a fhir:id ;
                   fhir:v "entryS"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "entryRequest" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "entryS" ] ;
               fhir:element [ fhir:v "request" ] ;
               fhir:variable [ fhir:v "request" ]
             ] ) ;
             fhir:rule ( [
               fhir:name [ fhir:v "requestMethod" ] ;
               fhir:source ( [
                 fhir:context [ fhir:v "qr" ]
               ] ) ;
               fhir:target ( [
                 fhir:context [ fhir:v "request" ] ;
                 fhir:element [ fhir:v "method" ] ;
                 fhir:transform [ fhir:v "copy" ] ;
                 fhir:parameter ( [
                   fhir:value [
                     a fhir:string ;
                     fhir:v "POST"
                   ]
                 ] )
               ] )
             ] [
               fhir:name [ fhir:v "requestUrl" ] ;
               fhir:source ( [
                 fhir:context [ fhir:v "qr" ]
               ] ) ;
               fhir:target ( [
                 fhir:context [ fhir:v "request" ] ;
                 fhir:element [ fhir:v "url" ] ;
                 fhir:transform [ fhir:v "copy" ] ;
                 fhir:parameter ( [
                   fhir:value [
                     a fhir:string ;
                     fhir:v "ServiceRequest"
                   ]
                 ] )
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "status" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ] ;
               fhir:element [ fhir:v "status" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:string ;
                   fhir:v "active"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "intent" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ] ;
               fhir:element [ fhir:v "intent" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:string ;
                   fhir:v "plan"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "priority" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ] ;
               fhir:element [ fhir:v "priority" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:string ;
                   fhir:v "routine"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "serviceRequestCode" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "aF" ] ;
               fhir:element [ fhir:v "value" ] ;
               fhir:variable [ fhir:v "v" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ] ;
               fhir:element [ fhir:v "code" ] ;
               fhir:variable [ fhir:v "codeRef" ]
             ] ) ;
             fhir:rule ( [
               fhir:name [ fhir:v "codeConcept" ] ;
               fhir:source ( [
                 fhir:context [ fhir:v "aF" ]
               ] ) ;
               fhir:target ( [
                 fhir:context [ fhir:v "codeRef" ] ;
                 fhir:element [ fhir:v "concept" ] ;
                 fhir:variable [ fhir:v "concept" ]
               ] ) ;
               fhir:rule ( [
                 fhir:name [ fhir:v "codeCoding" ] ;
                 fhir:source ( [
                   fhir:context [ fhir:v "aF" ]
                 ] ) ;
                 fhir:target ( [
                   fhir:context [ fhir:v "concept" ] ;
                   fhir:element [ fhir:v "coding" ] ;
                   fhir:variable [ fhir:v "coding" ]
                 ] ) ;
                 fhir:rule ( [
                   fhir:name [ fhir:v "codeSystem" ] ;
                   fhir:source ( [
                     fhir:context [ fhir:v "aF" ]
                   ] ) ;
                   fhir:target ( [
                     fhir:context [ fhir:v "coding" ] ;
                     fhir:element [ fhir:v "system" ] ;
                     fhir:transform [ fhir:v "copy" ] ;
                     fhir:parameter ( [
                       fhir:value [
                         a fhir:string ;
                         fhir:v "https://fhir.slade360.co.ke/fhir/CodeSystem/inpatient-clinical-concept-codesystem"
                       ]
                     ] )
                   ] )
                 ] [
                   fhir:name [ fhir:v "codeCode" ] ;
                   fhir:source ( [
                     fhir:context [ fhir:v "aF" ]
                   ] ) ;
                   fhir:target ( [
                     fhir:context [ fhir:v "coding" ] ;
                     fhir:element [ fhir:v "code" ] ;
                     fhir:transform [ fhir:v "copy" ] ;
                     fhir:parameter ( [
                       fhir:value [
                         a fhir:string ;
                         fhir:v "discharge-follow-up"
                       ]
                     ] )
                   ] )
                 ] [
                   fhir:name [ fhir:v "codeDisplay" ] ;
                   fhir:source ( [
                     fhir:context [ fhir:v "aF" ]
                   ] ) ;
                   fhir:target ( [
                     fhir:context [ fhir:v "coding" ] ;
                     fhir:element [ fhir:v "display" ] ;
                     fhir:transform [ fhir:v "copy" ] ;
                     fhir:parameter ( [
                       fhir:value [
                         a fhir:string ;
                         fhir:v "Follow-up arranged on discharge"
                       ]
                     ] )
                   ] )
                 ] )
               ] [
                 fhir:name [ fhir:v "codeText" ] ;
                 fhir:source ( [
                   fhir:context [ fhir:v "v" ]
                 ] ) ;
                 fhir:target ( [
                   fhir:context [ fhir:v "concept" ] ;
                   fhir:element [ fhir:v "text" ] ;
                   fhir:transform [ fhir:v "copy" ] ;
                   fhir:parameter ( [
                     fhir:value [
                       a fhir:id ;
                       fhir:v "v"
                     ]
                   ] )
                 ] )
               ] )
             ] ) ;
             fhir:documentation [ fhir:v "R5 ServiceRequest.code is a CodeableReference, so the concept sits one\r\nlevel down." ]
           ] [
             fhir:name [ fhir:v "subject" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ] ;
               fhir:element [ fhir:v "subject" ] ;
               fhir:variable [ fhir:v "s" ] ;
               fhir:condition [ fhir:v "(reference.exists() and (reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient')))" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ] ;
               fhir:element [ fhir:v "subject" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:id ;
                   fhir:v "s"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "encounter" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ] ;
               fhir:element [ fhir:v "encounter" ] ;
               fhir:variable [ fhir:v "e" ] ;
               fhir:condition [ fhir:v "(reference.exists() and (reference.startsWith('Encounter/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Encounter')))" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ] ;
               fhir:element [ fhir:v "encounter" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:id ;
                   fhir:v "e"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "authoredOn" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ] ;
               fhir:element [ fhir:v "authored" ] ;
               fhir:variable [ fhir:v "t" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ] ;
               fhir:element [ fhir:v "authoredOn" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:id ;
                   fhir:v "t"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "requesterFromAuthor" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ] ;
               fhir:element [ fhir:v "author" ] ;
               fhir:variable [ fhir:v "a" ] ;
               fhir:condition [ fhir:v "(reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient')))" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ] ;
               fhir:element [ fhir:v "requester" ] ;
               fhir:transform [ fhir:v "copy" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:id ;
                   fhir:v "a"
                 ]
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "requesterFallback" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ] ;
               fhir:condition [ fhir:v "author.where(((reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient'))))).exists().not()" ]
             ] ) ;
             fhir:rule ( [
               fhir:name [ fhir:v "requesterFromSource" ] ;
               fhir:source ( [
                 fhir:context [ fhir:v "qr" ] ;
                 fhir:element [ fhir:v "source" ] ;
                 fhir:variable [ fhir:v "src" ] ;
                 fhir:condition [ fhir:v "(reference.exists() and (reference.startsWith('Organization/') or reference.startsWith('Patient/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Organization') or ('/' + type).endsWith('/Patient')))" ]
               ] ) ;
               fhir:target ( [
                 fhir:context [ fhir:v "sr" ] ;
                 fhir:element [ fhir:v "requester" ] ;
                 fhir:transform [ fhir:v "copy" ] ;
                 fhir:parameter ( [
                   fhir:value [
                     a fhir:id ;
                     fhir:v "src"
                   ]
                 ] )
               ] )
             ] )
           ] [
             fhir:name [ fhir:v "linkQuestionnaireResponse" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "qr" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "sr" ] ;
               fhir:element [ fhir:v "supportingInfo" ] ;
               fhir:variable [ fhir:v "si" ]
             ] ) ;
             fhir:rule ( [
               fhir:name [ fhir:v "supportingInfoTarget" ] ;
               fhir:source ( [
                 fhir:context [ fhir:v "qr" ]
               ] ) ;
               fhir:target ( [
                 fhir:context [ fhir:v "si" ] ;
                 fhir:element [ fhir:v "reference" ] ;
                 fhir:variable [ fhir:v "ref" ] ;
                 fhir:transform [ fhir:v "create" ] ;
                 fhir:parameter ( [
                   fhir:value [
                     a fhir:string ;
                     fhir:v "Reference"
                   ]
                 ] )
               ] ) ;
               fhir:rule ( [
                 fhir:name [ fhir:v "supportingInfoRef" ] ;
                 fhir:source ( [
                   fhir:context [ fhir:v "qr" ] ;
                   fhir:element [ fhir:v "id" ] ;
                   fhir:variable [ fhir:v "qid" ]
                 ] ) ;
                 fhir:target ( [
                   fhir:context [ fhir:v "ref" ] ;
                   fhir:element [ fhir:v "reference" ] ;
                   fhir:transform [ fhir:v "copy" ] ;
                   fhir:parameter ( [
                     fhir:value [
                       a fhir:id ;
                       fhir:v "qid"
                     ]
                   ] )
                 ] )
               ] )
             ] )
           ] )
         ] )
       ] ) ;
       fhir:documentation [ fhir:v "The follow-up also leaves the document, so it can be worked rather than read." ]
     ] )
  ] [
     fhir:name [ fhir:v "BuildNoteBase" ] ;
     fhir:documentation [ fhir:v "─────────────────────────────────────────────────────────────────────────────\r\nShared note scaffolding. Repeated in each note map rather than imported: the\r\ntransform engine resolves `imports` against whatever StructureMaps happen to\r\nbe on the server, and a note that stops extracting because a shared map was\r\nnot uploaded is a worse failure than a duplicated group.\r\n─────────────────────────────────────────────────────────────────────────────\r\nTwo notes on what the publisher's StructureMap validator says about this file.\r\nSource parameters below are left untyped on purpose. Writing `: Element` makes\r\nthe validator compare QuestionnaireResponse.item against the literal 'Element'\r\nand report them as incompatible with each other, which is noise, not a finding.\r\nUntyped, it says only that it cannot check the paths.\r\n`evaluate(context, expression)` is flagged as \"takes 1 parameter but 2 were\r\nfound\". Leave it. The FML parser in the same jar refuses the one-argument form\r\noutright -- it wants a parameter, a comma, then the FHIRPath -- so the publisher\r\nis objecting to what its own compiler emits. Two arguments is the only form that\r\ncompiles, and it is what the R5 transform engine executes." ] ;
     fhir:input ( [
       fhir:name [ fhir:v "qr" ] ;
       fhir:type [ fhir:v "QR" ] ;
       fhir:mode [ fhir:v "source" ]
     ] [
       fhir:name [ fhir:v "comp" ] ;
       fhir:type [ fhir:v "Composition" ] ;
       fhir:mode [ fhir:v "target" ]
     ] [
       fhir:name [ fhir:v "entry" ] ;
       fhir:mode [ fhir:v "target" ]
     ] ) ;
     fhir:rule ( [
       fhir:name [ fhir:v "idAndFullUrl" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "comp" ] ;
         fhir:element [ fhir:v "id" ] ;
         fhir:transform [ fhir:v "uuid" ]
       ] ) ;
       fhir:dependent ( [
         fhir:name [ fhir:v "SetCompositionFullUrl" ] ;
         fhir:parameter ( [
           fhir:value [
             a fhir:id ;
             fhir:v "comp"
           ]
         ] [
           fhir:value [
             a fhir:id ;
             fhir:v "entry"
           ]
         ] )
       ] )
     ] [
       fhir:name [ fhir:v "entryRequest" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "entry" ] ;
         fhir:element [ fhir:v "request" ] ;
         fhir:variable [ fhir:v "request" ]
       ] ) ;
       fhir:rule ( [
         fhir:name [ fhir:v "requestMethod" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "request" ] ;
           fhir:element [ fhir:v "method" ] ;
           fhir:transform [ fhir:v "copy" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:string ;
               fhir:v "POST"
             ]
           ] )
         ] )
       ] [
         fhir:name [ fhir:v "requestUrl" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "request" ] ;
           fhir:element [ fhir:v "url" ] ;
           fhir:transform [ fhir:v "copy" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:string ;
               fhir:v "Composition"
             ]
           ] )
         ] )
       ] )
     ] [
       fhir:name [ fhir:v "status" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "comp" ] ;
         fhir:element [ fhir:v "status" ] ;
         fhir:transform [ fhir:v "copy" ] ;
         fhir:parameter ( [
           fhir:value [
             a fhir:string ;
             fhir:v "final"
           ]
         ] )
       ] )
     ] [
       fhir:name [ fhir:v "subject" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ] ;
         fhir:element [ fhir:v "subject" ] ;
         fhir:variable [ fhir:v "s" ] ;
         fhir:condition [ fhir:v "(reference.exists() and (reference.contains('://').not() and reference.startsWith('#').not() and reference.startsWith('urn:').not())) or reference.empty()" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "comp" ] ;
         fhir:element [ fhir:v "subject" ] ;
         fhir:transform [ fhir:v "copy" ] ;
         fhir:parameter ( [
           fhir:value [
             a fhir:id ;
             fhir:v "s"
           ]
         ] )
       ] )
     ] [
       fhir:name [ fhir:v "encounter" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ] ;
         fhir:element [ fhir:v "encounter" ] ;
         fhir:variable [ fhir:v "e" ] ;
         fhir:condition [ fhir:v "(reference.exists() and (reference.startsWith('Encounter/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Encounter')))" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "comp" ] ;
         fhir:element [ fhir:v "encounter" ] ;
         fhir:transform [ fhir:v "copy" ] ;
         fhir:parameter ( [
           fhir:value [
             a fhir:id ;
             fhir:v "e"
           ]
         ] )
       ] )
     ] [
       fhir:name [ fhir:v "date" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ] ;
         fhir:element [ fhir:v "authored" ] ;
         fhir:variable [ fhir:v "t" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "comp" ] ;
         fhir:element [ fhir:v "date" ] ;
         fhir:transform [ fhir:v "copy" ] ;
         fhir:parameter ( [
           fhir:value [
             a fhir:id ;
             fhir:v "t"
           ]
         ] )
       ] ) ;
       fhir:documentation [ fhir:v "Composition.date is 1..1. `authored` is only 0..1 on the response, so when a\r\nclient sends none the extraction time stands in. It is a poor substitute for\r\nwhen the note was written, but a document with no date at all is worse, and\r\nextraction happens moments after submission in practice." ]
     ] [
       fhir:name [ fhir:v "dateFallback" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ] ;
         fhir:condition [ fhir:v "authored.exists().not()" ]
       ] ) ;
       fhir:rule ( [
         fhir:name [ fhir:v "dateFromExtractionTime" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ] ;
           fhir:element [ fhir:v "date" ] ;
           fhir:transform [ fhir:v "evaluate" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:id ;
               fhir:v "qr"
             ]
           ] [
             fhir:value [
               a fhir:string ;
               fhir:v "now()"
             ]
           ] )
         ] )
       ] )
     ] [
       fhir:name [ fhir:v "authorFromAuthor" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ] ;
         fhir:element [ fhir:v "author" ] ;
         fhir:variable [ fhir:v "a" ] ;
         fhir:condition [ fhir:v "(reference.exists() and (reference.startsWith('Patient/') or reference.startsWith('Organization/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient') or ('/' + type).endsWith('/Organization')))" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "comp" ] ;
         fhir:element [ fhir:v "author" ] ;
         fhir:transform [ fhir:v "copy" ] ;
         fhir:parameter ( [
           fhir:value [
             a fhir:id ;
             fhir:v "a"
           ]
         ] )
       ] ) ;
       fhir:documentation [ fhir:v "Composition.author is 1..1 minimum. R5 QuestionnaireResponse carries both\r\n`author` (who recorded the answers) and `source` (who supplied them); the\r\nauthor is the one who wrote the note, so it wins, and `source` only stands\r\nin when the client did not send an author. If the response carries neither,\r\nthe note comes out unauthored -- there is nothing on a QuestionnaireResponse\r\nto invent one from, and guessing at who wrote a clinical note is not a thing\r\na map should do." ]
     ] [
       fhir:name [ fhir:v "authorFallback" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ] ;
         fhir:condition [ fhir:v "author.where(((reference.exists() and (reference.startsWith('Patient/') or reference.startsWith('Organization/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient') or ('/' + type).endsWith('/Organization'))))).exists().not()" ]
       ] ) ;
       fhir:rule ( [
         fhir:name [ fhir:v "authorFromSource" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ] ;
           fhir:element [ fhir:v "source" ] ;
           fhir:variable [ fhir:v "src" ] ;
           fhir:condition [ fhir:v "(reference.exists() and (reference.startsWith('Patient/') or reference.startsWith('Organization/'))) or (reference.empty() and (type.empty() or ('/' + type).endsWith('/Patient') or ('/' + type).endsWith('/Organization')))" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "comp" ] ;
           fhir:element [ fhir:v "author" ] ;
           fhir:transform [ fhir:v "copy" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:id ;
               fhir:v "src"
             ]
           ] )
         ] )
       ] )
     ] [
       fhir:name [ fhir:v "linkQuestionnaireResponse" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "comp" ] ;
         fhir:element [ fhir:v "relatesTo" ] ;
         fhir:variable [ fhir:v "rel" ]
       ] ) ;
       fhir:rule ( [
         fhir:name [ fhir:v "relatesToType" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "rel" ] ;
           fhir:element [ fhir:v "type" ] ;
           fhir:transform [ fhir:v "copy" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:string ;
               fhir:v "derived-from"
             ]
           ] )
         ] )
       ] [
         fhir:name [ fhir:v "relatesToTarget" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qr" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "rel" ] ;
           fhir:element [ fhir:v "resourceReference" ] ;
           fhir:variable [ fhir:v "ref" ] ;
           fhir:transform [ fhir:v "create" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:string ;
               fhir:v "Reference"
             ]
           ] )
         ] ) ;
         fhir:rule ( [
           fhir:name [ fhir:v "relatesToRef" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "qr" ] ;
             fhir:element [ fhir:v "id" ] ;
             fhir:variable [ fhir:v "qid" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "ref" ] ;
             fhir:element [ fhir:v "reference" ] ;
             fhir:transform [ fhir:v "copy" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:id ;
                 fhir:v "qid"
               ]
             ] )
           ] )
         ] )
       ] )
     ] )
  ] [
     fhir:name [ fhir:v "AddSectionText" ] ;
     fhir:input ( [
       fhir:name [ fhir:v "it" ] ;
       fhir:mode [ fhir:v "source" ]
     ] [
       fhir:name [ fhir:v "sec" ] ;
       fhir:mode [ fhir:v "target" ]
     ] ) ;
     fhir:rule ( [
       fhir:name [ fhir:v "sectionAnswer" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "it" ] ;
         fhir:element [ fhir:v "answer" ] ;
         fhir:listMode [ fhir:v "first" ] ;
         fhir:variable [ fhir:v "a" ]
       ] ) ;
       fhir:rule ( [
         fhir:name [ fhir:v "sectionText" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "a" ] ;
           fhir:element [ fhir:v "value" ] ;
           fhir:variable [ fhir:v "v" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "sec" ] ;
           fhir:element [ fhir:v "text" ] ;
           fhir:variable [ fhir:v "narrative" ]
         ] ) ;
         fhir:rule ( [
           fhir:name [ fhir:v "narrativeStatus" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "v" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "narrative" ] ;
             fhir:element [ fhir:v "status" ] ;
             fhir:transform [ fhir:v "copy" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:string ;
                 fhir:v "generated"
               ]
             ] )
           ] )
         ] [
           fhir:name [ fhir:v "narrativeXhtml" ] ;
           fhir:source ( [
             fhir:context [ fhir:v "v" ]
           ] ) ;
           fhir:target ( [
             fhir:context [ fhir:v "narrative" ] ;
             fhir:element [ fhir:v "div" ] ;
             fhir:variable [ fhir:v "xhtml" ] ;
             fhir:transform [ fhir:v "create" ] ;
             fhir:parameter ( [
               fhir:value [
                 a fhir:string ;
                 fhir:v "xhtml"
               ]
             ] )
           ] ) ;
           fhir:rule ( [
             fhir:name [ fhir:v "narrativeDiv" ] ;
             fhir:source ( [
               fhir:context [ fhir:v "v" ]
             ] ) ;
             fhir:target ( [
               fhir:context [ fhir:v "xhtml" ] ;
               fhir:element [ fhir:v "value" ] ;
               fhir:transform [ fhir:v "evaluate" ] ;
               fhir:parameter ( [
                 fhir:value [
                   a fhir:id ;
                   fhir:v "a"
                 ]
               ] [
                 fhir:value [
                   a fhir:string ;
                   fhir:v "'<div xmlns=\"http://www.w3.org/1999/xhtml\"><p>' + value.toString().escape('html') + '</p></div>'"
                 ]
               ] )
             ] )
           ] )
         ] )
       ] )
     ] )
  ] [
     fhir:name [ fhir:v "SetCompositionFullUrl" ] ;
     fhir:input ( [
       fhir:name [ fhir:v "comp" ] ;
       fhir:type [ fhir:v "Composition" ] ;
       fhir:mode [ fhir:v "source" ]
     ] [
       fhir:name [ fhir:v "entry" ] ;
       fhir:mode [ fhir:v "target" ]
     ] ) ;
     fhir:rule ( [
       fhir:name [ fhir:v "assignFullUrl" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "comp" ] ;
         fhir:element [ fhir:v "id" ] ;
         fhir:variable [ fhir:v "id" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "entry" ] ;
         fhir:element [ fhir:v "fullUrl" ] ;
         fhir:transform [ fhir:v "append" ] ;
         fhir:parameter ( [
           fhir:value [
             a fhir:string ;
             fhir:v "https://fhir.slade360.co.ke/fhir/Composition/"
           ]
         ] [
           fhir:value [
             a fhir:id ;
             fhir:v "id"
           ]
         ] )
       ] )
     ] )
  ] [
     fhir:name [ fhir:v "SetServiceRequestFullUrl" ] ;
     fhir:input ( [
       fhir:name [ fhir:v "sr" ] ;
       fhir:type [ fhir:v "ServiceRequest" ] ;
       fhir:mode [ fhir:v "source" ]
     ] [
       fhir:name [ fhir:v "entry" ] ;
       fhir:mode [ fhir:v "target" ]
     ] ) ;
     fhir:rule ( [
       fhir:name [ fhir:v "assignFullUrl" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "sr" ] ;
         fhir:element [ fhir:v "id" ] ;
         fhir:variable [ fhir:v "id" ]
       ] ) ;
       fhir:target ( [
         fhir:context [ fhir:v "entry" ] ;
         fhir:element [ fhir:v "fullUrl" ] ;
         fhir:transform [ fhir:v "append" ] ;
         fhir:parameter ( [
           fhir:value [
             a fhir:string ;
             fhir:v "https://fhir.slade360.co.ke/fhir/ServiceRequest/"
           ]
         ] [
           fhir:value [
             a fhir:id ;
             fhir:v "id"
           ]
         ] )
       ] )
     ] )
  ] [
     fhir:name [ fhir:v "CopyResponseTags" ] ;
     fhir:documentation [ fhir:v "── Tenancy ──────────────────────────────────────────────────────────────────\r\nWhose this is. The platform says which tenant a response belongs to with a\r\ntag on the response, and every tag the response carries is copied onto\r\neverything this map creates, as it is. Copied rather than named here, so the\r\nmap never has to know which tenant it is running for, nor which system the\r\ntag is under: that system has moved more than once." ] ;
     fhir:input ( [
       fhir:name [ fhir:v "qr" ] ;
       fhir:type [ fhir:v "QR" ] ;
       fhir:mode [ fhir:v "source" ]
     ] [
       fhir:name [ fhir:v "r" ] ;
       fhir:mode [ fhir:v "target" ]
     ] ) ;
     fhir:rule ( [
       fhir:name [ fhir:v "tenantTags" ] ;
       fhir:source ( [
         fhir:context [ fhir:v "qr" ] ;
         fhir:element [ fhir:v "meta" ] ;
         fhir:variable [ fhir:v "qm" ]
       ] ) ;
       fhir:rule ( [
         fhir:name [ fhir:v "copyTag" ] ;
         fhir:source ( [
           fhir:context [ fhir:v "qm" ] ;
           fhir:element [ fhir:v "tag" ] ;
           fhir:variable [ fhir:v "qt" ]
         ] ) ;
         fhir:target ( [
           fhir:context [ fhir:v "r" ] ;
           fhir:element [ fhir:v "meta" ] ;
           fhir:variable [ fhir:v "m" ]
         ] [
           fhir:context [ fhir:v "m" ] ;
           fhir:element [ fhir:v "tag" ] ;
           fhir:transform [ fhir:v "copy" ] ;
           fhir:parameter ( [
             fhir:value [
               a fhir:id ;
               fhir:v "qt"
             ]
           ] )
         ] )
       ] )
     ] )
  ] ) . #