2017-09-05 29 views
0

如標題所述,我想找到一種方法來檢查XML同級元素中的重複屬性數據。檢查同級元素中具有多個屬性的重複數據 - Schematron

我已經知道了類似的問題 [here] 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> 

我需要<test>元素是唯一與ZDXXH屬性。 測試的所有兄弟元素都應該滿足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"將觸發驗證,如果我將最後一行 <test ZDX="b" XH="4" ps="qwrew"/>更改爲<test ZDX="b" XH="7" ps="qwrew"/>,它會變成唯一的,並且不會觸發驗證。

那麼,如何在一個Schematron測試短語中檢查ZDXXH

回答

0

這應該工作

<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> 
相關問題