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

Xml Schematron-基于位置的元素验证,xml,xslt,xpath,xslt-2.0,schematron,Xml,Xslt,Xpath,Xslt 2.0,Schematron,我正在使用Schematron进行一些业务规则验证。我的xml数据如下所示: <labtests> <test> <observation> <code code="TT900" name="NMCK"/> <outcome value="074042"/> </observation> </te

我正在使用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>
我想对具有
code/@code=“TT500”
的第一个
块在
节点上执行一些特殊的业务验证检查

我想我可以使用下面的表达式来获得第一个预期的
块的位置

count(../../test/observation/code[@code="TT500"]/preceding-sibling::*)+1
但我不知道如何将此位置与当前上下文中的节点进行比较,以执行特殊验证

更新:

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

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

对于第一次TT500观察,结果/@值应至少包含6个字符

以下Schematron文档完全按照您的要求执行。
assert
report
之间没有真正的区别,您可以反转任何规则以适应两者

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

请解释您的断言或规则应该做什么,它涉及哪些节点,以及它应该断言或报告什么情况(即什么是允许的,什么是不允许的)。更好的办法是:出示两份文件,一份有效,另一份无效。@MathiasMüller请看更新后的问题。效果很好。从不知道可以在
节点中应用过滤器。我真傻。@AJQarshi,不客气。
sch:rule
中的值实际上可以是any,它只是所有可能表达式的子集。
<?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>
<?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>
E [ISO Schematron] outcome/@value should have at least 6 characters for the first TT500 observation