Search code examples
xmldata-modelingietf-netmod-yang

How to model a list in YANG with no common child elements?


I have an XML that looks like this.

<c>
    <node>
       <id1>value</id1>
       <id2>value</id2>
    </node>
    <node>
       <id3>value</id3>
       <id4>value</id4>
    </node>
</c>

How can this list be modeled in YANG? The problem here is that every list requires a key. What I tried to do:

container c {
  list node {
    key ""; /* What is the key? */
    leaf id1 {
      type string;
    }
    leaf id2{
      type string;
    }
    leaf id3 {
      type string;
    }
    leaf id4 {
      type string;
    }
  }
}

Solution

  • There is no good way to model this in YANG. Like you say, every (config true) list requires a key. Designating a leaf as a key makes that leaf mandatory, so you can't really have an instance document like in your example - if you make all four leafs to be keys (which you can as there may be multiple list keys) each node has to have all of them to be valid, and making just a couple of them to be keys, doesn't work for your example either.

    Note that a config false list does not need to have any keys. If you are not modeling configuration (or NETCONF/RESTCONF related data), that may be your way out.

    list node {
      config false;
      // ...
    }
    

    Otherwise, you have no choice - you need to introduce another leaf to serve as an id for an entry.

    Either way, you would probably utilize the unique statement (I'm assuming based on names of your leafs), to make entries in the list unique. It works in a similar way as key, but elements need not appear in an instance document if you use it - it only requires that the combination of nodes that do appear in the instance is unique across all entries of a list.

    list node {
      config false;
      unique "id1 id2 id3 id4";
      // ...
    }
    

    Note: there is a similar answered question with some more details.