将当前元素的子元素/子属性与以下元素的子元素/子属性进行比较

时间:2019-03-26 07:19:07

标签: xml xpath schematron

我正在尝试编写schematron规则,以将循环中的当前子元素(End @value)的属性与下一个子元素属性(Origin @value)进行比较。我不确定我是否做对了,所以这是我的尝试

这是我的尝试:

<sch:rule context="test">
    <sch:assert test="End/@value = following-sibling::test/Origin/@value " >Both the value are not Equal. </sch:assert>
</sch:rule>

该规则在最后一个元素之前都可以正常工作,最后一个元素期望另一个不存在的元素。问题可能出在“继兄弟”之后。

这是XML文件:

<tests>
<test x="-276.724" xEnd="-276.193">
    <Origin value="36.599"/>
    <End value="36.6"/>
</test>
<test x="-276.193" xEnd="-260.29">
    <Origin value="36.6"/>
    <End value="36.603"/>
</test>
<test x="-260.29" xEnd="-240.194">
    <Origin value="36.603"/>
    <End value="36.601"/>
</test>
<test x="-240.194" xEnd="-220.046">
    <Origin value="36.601"/>
    <End value="36.601"/>
</test>
<test x="-220.046" xEnd="-200.09">
    <Origin value="36.601"/>
    <End value="36.602"/>
</test>

预期结果: 由于当前子元素(End @value)=下一个子元素属性(Origin @value),因此输出应该成功。

实际结果。

    <test x="-220.046" xEnd="-200.09">
    <Origin value="36.601"/>
    <End value="36.602"/>
</test>

该元素我正在失败,而失败

1 个答案:

答案 0 :(得分:0)

我假设您正在使用XSLT 2.0绑定。

如果是,则End/@value = following-sibling::test/Origin/@valuevalue子元素的End属性与value子元素的Origin属性进行比较同级test元素之后的所有。如果Origin/@value值的任何匹配,则结果为true。那可能不是您想要的。

EndOrigin元素都是test元素的子元素,而<sch:rule context="test"> <sch:assert test="End/@value = Origin/@value" >Both the value are not Equal. </sch:assert> </sch:rule> 元素是XPath表达式的上下文。如果要测试两个子元素:

End/@value

如果您要使用当前test元素中的Origin/@value和以下test元素中的test来测试,但是在没有后续{{1 }}元素:

<sch:rule context="test[exists(following-sibling::test[1])]">
  <sch:assert test="End/@value = following-sibling::test[1]/Origin/@value"
  >Both the  value are not Equal. </sch:assert>
</sch:rule>

[1]将XPath限制为仅选择第一个test