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 - XML Representation

Active as of 2026-09-14

Raw xml | Download


<StructureMap xmlns="http://hl7.org/fhir">
  <id value="ExtractInpatientDischargeNote"/>
  <text>
    <status value="generated"/>
    <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">
<span style="color: #cc00cc">/// <b>url</b> = </span><span style="color: blue">'https://fhir.slade360.co.ke/fhir/StructureMap/ExtractInpatientDischargeNote'</span>
<span style="color: #cc00cc">/// <b>name</b> = </span><span style="color: blue">'ExtractInpatientDischargeNote'</span>
<span style="color: #cc00cc">/// <b>title</b> = </span><span style="color: blue">'Inpatient Discharge Note Extraction'</span>
<span style="color: #cc00cc">/// <b>status</b> = </span><span style="color: blue">'active'</span>
<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>

<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>
<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>
<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>
<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>

<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"> {
</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>
  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"> {
</span>    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>
    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>
    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>
    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"> {
</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"> {
</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>
        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>
        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>
      <span style="color: navy">}</span> <i>&quot;sghiCoding&quot;</i><span style="color: navy">;</span>
      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"> {
</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>
        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>
        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>
      <span style="color: navy">}</span> <i>&quot;loincCoding&quot;</i><span style="color: navy">;</span>
      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>
    <span style="color: navy">}</span> <i>&quot;noteType&quot;</i><span style="color: navy">;</span>
    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"> {
</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>
      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>
    <span style="color: navy">}</span> <i>&quot;summaryRule&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">// </span><span style="color: green">One section, one entry per medication. No MedicationRequest is created:</span>
    <span style="color: navy">// </span><span style="color: green">the form picked these off the server, and minting copies would double the</span>
    <span style="color: navy">// </span><span style="color: green">patient's prescription list.</span>
    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"> {
</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>
      itM.answer<b> as </b><span style="color: maroon">aM</span><b> then</b><span style="color: navy"> {
</span>        aM.value<b> as </b><span style="color: maroon">mv</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>
      <span style="color: navy">}</span> <i>&quot;medsAnswers&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;medsRule&quot;</i><span style="color: navy">;</span>
    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"> {
</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>
      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>
    <span style="color: navy">}</span> <i>&quot;followUpSectionRule&quot;</i><span style="color: navy">;</span>
    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"> {
</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>
      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>
    <span style="color: navy">}</span> <i>&quot;adviceRule&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;composition&quot;</i><span style="color: navy">;</span>
  <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>
  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"> {
</span>    itF.answer first<b> as </b><span style="color: maroon">aF</span><b> then</b><span style="color: navy"> {
</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"> {
</span>        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>
        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"> {
</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>
          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>
        <span style="color: navy">}</span> <i>&quot;entryRequest&quot;</i><span style="color: navy">;</span>
        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>
        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>
        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>
        <span style="color: navy">// </span><span style="color: green">R5 ServiceRequest.code is a CodeableReference, so the concept sits one</span>
        <span style="color: navy">// </span><span style="color: green">level down.</span>
        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"> {
</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"> {
</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"> {
</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>
              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>
              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>
            <span style="color: navy">}</span> <i>&quot;codeCoding&quot;</i><span style="color: navy">;</span>
            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>
          <span style="color: navy">}</span> <i>&quot;codeConcept&quot;</i><span style="color: navy">;</span>
        <span style="color: navy">}</span> <i>&quot;serviceRequestCode&quot;</i><span style="color: navy">;</span>
        qr.subject<b> as </b><span style="color: maroon">s</span><span style="color: navy"><b> -&gt; </b></span>sr.subject = <span style="color: maroon">s</span><span style="color: navy">;</span>
        qr.encounter<b> as </b><span style="color: maroon">e</span><span style="color: navy"><b> -&gt; </b></span>sr.encounter = <span style="color: maroon">e</span><span style="color: navy">;</span>
        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>
        qr.author<b> as </b><span style="color: maroon">a</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>
        qr<b> where </b><span style="color: navy">(</span>author.exists().not()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>          qr.source<b> as </b><span style="color: maroon">src</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>
        <span style="color: navy">}</span> <i>&quot;requesterFallback&quot;</i><span style="color: navy">;</span>
        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"> {
