【问题标题】:Check for duplicated data with multiple attribute in sibling elements - Schematron检查同级元素中具有多个属性的重复数据 - Schematron
【发布时间】:2017-09-05 08:23:14
【问题描述】:

如标题所述,我想找到一种方法来检查 XML 兄弟元素中的重复属性数据。

我已经知道类似的问题 [这里]Check for duplicated attribute data in sibling elements - Schematron,我试过了,对我没用。

我的情况是检查多个属性。

<root>
  <test ZDX="a" XH="1" ps="sdf"/>
  <test ZDX="a" XH="2" ps="dfg"/>
  <test ZDX="a" XH="3" ps="hfgh"/>
  <test ZDX="a" XH="2" ps="ertewr"/><!--same with the 2nd line-->
</root>

我需要 &lt;test&gt; 元素与 ZDXXH 属性是唯一的。 test 的所有兄弟元素都应该满足ZDXXH 属性不能同时等于另一个测试,否则会触发 SchemaTron 验证错误。

我试过这种方法,

<rule context="/root/test">
  <assert test="count(self::test) = count(self::test[not(@ZDX=preceding-sibling::test/@ZDX and @XH=preceding-sibling::test/@XH)])">
  test is not unique
  </assert>
</rule>

上面的情况很好用,下面的情况就不行了

<root>
  <test ZDX="a" XH="1" ps="sdf"/>
  <test ZDX="a" XH="2" ps="dfg"/>
  <test ZDX="a" XH="3" ps="hfgh"/>
  <test ZDX="a" XH="4" ps="ertewr"/>
  <test ZDX="b" XH="5" ps="ndmfj"/>
  <test ZDX="b" XH="6" ps="yuoi"/>
  <test ZDX="b" XH="4" ps="qwrew"/><!--conflict with the 4th line-->
</root>

在最后一个 test 元素中,如果我更改最后一行,XH="4" 将触发验证 &lt;test ZDX="b" XH="4" ps="qwrew"/&gt;to &lt;test ZDX="b" XH="7" ps="qwrew"/&gt;,变成唯一的,不会触发验证。

那么,我如何在一个 Schematron 测试短语中同时检查 ZDXXH

【问题讨论】:

    标签: xml validation schematron


    【解决方案1】:

    这应该可行

    <sch:pattern id="duplicate-attribute" abstract="true">
        <sch:rule context="$element">
            <sch:report test="@$attribute = following-sibling::$element/@$attribute 
                or @$attribute = preceding-sibling::$element/@$attribute">
                Duplicate '$attribute' attribute value found
            </sch:report>
        </sch:rule>
    </sch:pattern>
    
    <sch:pattern is-a="duplicate-attribute">
        <sch:param name="element" value="test"/>
        <sch:param name="attribute" value="XH"/>
    </sch:pattern>
    
    <sch:pattern is-a="duplicate-attribute">
        <sch:param name="element" value="test"/>
        <sch:param name="attribute" value="ZDX"/>
    </sch:pattern>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 2016-10-25
      • 2016-11-10
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多