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

标签 xml xslt xpath xslt-2.0 schematron

我正在使用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>               


我想在具有<outcome>的第一个<observation>块的code/@code="TT500"节点上执行一些特殊的业务验证检查。

我想我可以使用以下表达式来获取第一个预期的<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

关于xml - Schematron-基于其位置的元素验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29234302/

相关文章:

javascript - 对 <xsl :template> 中的 <div> 的引用

xml - Web 开发人员没有广泛使用浏览器端 XSLT 的原因是什么?

java - 解析 BPEL 文件以提取 Activity + XPath

android - AutoResizeTextView 未按预期工作

xml - XSLT 多重替换

java - 撒克逊 XSLT 转换 : How to change serialization of an empty tag from <x/> to <x></x>?

python - 使用 Python 的 XPATH 解析 XML

使用 idref 的 XPath 表达式

java - E/运动识别管理器 : mSContextService causing app to crash on my android phone

c++ - QT XML 阅读器每次读取相同的标签