</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"> {
</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>
          <span style="color: navy">}</span> <i>&quot;supportingInfoTarget&quot;</i><span style="color: navy">;</span>
        <span style="color: navy">}</span> <i>&quot;linkQuestionnaireResponse&quot;</i><span style="color: navy">;</span>
      <span style="color: navy">}</span> <i>&quot;serviceRequest&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;followUpAnswer&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;followUpRequestRule&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</span><span style="color: navy">// </span><span style="color: green">─────────────────────────────────────────────────────────────────────────────</span>
<span style="color: navy">// </span><span style="color: green">Shared note scaffolding. Repeated in each note map rather than imported: the</span>
<span style="color: navy">// </span><span style="color: green">transform engine resolves `imports` against whatever StructureMaps happen to</span>
<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>
<span style="color: navy">// </span><span style="color: green">not uploaded is a worse failure than a duplicated group.</span>
<span style="color: navy">// </span><span style="color: green">─────────────────────────────────────────────────────────────────────────────</span>
<span style="color: navy">// </span><span style="color: green">Two notes on what the publisher's StructureMap validator says about this file.</span>
<span style="color: navy">// </span><span style="color: green">Source parameters below are left untyped on purpose. Writing `: Element` makes</span>
<span style="color: navy">// </span><span style="color: green">the validator compare QuestionnaireResponse.item against the literal 'Element'</span>
<span style="color: navy">// </span><span style="color: green">and report them as incompatible with each other, which is noise, not a finding.</span>
<span style="color: navy">// </span><span style="color: green">Untyped, it says only that it cannot check the paths.</span>
<span style="color: navy">// </span><span style="color: green">`evaluate(context, expression)` is flagged as &quot;takes 1 parameter but 2 were</span>
<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>
<span style="color: navy">// </span><span style="color: green">outright -- it wants a parameter, a comma, then the FHIRPath -- so the publisher</span>
<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>
<span style="color: navy">// </span><span style="color: green">compiles, and it is what the R5 transform engine executes.</span>
<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"> {
</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>
  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"> {
</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>
    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>
  <span style="color: navy">}</span> <i>&quot;entryRequest&quot;</i><span style="color: navy">;</span>
  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>
  qr.subject<b> as </b><span style="color: maroon">s</span><span style="color: navy"><b> -&gt; </b></span>comp.subject = <span style="color: maroon">s</span><span style="color: navy">;</span>
  qr.encounter<b> as </b><span style="color: maroon">e</span><span style="color: navy"><b> -&gt; </b></span>comp.encounter = <span style="color: maroon">e</span><span style="color: navy">;</span>
  <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>
  <span style="color: navy">// </span><span style="color: green">client sends none the extraction time stands in. It is a poor substitute for</span>
  <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>
  <span style="color: navy">// </span><span style="color: green">extraction happens moments after submission in practice.</span>
  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>
  qr<b> where </b><span style="color: navy">(</span>authored.exists().not()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</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>
  <span style="color: navy">}</span> <i>&quot;dateFallback&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">// </span><span style="color: green">Composition.author is 1..1 minimum. R5 QuestionnaireResponse carries both</span>
  <span style="color: navy">// </span><span style="color: green">`author` (who recorded the answers) and `source` (who supplied them); the</span>
  <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>
  <span style="color: navy">// </span><span style="color: green">in when the client did not send an author. If the response carries neither,</span>
  <span style="color: navy">// </span><span style="color: green">the note comes out unauthored -- there is nothing on a QuestionnaireResponse</span>
  <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>
  <span style="color: navy">// </span><span style="color: green">a map should do.</span>
  qr.author<b> as </b><span style="color: maroon">a</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>
  qr<b> where </b><span style="color: navy">(</span>author.exists().not()<span style="color: navy">)</span><b> then</b><span style="color: navy"> {
</span>    qr.source<b> as </b><span style="color: maroon">src</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>
  <span style="color: navy">}</span> <i>&quot;authorFallback&quot;</i><span style="color: navy">;</span>
  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"> {
</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>
    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"> {
</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>
    <span style="color: navy">}</span> <i>&quot;relatesToTarget&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;linkQuestionnaireResponse&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</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"> {
