2016-11-23 38 views
1

我無法弄清楚如何創建一個葉的「必須」,使其在列表中唯一。楊,葉必須在列表中是唯一的

這是帶有葉子的列表,該葉子可能與此列表中的任何其他葉子的值不同。的代碼

實施例:

list myList { 
    key name; 

    leaf name { 
    type uint32; 
    } 

    container myContainer { 

    leaf myLeaf { 
     type uint32; 
    } 
    must "count(/myList/myContainer/myLeaf = .) > 1" { //Dont know how to create this must function. 
     error-message "myLeaf needs to be unique in the myList list"; 
    } 

    } 

} 

所以我想myLeaf到,如果有已經在myList中以電流值存在的元件觸發差錯消息。

回答

2

您有該清單的unique關鍵字爲此目的,不需要猛擊你的頭對must表達式。它的參數中需要一個或多個空間分隔的模式節點標識符。

list myList { 
     key name; 
     unique "myContainer/myLeaf"; 

     leaf name { 
     type uint32; 
     } 

     container myContainer { 

     leaf myLeaf { 
      type uint32; 
     } 

     } 

    } 

如果你真的想要一個must來處理這個(你不應該),你可以這樣做:

leaf myLeaf { 
     must "count(/myList/myContainer/myLeaf[current()=.])=1"; 
     type uint32; 
    } 

current() XPath函數返回葉被檢查(初始上下文節點),而.代表self::node(),適用於您選擇的任何內容(當前XPath上下文節點集)。

另請注意:約定代表斷言 - 它必須評估爲true(),否則實例被認爲無效。因此,您的> 1條件將達到您所需要的相反。

相關問題