简体   繁体   English

YANG列表内的xpath表达式的相对路径

[英]relative path of an xpath expression inside a YANG list

I have the following YANG model: 我有以下YANG模型:

list machines {
                key "name";
                leaf "name" {
                    type string;
                }
                leaf link {
                    type leafref {
                            path "../name";
                    }
                }
}

Suppose there are three elements in the list: 假设列表中包含三个元素:

"machines" : [
    { "name": "a" },
    { "name": "b" },
    { "name": "c" }
]

If I want to set "link" for element b , the valid values for "link" are "a", "b", "c" or just "b" ? 如果我要为元素b设置"link" ,则"link"的有效值为"a", "b", "c"或仅仅是"b"

I didn't find answer in RFC7950 . 我在RFC7950中找不到答案。 And in pyangbind , only "b" can be set. pyangbind中 ,只能设置"b" But I'm not sure that is the right behavior. 但是我不确定这是正确的行为。

If ../name here can only reference "b" , what is the correct path to reference "a", "b", "c" , that is, all names of list elements? 如果此处的../name只能引用"b" ,那么引用"a", "b", "c"的正确路径是什么,即列表元素的所有名称?

If ../name here can reference "a", "b", "c" , what is the correct path to reference only "b" ? 如果此处的../name可以引用"a", "b", "c" ,那么仅引用"b"的正确路径是什么?

Thanks 谢谢

This is what RFC7950 says for lists: 这是RFC7950对列表的说明:

The "list" statement is used to define an interior data node in the schema tree. “列表”语句用于在架构树中定义内部数据节点。 A list node may exist in multiple instances in the data tree. 列表节点可能存在于数据树中的多个实例中。 Each such instance is known as a list entry. 每个这样的实例称为列表条目。

Despite your JSON encoded instance document seemingly suggesting that only one instance of the machines list exists within it, there are actually three list instances. 尽管您的JSON编码实例文档似乎暗示其中仅存在一个machines列表实例,但实际上有三个列表实例。 The name you chose for the list deepens this confusion, as the recommended name for a list definition would be "machine". 您为列表选择的名称加深了这种混淆,因为列表定义的建议名称为“ machine”。 An enveloping container would take the plural form of the name. 信封将采用名称的复数形式。

In XML encoding, the above becomes more obvious. 在XML编码中,以上内容变得更加明显。

<machines>
  <name>a</name>
</machines>
<machines>
  <name>b</name>
</machines>
<machines>
  <name>c</name>
</machines>

If I want to set "link" for element b, the valid values for "link" are "a", "b", "c" or just "b"? 如果我想为元素b设置“链接”,则“链接”的有效值为“ a”,“ b”,“ c”或仅仅是“ b”?

A leafref path expression is a subset of XPath. Leafref路径表达式是XPath的子集。 The same rules apply, you are just limited in what you can select with the expression - it is intended to select one or more leaf instances. 遵循相同的规则,但您只能在表达式中进行选择,即只能选择一个或多个叶子实例。 XPath always works on a tree of objects and this tree is (somewhat awkwardly) defined in RFC7950 - the document refers to this tree as the accessible tree and it consists of instantiated data nodes (subset of a data tree). XPath始终在对象树上工作,并且该树在RFC7950中(有点笨拙)定义-文档将此树称为accessible tree ,并且由实例化数据节点(数据树的子集)组成。 This is described in Section 6.4.1 . 6.4.1节中对此进行了描述。

In addition to the accessible tree, a context node is also important (relative expressions refer to it implicitly). 除了可访问树之外,上下文节点也很重要(相对表达式隐式引用了它)。 Here is what RFC7950 says about the context node in your example. 这是RFC7950关于示例中上下文节点的内容。

the context node is the node in the data tree for which the "path" statement is defined 上下文节点是数据树中为其定义“路径”语句的节点

This would be the link instance in your case, a sibling to the name instance with value b . 在您的情况下,这将是link实例,是值为bname实例的同级对象。 Here's a quick pseudo- data tree: 这是一个快速的伪数据树:

machines[1]
 +-name(a)
machines[2]
 +-name(b)
 +-link
machines[3]
 +-name(c)

The expression first proceeds to select the parent of link . 表达式首先继续选择link的父级。 There is always only one parent for any given data node instance and in your case that would be the list instance that has both siblings mentioned before as children ( machines[2] ). 对于任何给定的数据节点实例,始终只有一个父对象,在您的情况下,该列表实例就是前面提到的两个同级都作为子对象的列表实例( machines[2] )。 The expression then proceeds to select children that have a name name, which is the instance of name with value b . 然后,表达式继续选择具有name name的子代,该子代是具有值bname的实例。

This means the only valid value for your link node is b . 这意味着link节点的唯一有效值为b This only applies to your specific example. 这仅适用于您的特定示例。

If ../name here can only reference "b", what is the correct path to reference "a", "b", "c", that is, all names of list elements? 如果此处的../name只能引用“ b”,那么引用“ a”,“ b”,“ c”(即列表元素的所有名称)的正确路径是什么?

Use an absolute expression or go one parent further, then back again: 使用绝对表达式或进一步使父母双亲,然后再次返回:

  • //machines/name (replace the // with an absolute path to machines ) //machines/name (用machines的绝对路径替换//
  • ../../machines/name

Both of these will then first select all instantiated machines . 然后,这两个都将首先选择所有实例化的machines

Note: this is just how XPath works. 注意:这就是XPath的工作方式。 The other alternative would apply if RFC7950 text would say "there is only one list instance with multiple entries" or something along those lines. 如果RFC7950文本说“只有一个具有多个条目的列表实例”或类似内容,则可以采用另一种选择。

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

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