</span>  it.answer first<b> as </b><span style="color: maroon">a</span><b> then</b><span style="color: navy"> {
</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"> {
</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>
      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"> {
</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>
      <span style="color: navy">}</span> <i>&quot;narrativeXhtml&quot;</i><span style="color: navy">;</span>
    <span style="color: navy">}</span> <i>&quot;sectionText&quot;</i><span style="color: navy">;</span>
  <span style="color: navy">}</span> <i>&quot;sectionAnswer&quot;</i><span style="color: navy">;</span>
<span style="color: navy">}

</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"> {
</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>
<span style="color: navy">}

</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"> {
</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>
<span style="color: navy">}

</span></pre></div>
  </text>
  <url
       value="https://fhir.slade360.co.ke/fhir/StructureMap/ExtractInpatientDischargeNote"/>
  <version value="0.1.0"/>
  <name value="ExtractInpatientDischargeNote"/>
  <title value="Inpatient Discharge Note Extraction"/>
  <status value="active"/>
  <date value="2026-09-14T06:31:13+00:00"/>
  <publisher value="Kathurima Kimathi"/>
  <contact>
    <name value="Kathurima Kimathi"/>
    <telecom>
      <system value="url"/>
      <value value="https://www.linkedin.com/in/kathurima-kimathi/"/>
    </telecom>
    <telecom>
      <system value="email"/>
      <value value="kathurimakimathi415@gmail.com"/>
    </telecom>
  </contact>
  <contact>
    <name value="Oscar John"/>
    <telecom>
      <system value="email"/>
      <value value="oscarjohnotieno@gmail.com"/>
      <use value="work"/>
    </telecom>
  </contact>
  <contact>
    <name value="Kennedy Omondi"/>
    <telecom>
      <system value="email"/>
      <value value="kennankole@gmail.com"/>
      <use value="work"/>
    </telecom>
  </contact>
  <description
               value="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."/>
  <structure>
    <url
         value="http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse"/>
    <mode value="source"/>
    <alias value="QR"/>
  </structure>
  <structure>
    <url value="http://hl7.org/fhir/StructureDefinition/Bundle"/>
    <mode value="target"/>
    <alias value="Bundle"/>
  </structure>
  <structure>
    <url value="http://hl7.org/fhir/StructureDefinition/Composition"/>
    <mode value="target"/>
    <alias value="Composition"/>
  </structure>
  <structure>
    <url value="http://hl7.org/fhir/StructureDefinition/ServiceRequest"/>
    <mode value="target"/>
    <alias value="ServiceRequest"/>
  </structure>
  <group>
    <name value="ExtractInpatientDischargeNote"/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="bundle"/>
      <type value="Bundle"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="setBundleType"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="bundle"/>
        <element value="type"/>
        <transform value="copy"/>
        <parameter>
          <valueString value="transaction"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="composition"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="bundle"/>
        <element value="entry"/>
        <variable value="entry"/>
      </target>
      <target>
        <context value="entry"/>
        <element value="resource"/>
        <variable value="comp"/>
        <transform value="create"/>
        <parameter>
          <valueString value="Composition"/>
        </parameter>
      </target>
      <rule>
        <name value="noteBase"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="comp"/>
        </target>
        <target>
          <context value="entry"/>
        </target>
        <dependent>
          <name value="BuildNoteBase"/>
          <parameter>
            <valueId value="qr"/>
          </parameter>
          <parameter>
            <valueId value="comp"/>
          </parameter>
          <parameter>
            <valueId value="entry"/>
          </parameter>
        </dependent>
      </rule>
      <rule>
        <name value="noteName"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="comp"/>
          <element value="name"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="InpatientDischargeNote"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="noteTitle"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="comp"/>
          <element value="title"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="Discharge note"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="noteType"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="comp"/>
          <element value="type"/>
          <variable value="ty"/>
        </target>
        <rule>
          <name value="sghiCoding"/>
          <source>
            <context value="qr"/>
          </source>
          <target>
            <context value="ty"/>
            <element value="coding"/>
            <variable value="sghi"/>
          </target>
          <rule>
            <name value="sghiSystem"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="sghi"/>
              <element value="system"/>
              <transform value="copy"/>
              <parameter>
                <valueString
                             value="https://fhir.slade360.co.ke/fhir/CodeSystem/inpatient-document-type-codesystem"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="sghiCode"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="sghi"/>
              <element value="code"/>
              <transform value="copy"/>
              <parameter>
                <valueString value="discharge-note"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="sghiDisplay"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="sghi"/>
              <element value="display"/>
              <transform value="copy"/>
              <parameter>
                <valueString value="Discharge note"/>
              </parameter>
            </target>
          </rule>
        </rule>
        <rule>
          <name value="loincCoding"/>
          <source>
            <context value="qr"/>
          </source>
          <target>
            <context value="ty"/>
            <element value="coding"/>
            <variable value="loinc"/>
          </target>
          <rule>
            <name value="loincSystem"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="loinc"/>
              <element value="system"/>
              <transform value="copy"/>
              <parameter>
                <valueString value="http://loinc.org"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="loincCode"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="loinc"/>
              <element value="code"/>
              <transform value="copy"/>
              <parameter>
                <valueString value="18842-5"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="loincDisplay"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="loinc"/>
              <element value="display"/>
              <transform value="copy"/>
              <parameter>
                <valueString value="Discharge summary"/>
              </parameter>
            </target>
          </rule>
        </rule>
        <rule>
          <name value="typeText"/>
          <source>
            <context value="qr"/>
          </source>
          <target>
            <context value="ty"/>
            <element value="text"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Discharge summary"/>
            </parameter>
          </target>
        </rule>
      </rule>
      <rule>
        <name value="summaryRule"/>
        <source>
          <context value="qr"/>
          <element value="item"/>
          <variable value="it"/>
          <condition value="(linkId = 'summary') and answer.value.exists()"/>
        </source>
        <target>
          <context value="comp"/>
          <element value="section"/>
          <variable value="sec"/>
        </target>
        <rule>
          <name value="summaryTitle"/>
          <source>
            <context value="it"/>
          </source>
          <target>
            <context value="sec"/>
            <element value="title"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Admission summary"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="summaryText"/>
          <source>
            <context value="it"/>
          </source>
          <target>
            <context value="sec"/>
          </target>
          <dependent>
            <name value="AddSectionText"/>
            <parameter>
              <valueId value="it"/>
            </parameter>
            <parameter>
              <valueId value="sec"/>
            </parameter>
          </dependent>
        </rule>
      </rule>
      <rule>
        <name value="medsRule"/>
        <source>
          <context value="qr"/>
          <element value="item"/>
          <variable value="itM"/>
          <condition
                     value="(linkId = 'medsOnDischarge') and answer.value.exists()"/>
        </source>
        <target>
          <context value="comp"/>
          <element value="section"/>
          <variable value="secM"/>
        </target>
        <rule>
          <name value="medsTitle"/>
          <source>
            <context value="itM"/>
          </source>
          <target>
            <context value="secM"/>
            <element value="title"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Medication on discharge"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="medsAnswers"/>
          <source>
            <context value="itM"/>
            <element value="answer"/>
            <variable value="aM"/>
          </source>
          <rule>
            <name value="medsEntry"/>
            <source>
              <context value="aM"/>
              <element value="value"/>
              <variable value="mv"/>
            </source>
            <target>
              <context value="secM"/>
              <element value="entry"/>
              <transform value="copy"/>
              <parameter>
                <valueId value="mv"/>
              </parameter>
            </target>
          </rule>
        </rule>
        <documentation
                       value="One section, one entry per medication. No MedicationRequest is created:
