cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
624
Views
5
Helpful
2
Replies

Help with YANG xpath

ian.barrere
Level 1
Level 1

Hello,

 

We have a YANG model, a part of which looks like this:

 list c-tag {
            key "tag";
            leaf tag {
              type types:flexible-vlan-tag;
            }
            choice default-or-native {
              description "The VLAN is either set to default or native";
              case native {
                leaf native {
                  type empty;
                  must "count(../../c-tag/native[current()=.])=1" {
                    error-message "Only one c-tag can be native";
                  }
                }
              }
...

The count statement is meant to limit the number of list elements that have this leaf to 1. The code predates me, but it works. I don't know why it works, just wondering if somebody can clear that up.

 

As I understand it, the path exits to the list level and then goes back in, presumably to take into account all list elements rather than just the current one. But then what about this [current()=.]? 

1 Accepted Solution

Accepted Solutions

ramkraja
Cisco Employee
Cisco Employee

That part is actually unnecessary, in your case. "current()" and "." are the same, and they both return the current node in the context.

Your expression could also be written as

must "count(../../c-tag/native)=1"

meaning, the number of 'c-tag' instances where the 'native' leaf exists is equal to one. For an empty leaf, you cannot do any value comparison - it can just exist or not exist.

View solution in original post

2 Replies 2

ramkraja
Cisco Employee
Cisco Employee

That part is actually unnecessary, in your case. "current()" and "." are the same, and they both return the current node in the context.

Your expression could also be written as

must "count(../../c-tag/native)=1"

meaning, the number of 'c-tag' instances where the 'native' leaf exists is equal to one. For an empty leaf, you cannot do any value comparison - it can just exist or not exist.

Interesting, thank you for the clarification. That was my thinking as well, but I was stumped by the use of the square brackets on a leaf node. I am familiar with their use for defining predicates in something like a list, but how could square brackets be used on a leaf?