2016-03-21 14 views
3

僅僅是我還是不可能通過Schematron尋找一個不存在的元素。我也似乎無法找到任何文檔。如何測試Schematron中不存在的元素

採取以下規則:

<sch:rule context="/A/B/C[@TYPE='TEST1']" id="identifier-required"> 
    identifier must be present 
     <sch:assert test="not(.)" id="identifier-required"> 
    identifier-required: identifier must be present 
     </sch:assert> 
    </sch:rule> 

,並將其應用於對下列文件:

<A> 
    <B> 
     <C TYPE="TEST2">TEST</C> 
     <C TYPE="TEST3">TEST</C> 
    </B> 
</A> 

理論上,這應該會失敗,但是我發現事實並非如此。任何人都知道這是否是正確的行爲?

回答

1

上下文從不匹配任何內容的規則永遠不會觸發,也不會報告錯誤。你需要定義一個具有確實存在的上下文的規則(例如「/」)並聲明在該上下文中必須至少有一個節點通過表達式選擇A/B/C[@TYPE='TEST1']

2

當然可以檢查是否存在Schematron中的一個元素。

您的斷言不會失敗,因爲其規則上下文不匹配。

如果您的規則匹配,則必然會存在.,因此<sch:assert test="not(.)">永遠不會通過。

您可以改爲設置你的背景,而不是到C父,然後斷言,這樣的C不存在作爲一個孩子:

<sch:rule context="/A/B" id="identifier-required"> 
    <sch:assert test="not(C[@TYPE='TEST1'])" id="identifier-required"> 
     identifier-required: identifier must be present 
    </sch:assert> 
</sch:rule> 

但是你的診斷消息表明,你居然要聲明的是這樣的C存在,所以也許你真正想要的是:

<sch:rule context="/A/B" id="identifier-required"> 
    <sch:assert test="C[@TYPE='TEST1']" id="identifier-required"> 
     identifier-required: identifier must be present 
    </sch:assert> 
</sch:rule> 

這將失敗,並與消息,你給XML「標識要求的:標識必須存在」。

相關問題