the form picked these off the server, and minting copies would double the
patient's prescription list."/>
      </rule>
      <rule>
        <name value="followUpSectionRule"/>
        <source>
          <context value="qr"/>
          <element value="item"/>
          <variable value="it"/>
          <condition value="(linkId = 'followUp') and answer.value.exists()"/>
        </source>
        <target>
          <context value="comp"/>
          <element value="section"/>
          <variable value="sec"/>
        </target>
        <rule>
          <name value="followUpTitle"/>
          <source>
            <context value="it"/>
          </source>
          <target>
            <context value="sec"/>
            <element value="title"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Follow-up"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="followUpText"/>
          <source>
            <context value="it"/>
          </source>
          <target>
            <context value="sec"/>
          </target>
          <dependent>
            <name value="AddSectionText"/>
            <parameter>
              <valueId value="it"/>
            </parameter>
            <parameter>
              <valueId value="sec"/>
            </parameter>
          </dependent>
        </rule>
      </rule>
      <rule>
        <name value="adviceRule"/>
        <source>
          <context value="qr"/>
          <element value="item"/>
          <variable value="it"/>
          <condition
                     value="(linkId = 'adviceGiven') and answer.value.exists()"/>
        </source>
        <target>
          <context value="comp"/>
          <element value="section"/>
          <variable value="sec"/>
        </target>
        <rule>
          <name value="adviceTitle"/>
          <source>
            <context value="it"/>
          </source>
          <target>
            <context value="sec"/>
            <element value="title"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="Advice given"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="adviceText"/>
          <source>
            <context value="it"/>
          </source>
          <target>
            <context value="sec"/>
          </target>
          <dependent>
            <name value="AddSectionText"/>
            <parameter>
              <valueId value="it"/>
            </parameter>
            <parameter>
              <valueId value="sec"/>
            </parameter>
          </dependent>
        </rule>
      </rule>
    </rule>
    <rule>
      <name value="followUpRequestRule"/>
      <source>
        <context value="qr"/>
        <element value="item"/>
        <variable value="itF"/>
        <condition value="(linkId = 'followUp') and answer.value.exists()"/>
      </source>
      <rule>
        <name value="followUpAnswer"/>
        <source>
          <context value="itF"/>
          <element value="answer"/>
          <listMode value="first"/>
          <variable value="aF"/>
        </source>
        <rule>
          <name value="serviceRequest"/>
          <source>
            <context value="aF"/>
          </source>
          <target>
            <context value="bundle"/>
            <element value="entry"/>
            <variable value="entryS"/>
          </target>
          <target>
            <context value="entryS"/>
            <element value="resource"/>
            <variable value="sr"/>
            <transform value="create"/>
            <parameter>
              <valueString value="ServiceRequest"/>
            </parameter>
          </target>
          <rule>
            <name value="idAndFullUrl"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="sr"/>
              <element value="id"/>
              <transform value="uuid"/>
            </target>
            <dependent>
              <name value="SetServiceRequestFullUrl"/>
              <parameter>
                <valueId value="sr"/>
              </parameter>
              <parameter>
                <valueId value="entryS"/>
              </parameter>
            </dependent>
          </rule>
          <rule>
            <name value="entryRequest"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="entryS"/>
              <element value="request"/>
              <variable value="request"/>
            </target>
            <rule>
              <name value="requestMethod"/>
              <source>
                <context value="qr"/>
              </source>
              <target>
                <context value="request"/>
                <element value="method"/>
                <transform value="copy"/>
                <parameter>
                  <valueString value="POST"/>
                </parameter>
              </target>
            </rule>
            <rule>
              <name value="requestUrl"/>
              <source>
                <context value="qr"/>
              </source>
              <target>
                <context value="request"/>
                <element value="url"/>
                <transform value="copy"/>
                <parameter>
                  <valueString value="ServiceRequest"/>
                </parameter>
              </target>
            </rule>
          </rule>
          <rule>
            <name value="status"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="sr"/>
              <element value="status"/>
              <transform value="copy"/>
              <parameter>
                <valueString value="active"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="intent"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="sr"/>
              <element value="intent"/>
              <transform value="copy"/>
              <parameter>
                <valueString value="plan"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="priority"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="sr"/>
              <element value="priority"/>
              <transform value="copy"/>
              <parameter>
                <valueString value="routine"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="serviceRequestCode"/>
            <source>
              <context value="aF"/>
              <element value="value"/>
              <variable value="v"/>
            </source>
            <target>
              <context value="sr"/>
              <element value="code"/>
              <variable value="codeRef"/>
            </target>
            <rule>
              <name value="codeConcept"/>
              <source>
                <context value="aF"/>
              </source>
              <target>
                <context value="codeRef"/>
                <element value="concept"/>
                <variable value="concept"/>
              </target>
              <rule>
                <name value="codeCoding"/>
                <source>
                  <context value="aF"/>
                </source>
                <target>
                  <context value="concept"/>
                  <element value="coding"/>
                  <variable value="coding"/>
                </target>
                <rule>
                  <name value="codeSystem"/>
                  <source>
                    <context value="aF"/>
                  </source>
                  <target>
                    <context value="coding"/>
                    <element value="system"/>
                    <transform value="copy"/>
                    <parameter>
                      <valueString
                                   value="https://fhir.slade360.co.ke/fhir/CodeSystem/inpatient-clinical-concept-codesystem"/>
                    </parameter>
                  </target>
                </rule>
                <rule>
                  <name value="codeCode"/>
                  <source>
                    <context value="aF"/>
                  </source>
                  <target>
                    <context value="coding"/>
                    <element value="code"/>
                    <transform value="copy"/>
                    <parameter>
                      <valueString value="discharge-follow-up"/>
                    </parameter>
                  </target>
                </rule>
                <rule>
                  <name value="codeDisplay"/>
                  <source>
                    <context value="aF"/>
                  </source>
                  <target>
                    <context value="coding"/>
                    <element value="display"/>
                    <transform value="copy"/>
                    <parameter>
                      <valueString value="Follow-up arranged on discharge"/>
                    </parameter>
                  </target>
                </rule>
              </rule>
              <rule>
                <name value="codeText"/>
                <source>
                  <context value="v"/>
                </source>
                <target>
                  <context value="concept"/>
                  <element value="text"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueId value="v"/>
                  </parameter>
                </target>
              </rule>
            </rule>
            <documentation
                           value="R5 ServiceRequest.code is a CodeableReference, so the concept sits one
