1

I cant figure out how to create a "must" for a leaf that makes it unique in a list.

This is list with a leaf in it, this leaf may not have the same value as any other leaf in this list.

Example of code:

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";
    }

  }

}

So i want myLeaf to trigger the error-message if there already exist an element in myList with the current value.

4

1 回答 1

3

为此,您拥有列表的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;
    }

XPath 函数返回被检查的current()叶子(初始上下文节点),同时.代表self::node()并应用于您选择的任何内容(当前 XPath 上下文节点集)。

另请注意:must约束表示断言 - 它必须评估为true(),否则实例被视为无效。因此,您的> 1情况将与您的要求相反。

于 2016-11-23T14:42:38.173 回答