【问题标题】:Schematron rule fails Pattern '' Failed : [123] Picture obligatorySchematron 规则失败 Pattern '' Failed : [123] 图片必填
【发布时间】:2020-11-24 15:54:27
【问题描述】:

我正在对 XML 文件使用 schematron 验证。我尝试编写我的第一条规则,并使用在线工具https://www.liquid-technologies.com/online-schematron-validator 进行检查。这条规则有什么问题?我想要做的是验证图片节点是否存在,如果它存在,它是否有一些文本,所以它不能是这样的:<Picture/>,而是<Picture>aasd</Picture>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Message>
  <book>
    <chapterA>
      <SectionA>
        <Picture>picture</Picture>
      </SectionA>
    </chapterA>
  </book>
</Message>

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2">
  <title>Myrule</title>
  <pattern>
    <rule context="Message">
      <assert test="boolean(/*[local-name() = 'book']/*[local-name() = 'chapterA']/*[local-name() = 'SectionA']/*[local-name() = 'Picture'])" flag="fatal" id="id123">[123] Picture obligatory</assert>
    </rule>
  </pattern>
</schema>

【问题讨论】:

    标签: xml xsd xml-validation schematron


    【解决方案1】:

    我不知道 schematron 是什么,但查看规则时我认为 xpath 不正确。如果你改变了

    <assert test="boolean(/*[local-name() = 'book']/*[local-name() = 'chapterA']/*[local-name() = 'SectionA']/*[local-name() = 'Picture'])" flag="fatal" id="id123">[123] Picture obligatory</assert>
    

    <assert test="boolean(/Message/*[local-name() = 'book']/*[local-name() = 'chapterA']/*[local-name() = 'SectionA']/*[local-name() = 'Picture'])" flag="fatal" id="id123">[123] Picture obligatory</assert>
    

    它有效。

    但是您的 xpath 不会检查图片元素是否为空。我也觉得可以简化成这样

    <assert test="boolean(/Message/book/chapterA/SectionA/Picture/text())" flag="fatal" id="id123">[123] Picture obligatory</assert>
    

    当图片元素为空时,这将抛出你定义的致命错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-28
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多