level down."/>
          </rule>
          <rule>
            <name value="subject"/>
            <source>
              <context value="qr"/>
              <element value="subject"/>
              <variable value="s"/>
            </source>
            <target>
              <context value="sr"/>
              <element value="subject"/>
              <transform value="copy"/>
              <parameter>
                <valueId value="s"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="encounter"/>
            <source>
              <context value="qr"/>
              <element value="encounter"/>
              <variable value="e"/>
            </source>
            <target>
              <context value="sr"/>
              <element value="encounter"/>
              <transform value="copy"/>
              <parameter>
                <valueId value="e"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="authoredOn"/>
            <source>
              <context value="qr"/>
              <element value="authored"/>
              <variable value="t"/>
            </source>
            <target>
              <context value="sr"/>
              <element value="authoredOn"/>
              <transform value="copy"/>
              <parameter>
                <valueId value="t"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="requesterFromAuthor"/>
            <source>
              <context value="qr"/>
              <element value="author"/>
              <variable value="a"/>
            </source>
            <target>
              <context value="sr"/>
              <element value="requester"/>
              <transform value="copy"/>
              <parameter>
                <valueId value="a"/>
              </parameter>
            </target>
          </rule>
          <rule>
            <name value="requesterFallback"/>
            <source>
              <context value="qr"/>
              <condition value="author.exists().not()"/>
            </source>
            <rule>
              <name value="requesterFromSource"/>
              <source>
                <context value="qr"/>
                <element value="source"/>
                <variable value="src"/>
              </source>
              <target>
                <context value="sr"/>
                <element value="requester"/>
                <transform value="copy"/>
                <parameter>
                  <valueId value="src"/>
                </parameter>
              </target>
            </rule>
          </rule>
          <rule>
            <name value="linkQuestionnaireResponse"/>
            <source>
              <context value="qr"/>
            </source>
            <target>
              <context value="sr"/>
              <element value="supportingInfo"/>
              <variable value="si"/>
            </target>
            <rule>
              <name value="supportingInfoTarget"/>
              <source>
                <context value="qr"/>
              </source>
              <target>
                <context value="si"/>
                <element value="reference"/>
                <variable value="ref"/>
                <transform value="create"/>
                <parameter>
                  <valueString value="Reference"/>
                </parameter>
              </target>
              <rule>
                <name value="supportingInfoRef"/>
                <source>
                  <context value="qr"/>
                  <element value="id"/>
                  <variable value="qid"/>
                </source>
                <target>
                  <context value="ref"/>
                  <element value="reference"/>
                  <transform value="copy"/>
                  <parameter>
                    <valueId value="qid"/>
                  </parameter>
                </target>
              </rule>
            </rule>
          </rule>
        </rule>
      </rule>
      <documentation
                     value="The follow-up also leaves the document, so it can be worked rather than read."/>
    </rule>
  </group>
  <group>
    <name value="BuildNoteBase"/>
    <documentation
                   value="─────────────────────────────────────────────────────────────────────────────
