【问题标题】:Comparing current element's sub-elements/child attributes with the following element's sub-elements/child attributes将当前元素的子元素/子属性与以下元素的子元素/子属性进行比较
【发布时间】:2019-03-26 07:19:07
【问题描述】:

我正在尝试编写 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>

这条规则在最后一个元素之前工作正常,最后一个元素期待另一个不存在的元素。问题可能出在“following-sibling”。

这是 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>

这个元素我得到了断言失败

【问题讨论】:

    标签: xml xpath schematron


    【解决方案1】:

    我假设您使用的是 XSLT 2.0 绑定。

    如果是这样,End/@value = following-sibling::test/Origin/@value 会将End 子元素的value 属性与Origin 子元素的value 属性进行比较,every 跟随兄弟test元素。如果 anyOrigin/@value 值匹配,则计算结果为 true。这可能不是你想要的。

    EndOrigin 元素都是 test 元素的子元素,这是 XPath 表达式的上下文。如果要测试两个子元素:

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

    如果您想使用来自以下test 元素的Origin/@value 测试当前test 元素中的End/@value,但在没有以下test 元素时不会失败:

    <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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      • 2015-02-13
      • 2011-09-29
      • 2023-03-23
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      相关资源
      最近更新 更多