【问题标题】:Schematron validating multiple elementsSchematron 验证多个元素
【发布时间】:2015-10-25 15:39:38
【问题描述】:

假设我有一个 XML 文档定义:

<people>

  <person>
    <city>London</city>
  </person>
  <person>
    <city>Paris</city>
  </person>

</people>

我想要一个 schematron 来检查每个人是否住在伦敦。

我试过了:

<sch:rule context="people">
            <sch:assert test="person/city = 'London'">Everybody must live in London!</sch:assert>
 </sch:rule>

但是,只要存在一个住在伦敦的人,这就会返回 true。有没有办法告诉 schematron 对匹配 XPathcondition 人/城市的每个元素应用测试?

【问题讨论】:

    标签: xml xpath xquery schematron


    【解决方案1】:

    “没有人可以住在伦敦以外”怎么样:

    <sch:rule context="people">
        <sch:assert test="not(person[city != 'London'])">Everybody must live in London!</sch:assert>
    </sch:rule>
    

    【讨论】:

    • 这看起来可行,谢谢。但是,有没有办法告诉 Schematron 将条件应用于所有匹配的元素?
    • 我不这么认为。如果你想对person 元素进行断言,不要在people 上下文中进行断言。 (当然,除非您希望它们仅应用于 &lt;people&gt; 元素内的人员,在这种情况下,您可以将上下文设置为 people/person 并将断言设置为 city = 'London'。)
    【解决方案2】:

    这有许多不同的解决方案。示例解决方案 1,如果有人不住在伦敦,请报告:

    <sch:rule context="people">
      <sch:report test="person/city != 'London'">Everybody must live in London!</sch:report>
    </sch:rule>
    

    示例解决方案 2,断言每个人都必须住在伦敦,请注意,这会将每个不住在伦敦的人报告为错误,而不是仅报告节点 people

    <sch:rule context="people/person">
        <sch:assert test="city = 'London'">This person should be living in london</sch:assert>
    </sch:rule>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 2012-12-30
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多