Shared note scaffolding. Repeated in each note map rather than imported: the
transform engine resolves `imports` against whatever StructureMaps happen to
be on the server, and a note that stops extracting because a shared map was
not uploaded is a worse failure than a duplicated group.
─────────────────────────────────────────────────────────────────────────────
Two notes on what the publisher's StructureMap validator says about this file.
Source parameters below are left untyped on purpose. Writing `: Element` makes
the validator compare QuestionnaireResponse.item against the literal 'Element'
and report them as incompatible with each other, which is noise, not a finding.
Untyped, it says only that it cannot check the paths.
`evaluate(context, expression)` is flagged as &quot;takes 1 parameter but 2 were
found&quot;. Leave it. The FML parser in the same jar refuses the one-argument form
outright -- it wants a parameter, a comma, then the FHIRPath -- so the publisher
is objecting to what its own compiler emits. Two arguments is the only form that
compiles, and it is what the R5 transform engine executes."/>
    <input>
      <name value="qr"/>
      <type value="QR"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="comp"/>
      <type value="Composition"/>
      <mode value="target"/>
    </input>
    <input>
      <name value="entry"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="idAndFullUrl"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="comp"/>
        <element value="id"/>
        <transform value="uuid"/>
      </target>
      <dependent>
        <name value="SetCompositionFullUrl"/>
        <parameter>
          <valueId value="comp"/>
        </parameter>
        <parameter>
          <valueId value="entry"/>
        </parameter>
      </dependent>
    </rule>
    <rule>
      <name value="entryRequest"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="entry"/>
        <element value="request"/>
        <variable value="request"/>
      </target>
      <rule>
        <name value="requestMethod"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="request"/>
          <element value="method"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="POST"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="requestUrl"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="request"/>
          <element value="url"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="Composition"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="status"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="comp"/>
        <element value="status"/>
        <transform value="copy"/>
        <parameter>
          <valueString value="final"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="subject"/>
      <source>
        <context value="qr"/>
        <element value="subject"/>
        <variable value="s"/>
      </source>
      <target>
        <context value="comp"/>
        <element value="subject"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="s"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="encounter"/>
      <source>
        <context value="qr"/>
        <element value="encounter"/>
        <variable value="e"/>
      </source>
      <target>
        <context value="comp"/>
        <element value="encounter"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="e"/>
        </parameter>
      </target>
    </rule>
    <rule>
      <name value="date"/>
      <source>
        <context value="qr"/>
        <element value="authored"/>
        <variable value="t"/>
      </source>
      <target>
        <context value="comp"/>
        <element value="date"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="t"/>
        </parameter>
      </target>
      <documentation
                     value="Composition.date is 1..1. `authored` is only 0..1 on the response, so when a
