【问题标题】:xpath to check the correct sequence of book-parts with @seqxpath 使用@seq 检查书籍部分的正确顺序
【发布时间】:2017-10-10 07:37:36
【问题描述】:

我是 XML 开发的新手,我只是阅读在线资料来帮助我开始,所以我真的不是专家。我的 schematron 文件有问题。

问题: schematron 需要通过@seq 检查每个书本部分是否按顺序排列。但是,我当前的 schematron 标记不适用于嵌套的书籍部分。

XML:

<book>
  <meta>....</meta>
  <body>
     <book-part seq="1">....</book-part>
     <book-part seq="2">....</book-part>
     <book-part seq="3">
        <book-part-meta>....</book-part-meta>
        <book-body>
          <book-part seq="4">....</book-part>
          <book-part seq="5">....</book-part>
          <book-part seq="6">....</book-part>
        </book-body>
     </book-part>
     <book-part seq="7">
        <book-part-meta>....</book-part-meta>
        <book-body>
          <book-part seq="8">....</book-part>
          <book-part seq="9">....</book-part>
          <book-part seq="10">....</book-part>
        </book-body>
     </book-part>
     <book-part seq="11">....</book-part>
     <book-part seq="12">....</book-part>
 </body>
</book>

Schematron 标记:
<rule context="book-part[@seq]"> <report role="error" test="preceding::book-part[@seq] and number(@seq) != (number(preceding::book-part[1]/@seq) + 1)">Book-parts must be in sequence</report> </rule>

schematron 错误 book-part seq="4"book-part seq="8"。第一个嵌套的书本部分似乎有问题。有没有人可以帮我解决这个问题?

【问题讨论】:

    标签: xml xpath schematron


    【解决方案1】:

    您在这里混合了层次结构级别。在这种情况下,您不仅需要preceding::,还需要parent::。 (为了一致性,我认为它应该在子级别上再次从 1 开始,但这只是我的个人意见,而不是这里的重点。)您可以通过将其与前面和祖先书籍的数量进行比较来测试 @seq 是否正确- 部分在一起:

    <pattern id="test">
        <rule context="book-part[@seq]">
            <report role="error"
                test="number(@seq) != count(ancestor::book-part[@seq] | preceding::book-part[@seq]) + 1"
                >Book-parts must be numbered in sequence</report>
        </rule>
    </pattern>
    

    我会在单独的规则中处理其他条件(前面的书籍部分必须有 @seq):

    <pattern id="test2">
        <rule context="book-part[@seq]">
            <report role="error"
                test="preceding::book-part[not(@seq)] | ancestor::book-part[not(@seq)]"
                >Book-parts must be numbered</report>
        </rule>
    </pattern>
    

    【讨论】:

    • 哦,是的,与我目前所拥有的相比,这似乎更简单。
    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多