首页 > 解决方案 > Schematron/previous-sibling 和 following-sibling 是同一个元素

问题描述

仅当 EFFECT 元素位于两个 SUBTASK 元素之间时,才拼命地试图让 Schematron 发出警报——它们是彼此的兄弟。非常感谢任何帮助,如果需要更多信息,请告诉我。

下面是一些简单的示例代码:

<TOPIC>
 <SUBTASK></SUBTASK>
 <EFFECT></EFFECT>
 <SUBTASK></SUBTASK>
</TOPIC>

这是我的两次 Schematron 尝试 - 问题是它会在文档中的每个 EFFECT 上发出警报,而不仅仅是在两个 SUBTASK 之间。*注意 - EFFECT 是一个通配符,允许在任何地方使用。

<rule context="EFFECT">
 <assert test="preceding-sibling::*[position()[1]][SUBTASK] = following-sibling::*[position()[1]][SUBTASK]" role="error" id="error_EFFECT_between_SUBTASK" 
   >EFFECT not allowed between two SUBTASKs</assert>
</rule>

<rule context="EFFECT">
 <assert test="count(preceding-sibling::*[position()>0]) and preceding-sibling::*[position()[1]][SUBTASK] = following-sibling::*[position()[1]][SUBTASK]" role="error" id="error_EFFECT_between_SUBTASKs" 
  >EFFECT not allowed between two SUBTASK</assert>
</rule>

标签: schematron

解决方案


你拥有大部分作品,但缺少self::“自我”轴。尝试(未经测试):

<rule context="EFFECT">
 <assert test="preceding-sibling::*[1][self::SUBTASK] and
               following-sibling::*[1][self::SUBTASK]"
         role="error" id="error_EFFECT_between_SUBTASKs" 
  >EFFECT not allowed between two SUBTASK</assert>
</rule>

[SUBTASK]如果有一个SUBTASK孩子,则为真,如果上下文元素是 a ,则不是SUBTASK

[position()[1]]可能是 XPath 1.0 的错误,但在 XPath 2.0 或 XPath 3.0 中,每次都是正确的,因为position()返回一个非零数字,并且将[1]单项数字序列减少为第一个(也是唯一一个)数字序列。


推荐阅读