client sends none the extraction time stands in. It is a poor substitute for
when the note was written, but a document with no date at all is worse, and
extraction happens moments after submission in practice."/>
    </rule>
    <rule>
      <name value="dateFallback"/>
      <source>
        <context value="qr"/>
        <condition value="authored.exists().not()"/>
      </source>
      <rule>
        <name value="dateFromExtractionTime"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="comp"/>
          <element value="date"/>
          <transform value="evaluate"/>
          <parameter>
            <valueId value="qr"/>
          </parameter>
          <parameter>
            <valueString value="now()"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="authorFromAuthor"/>
      <source>
        <context value="qr"/>
        <element value="author"/>
        <variable value="a"/>
      </source>
      <target>
        <context value="comp"/>
        <element value="author"/>
        <transform value="copy"/>
        <parameter>
          <valueId value="a"/>
        </parameter>
      </target>
      <documentation
                     value="Composition.author is 1..1 minimum. R5 QuestionnaireResponse carries both
`author` (who recorded the answers) and `source` (who supplied them); the
author is the one who wrote the note, so it wins, and `source` only stands
in when the client did not send an author. If the response carries neither,
the note comes out unauthored -- there is nothing on a QuestionnaireResponse
to invent one from, and guessing at who wrote a clinical note is not a thing
a map should do."/>
    </rule>
    <rule>
      <name value="authorFallback"/>
      <source>
        <context value="qr"/>
        <condition value="author.exists().not()"/>
      </source>
      <rule>
        <name value="authorFromSource"/>
        <source>
          <context value="qr"/>
          <element value="source"/>
          <variable value="src"/>
        </source>
        <target>
          <context value="comp"/>
          <element value="author"/>
          <transform value="copy"/>
          <parameter>
            <valueId value="src"/>
          </parameter>
        </target>
      </rule>
    </rule>
    <rule>
      <name value="linkQuestionnaireResponse"/>
      <source>
        <context value="qr"/>
      </source>
      <target>
        <context value="comp"/>
        <element value="relatesTo"/>
        <variable value="rel"/>
      </target>
      <rule>
        <name value="relatesToType"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="rel"/>
          <element value="type"/>
          <transform value="copy"/>
          <parameter>
            <valueString value="derived-from"/>
          </parameter>
        </target>
      </rule>
      <rule>
        <name value="relatesToTarget"/>
        <source>
          <context value="qr"/>
        </source>
        <target>
          <context value="rel"/>
          <element value="resourceReference"/>
          <variable value="ref"/>
          <transform value="create"/>
          <parameter>
            <valueString value="Reference"/>
          </parameter>
        </target>
        <rule>
          <name value="relatesToRef"/>
          <source>
            <context value="qr"/>
            <element value="id"/>
            <variable value="qid"/>
          </source>
          <target>
            <context value="ref"/>
            <element value="reference"/>
            <transform value="copy"/>
            <parameter>
              <valueId value="qid"/>
            </parameter>
          </target>
        </rule>
      </rule>
    </rule>
  </group>
  <group>
    <name value="AddSectionText"/>
    <input>
      <name value="it"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="sec"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="sectionAnswer"/>
      <source>
        <context value="it"/>
        <element value="answer"/>
        <listMode value="first"/>
        <variable value="a"/>
      </source>
      <rule>
        <name value="sectionText"/>
        <source>
          <context value="a"/>
          <element value="value"/>
          <variable value="v"/>
        </source>
        <target>
          <context value="sec"/>
          <element value="text"/>
          <variable value="narrative"/>
        </target>
        <rule>
          <name value="narrativeStatus"/>
          <source>
            <context value="v"/>
          </source>
          <target>
            <context value="narrative"/>
            <element value="status"/>
            <transform value="copy"/>
            <parameter>
              <valueString value="generated"/>
            </parameter>
          </target>
        </rule>
        <rule>
          <name value="narrativeXhtml"/>
          <source>
            <context value="v"/>
          </source>
          <target>
            <context value="narrative"/>
            <element value="div"/>
            <variable value="xhtml"/>
            <transform value="create"/>
            <parameter>
              <valueString value="xhtml"/>
            </parameter>
          </target>
          <rule>
            <name value="narrativeDiv"/>
            <source>
              <context value="v"/>
            </source>
            <target>
              <context value="xhtml"/>
              <element value="value"/>
              <transform value="evaluate"/>
              <parameter>
                <valueId value="a"/>
              </parameter>
              <parameter>
                <valueString
                             value="'&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;'"/>
              </parameter>
            </target>
          </rule>
        </rule>
      </rule>
    </rule>
  </group>
  <group>
    <name value="SetCompositionFullUrl"/>
    <input>
      <name value="comp"/>
      <type value="Composition"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="entry"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="assignFullUrl"/>
      <source>
        <context value="comp"/>
        <element value="id"/>
        <variable value="id"/>
      </source>
      <target>
        <context value="entry"/>
        <element value="fullUrl"/>
        <transform value="append"/>
        <parameter>
          <valueString value="https://fhir.slade360.co.ke/fhir/Composition/"/>
        </parameter>
        <parameter>
          <valueId value="id"/>
        </parameter>
      </target>
    </rule>
  </group>
  <group>
    <name value="SetServiceRequestFullUrl"/>
    <input>
      <name value="sr"/>
      <type value="ServiceRequest"/>
      <mode value="source"/>
    </input>
    <input>
      <name value="entry"/>
      <mode value="target"/>
    </input>
    <rule>
      <name value="assignFullUrl"/>
      <source>
        <context value="sr"/>
        <element value="id"/>
        <variable value="id"/>
      </source>
      <target>
        <context value="entry"/>
        <element value="fullUrl"/>
        <transform value="append"/>
        <parameter>
          <valueString
                       value="https://fhir.slade360.co.ke/fhir/ServiceRequest/"/>
        </parameter>
        <parameter>
          <valueId value="id"/>
        </parameter>
      </target>
    </rule>
  </group>
</StructureMap>