根据条件将节点分组在一起,并使用Schematron进行验证



我正在使用Schematron编写规则,以验证下面的数据。要求是核实患者在过去12个月内是否至少有一次遭遇。如果每个患者有多次遭遇,请使用最后一次遭遇。

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

上述数据应该通过验证,因为最近一次遭遇不到一年前。我想知道的是,如果我写了一个模板,按照患者id将遇到的情况分组在一起,有没有办法将该模板传递到规则上下文?如果没有,还有其他方法吗?我对xslt和Schematron都是全新的,以下是我迄今为止所拥有的:

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

使用XSLT3底层可以使用

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

或者输出更详细的信息,只处理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>

我不认为您可以像代码尝试的那样自由地混合Schematron和XSLT,您需要设置一个XProc管道来使用p:xslt对原始输入进行分组,然后使用Schematron进行验证。

至于使用nodeschematron运行第二个示例的问题,它使用了一个XPath实现,该实现似乎不支持XPath 3.1sort函数,node schematron也无法将映射作为schematron变量的中间结果进行处理,因此似乎只将所有映射都填充到一个变量表达式中即可;两个例子:

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

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

如果您需要一个失败的断言,那么用替换sch:report

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

最新更新