Schematron - 基于其位置的元素验证

Schematron - Element validation based on its position

我正在使用 Schematron 进行一些业务规则验证。我的 xml 数据如下:

<labtests>  
    <test>
        <observation>
            <code code="TT900" name="NMCK"/>
            <outcome value="074042"/>
        </observation>            
    </test>

    <test>
        <observation>
            <code code="TT500" name="LVCT"/>
            <outcome value="852417"/>
        </observation>            
    </test>
    <test>
        <observation>
            <code code="TT500" name="LVCT"/>
            <outcome value="36542"/>
        </observation>            
    </test>
    <test>
        <observation>
            <code code="TT100" name="GVMC"/>
            <outcome value="874541"/>
        </observation>            
    </test>
    <test>
        <observation>
            <code code="TT500" name="LVCT"/>
            <outcome value="369521"/>
        </observation>            
    </test>
</labtests>

当前上下文设置为 labtests/test/observation,如下所示:

<iso:rule context="labtests/test/observation">
    <!--perform all validations here-->


</iso:rule>               

我想对具有 code/@code="TT500".

的第一个 <observation> 块的 <outcome> 节点执行一些特殊的业务验证检查

我想我可以使用以下表达式来获取第一个预期 <observation> 块的位置

count(../../test/observation/code[@code="TT500"]/preceding-sibling::*)+1

但我不知道如何将此位置与当前上下文中的节点进行比较以进行特殊验证。

更新:

为了简单起见,我们假设在这种情况下要执行的特殊验证是 outcome/@value 的长度必须大于或等于 6。即

<iso:report test="not(string-length(outcome/@value) >= 6">
    outcome/@value should have at least 6 characters for the first TT500 observation
</iso:report>

以下 Schematron 文档完全符合您的要求。 assertreport 之间没有真正的区别,您可以颠倒任何规则以适应两者。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
    <pattern>
        <rule context="observation[code/@code = 'TT500' and not(preceding::observation[code/@code = 'TT500'])]">
            <assert test="string-length(outcome/@value) ge 6"> outcome/@value should have at least 6 characters for the first TT500 observation </assert>
        </rule>
    </pattern>
</schema>

当使用此 SCH 规则验证以下(无效)XML 文档时:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="sample.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<labtests>  
    <test>
        <observation>
            <code code="TT900" name="NMCK"/>
            <outcome value="07442"/>
        </observation>            
    </test>
    <test>
        <observation>
            <code code="TT500" name="LVCT"/>
            <outcome value="85417"/>
        </observation>            
    </test>
    <test>
        <observation>
            <code code="TT500" name="LVCT"/>
            <outcome value="36542"/>
        </observation>            
    </test>           
</labtests>

Schematron 处理器将按照

的方式发出警告
E [ISO Schematron] outcome/@value should have at least 6 characters for the first TT500 observation