-1

I have this yang model:

container x {
  list y{
    key "a"
    leaf a{
    }
    leaf b{
    }
  }
}

how would I access a leaf for example the b leaf wouldn't it be x/y{value a}/b? or x/y/b I tried so many combos of xpaths none are working those were just my 2 smartest combos. Please help or point me to some examples. Thanks!

predi
  • 5,528
  • 32
  • 60
citruscake
  • 21
  • 1
  • 7

1 Answers1

4

Expressions like x/y[a='value']/b or x/y/b cannot be defined anywhere for your example module and return a meaningful result. They represent relative expressions and such expressions always depend on their initial context node (the schema node for which they are defined). For example, if you define a must (XPath) expression as a substatement to the y list, the list is your initial context node and the expression resolves relative to it.

Since x is a top-level node, you cannot start a relative expression with it - this would mean that you would have to define the expression (must, when) as a substatement to the module statement, which is not allowed. That is why absolute paths exist: /x/y/b. For such an expression, the initial context node is always the document root node, since the first slash (/) selects it. The document root node will potentially have x as its child for your module.

Relative (or absolute) XPath expressions such as x/y/b are actually abbreviated forms of expressions like the following: child::x/child::y/child::b. All path expressions are actually composed of parts (what is between the slashes), called "steps" and all steps have at least two components:

  • a node test, which is what x, y and b are, respectively. They match node names.
  • an axis, which is what child:: is. An axis provides additional guidance on which nodes to select (children, siblings, parent, etc.). Even if a step does not explicitly define an axis, it will default to child::, which selects children of the nodes in current node context.

The whole purpose of a step is to select/filter nodes from the current node context. So here is what x/y/b actually says in plain English: first, give me all children of the initial context node, which are named x, and put them into current node context. Then select all children of nodes in the current node context, which are named y, and replace all nodes in the current node context with them. Then select all children of nodes in the current node context, which are named b, and replace all nodes in the current node context with them. The nodes within the current node context are the result of the expression.

With all that in mind, here are a few examples:

  • when "../a='value'";, defined as child to leaf b.
  • must "b > 25";, defined as child to list y.
  • must "y[a='key1']/b";, defined as child to container x.

Note: all XPath expressions in YANG are evaluated at server/client runtime and are applied against a tree of instantiated data nodes, also known as the accessible tree. You cannot select schema nodes with an XPath expression - only instantiated data nodes!

predi
  • 5,528
  • 32
  • 60
  • "x": [ {"a": 1, "b": 2}] , this is a clear example sorry about that. I don't get what you mean I can start a relative expression with x . Because I am because I can access x like /x and it returns "x": [ {"a": 1, "b": 2}] . instead of displaying all of that I just wan "b": 2. My description of what I'm trying to do probably wasn't clear . But thanks for helping me . – citruscake Aug 02 '18 at 17:12
  • `/x` is an absolute expression, while `x` is relative (first expression starts with a slash and the second with a node test). That is what I was trying to explain above. You seem to want to form a RESTCONF/NETCONF query, while I was explaining XPath usage within YANG definitions. – predi Aug 03 '18 at 06:32