xpath 使用@seq 检查书籍部分的正确顺序

xpath to check the correct sequence of book-parts with @seq

我是 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"。第一个嵌套的书部分似乎有问题。有人可以帮我解决这个问题吗?

您在这里混合了层级。在这种情况下,您不仅需要 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>