微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决根据条件将节点分组在一起,并使用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>

解决方法

有了XSLT 3基础,您可以使用

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

对于使用node-schematron运行第二个示例时遇到的问题,它使用了似乎不支持XPath 3.1 sort函数的XPath实现,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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?