【问题标题】:YANG, leaf must be unique in a listYANG,叶子在列表中必须是唯一的
【发布时间】:2016-11-23 10:47:25
【问题描述】:

我不知道如何为叶子创建一个“必须”,使其在列表中独一无二。

这是一个包含叶子的列表,这个叶子可能与这个列表中的任何其他叶子的值不同。

代码示例:

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

  }

}

因此,如果 myList 中已经存在具有当前值的元素,我希望 myLeaf 触发错误消息。

【问题讨论】:

    标签: xpath ietf-netmod-yang


    【解决方案1】:

    为此,您拥有列表的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 上下文节点集)。

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

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多