【问题标题】:How to test for a non-existent element in Schematron如何在 Schematron 中测试不存在的元素
【发布时间】:2016-03-21 16:12:25
【问题描述】:

是我自己还是不可能通过 Schematron 寻找不存在的元素。我似乎也找不到任何关于此的文档。

采取以下规则:

    <sch:rule context="/A/B/C[@TYPE='TEST1']" id="identifier-required">
    identifier must be present
        <sch:assert test="not(.)" id="identifier-required">
    identifier-required: identifier must be present
        </sch:assert>
    </sch:rule>

并将其应用于以下文档:

<A>
   <B>
      <C TYPE="TEST2">TEST</C>
      <C TYPE="TEST3">TEST</C>
   </B>
</A>

理论上这应该会失败,但我发现它不会。有人知道这是否是正确的行为吗?

【问题讨论】:

    标签: xml xslt schematron


    【解决方案1】:

    当然可以检查 Schematron 中是否缺少元素。

    您的断言不会失败,因为它的规则上下文不匹配。

    如果您的规则匹配,那么. 必然存在,因此&lt;sch:assert test="not(.)"&gt; 无论如何都不会通过。

    您可以改为将上下文设置为 C 的父级,然后断言这样的 C 不作为子级存在:

    <sch:rule context="/A/B" id="identifier-required">
        <sch:assert test="not(C[@TYPE='TEST1'])" id="identifier-required">
          identifier-required: identifier must be present
        </sch:assert>
    </sch:rule>
    

    但您的诊断信息表明您确实希望断言存在这样的C,所以您真正想要的是:

    <sch:rule context="/A/B" id="identifier-required">
        <sch:assert test="C[@TYPE='TEST1']" id="identifier-required">
          identifier-required: identifier must be present
        </sch:assert>
    </sch:rule>
    

    如果您给定的 XML 带有消息“需要标识符:标识符必须存在”,这将失败。

    【讨论】:

      【解决方案2】:

      上下文从不匹配任何内容的规则永远不会触发,也永远不会报告错误。您需要使用确实存在的某些上下文(例如“/”)定义一个规则,并断言在该上下文中,必须至少有一个由表达式 A/B/C[@TYPE='TEST1'] 选择的节点

      【讨论】:

        猜你喜欢
        • 2013-12-01
        • 2021-03-04
        • 2014-05-11
        • 1970-01-01
        • 2011-07-29
        • 2011-12-04
        • 1970-01-01
        • 2013-11-11
        • 2017-09-18
        相关资源
        最近更新 更多