xsltschematron

Group nodes together based on condition and validate using Schematron


I am working on writing rules using Schematron to validate data below. The requirement is to verify whether a patient has at least one encounter in the past 12 months. If there are multiple encounters per patient, use the last encounter.

<root>
   <entry>
    <resource>
      <resourceType>Encounter</resourceType>
      <subject>
        <id>Patient/12345</id>
      </subject>
      <encounterDate>2018-04-10T10:00:00</encounterDate>
    </resource>
  </entry>
   <entry>
    <resource>
      <resourceType>Encounter</resourceType>
      <subject>
        <id>Patient/abcde</id>
      </subject>
      <encounterDate>2020-04-10T10:00:00</encounterDate>
    </resource>
  </entry>
  <entry>
   <resource>
      <resourceType>Encounter</resourceType>
      <subject>
        <id>Patient/abcde</id>
      </subject>
      <encounterDate>2019-05-10T10:00:00</encounterDate>
    </resource>
  </entry>
</root>

The above data should pass the validation because the latest encounter is less than a year ago. What I want to know is, if I write a template that groups encounters together by patient id, is there a way to pass that template to the rule context? If not, is there any other way of doing it? I am completely new to both xslt and Schematron and here is what I have so far:

<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
   <pattern>
   <key name="patientId" match="entry" use="/resouce/subject/id/text()"/>
   <template name="dateByPatient" match="entry">
   <root>
     <for-each select="resource/subject/id">
       <patient >
         <for-each select="key('patientId',text())">
           <effectiveDateTime><value-of select="./resource/encounterDate"/></effectiveDateTime>
          </for-each>
       </patient>
     </for-each>
     </root>
   </template>
   <let name="template">
    <dateByPatient/>
   </let>
   <let name="latest">
   <root>
   <for-each select="$template/root/patient">
   <patient >
    <sort select="effectiveDateTime" order="descending" />
       <if test="position() = 1">
       <effectiveDateTime><value-of select="effectiveDateTime" /></effectiveDateTime>
       </if>
    </patient>
   </for-each>
     </root>
   </let>
     <rule context="$latest/root/patient/effectiveDateTime">
     <let name="days" value="days-from-duration(fn:current-dateTime() - xs:dateTime(text()))" />
     <assert test="days-from-duration(fn:current-dateTime() - xs:dateTime(text())) &lt; 365">
       Encounter date more than a year : <value-of select="$days" /> days 
     </assert>
   </rule>
 </pattern>
</schema>

Solution

  • With XSLT 3 underlying you could use

    <?xml version="1.0" encoding="UTF-8"?>
    <sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
        xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
        <sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
        <sch:pattern>
            <sch:rule context="root">
                <sch:let name="groups"
                    value="let $encounter-resources := entry/resource[resourceType = 'Encounter']
                           return map:merge( 
                             $encounter-resources
                             ! 
                             map { 
                               data(subject/id) : xs:dateTime(encounterDate) 
                             }, 
                             map { 'duplicates' : 'combine' }
                           )"/>
                <sch:assert 
                    test="every $patient in map:keys($groups) 
                          satisfies 
                          (current-dateTime() - max($groups($patient))) 
                          lt xs:dayTimeDuration('P365D')">At least one patient with latest encounter more than a year ago.</sch:assert>
            </sch:rule>
        </sch:pattern>
    </sch:schema>
    

    Or to output more detailed information and to only process resources with type Encounter:

    <?xml version="1.0" encoding="UTF-8"?>
    <sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
        xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
        <sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
        <sch:pattern>
            <sch:rule context="root">
                <sch:let name="groups"
                    value="let $encounter-resources := entry/resource[resourceType = 'Encounter']
                           return map:merge( 
                             $encounter-resources 
                             ! 
                             map { 
                               data(subject/id) : xs:dateTime(encounterDate) 
                             }, 
                             map { 'duplicates' : 'combine' }
                           )"/>
                <sch:let name="failing-patients"
                    value="map:keys($groups)[(current-dateTime() - max($groups(.))) gt xs:dayTimeDuration('P365D')]"/>
                <sch:report 
                    test="exists($failing-patients)">Patients <sch:value-of select="$failing-patients"/> with latest encounter more than a year ago.</sch:report>
            </sch:rule>
        </sch:pattern>
    </sch:schema>
    

    I don't think you can mix Schematron and XSLT as freely as your code tries, you would need to set up an XProc pipeline to use p:xslt to group the original input and then a validation step to validate with Schematron.

    As for your problems to run the second sample with node-schematron, it uses an XPath implementation that doesn't support the XPath 3.1 sort function it seems, node-schematron also fails to handle maps as intermediary results of a Schematron variable, so only stuffing all into one variable expression seems to do; two examples work:

    <sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
        xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
        <sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
        <sch:pattern>
            <sch:rule context="root">
                <sch:let name="failing-patients"
                    value="let $encounter-resources := entry/resource[resourceType = 'Encounter'],
                             $groups := map:merge( 
                             $encounter-resources
                             ! 
                             map { 
                               data(subject/id) : xs:dateTime(encounterDate) 
                             }, 
                             map { 'duplicates' : 'combine' }
                           )
                           return map:keys($groups)[(current-dateTime() - max($groups(.))) gt xs:dayTimeDuration('P365D')]"/>
    
                <sch:report 
                    test="exists($failing-patients)">Patients <sch:value-of select="$failing-patients"/> with latest encounter more than a year ago.</sch:report>
            </sch:rule>
        </sch:pattern>
    </sch:schema>
    

    or

    <sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
        xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
        <sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
        <sch:pattern>
            <sch:rule context="root">
                <sch:let name="failing-patients"
                    value="let 
                             $encounter-resources := entry/resource[resourceType = 'Encounter'],
                             $groups := fold-left(
                                $encounter-resources, 
                                map{}, 
                                function($m, $e) { 
                                    map:put(
                                        $m, 
                                        data($e/subject/id),
                                        max((xs:dateTime($e/encounterDate), map:get($m, data($e/subject/id))))
                                    )
                                })
                          return map:keys($groups)[(current-dateTime() - $groups(.)) gt xs:dayTimeDuration('P365D')]"/>
                <sch:report test="exists($failing-patients)">Patients <sch:value-of
                        select="$failing-patients"/> with latest encounter more than a year
                    ago.</sch:report>
            </sch:rule>
        </sch:pattern>
    </sch:schema>
    

    If you need an assertion that fails then replace the sch:report with

    <sch:assert 
                test="empty($failing-patients)">Patients <sch:value-of select="$failing-patients"/> with latest encounter more than a year ago.</sch:assert>