简体   繁体   English

杨,叶必须是唯一的列表

[英]YANG, leaf must be unique in a list

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. 所以我希望myLeaf在myList中已经存在具有当前值的元素时触发错误消息。

You have the list's unique keyword for that purpose, no need to bash you head against must expressions. 为此目的,您拥有列表的unique关键字,无需反对must表达式。 It takes one or more space separated schema node identifiers in its argument. 它在其参数中需要一个或多个空格分隔的模式节点标识符。

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

      leaf name {
        type uint32;
      }

      container myContainer {

        leaf myLeaf {
          type uint32;
        }

      }

    }

If you really want a must to handle this (which you shouldn't) you could do something like: 如果你真的想要一个must处理这个(你不应该),你可以这样做:

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

The current() XPath function returns the leaf being checked (the initial context node), while . current() XPath函数返回正在检查的叶子(初始上下文节点),而. stands for self::node() and applies to whatever you selected (current XPath context node set). 代表self::node()并适用于您选择的任何内容(当前XPath上下文节点集)。

Also note: a must constraint represents an assertion - it MUST evaluate to true() , otherwise an instance is considered invalid. 另请注意: must约束表示断言 - 它必须求值为true() ,否则实例被视为无效。 Your > 1 condition would therefore achieve the opposite of what you require. 因此,您的> 1条件将达到您所要求的相反条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM