Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding namespaces to XML data trees #1104

Closed
syyyr opened this issue Jun 3, 2020 · 3 comments
Closed

Adding namespaces to XML data trees #1104

syyyr opened this issue Jun 3, 2020 · 3 comments

Comments

@syyyr
Copy link
Contributor

syyyr commented Jun 3, 2020

Hello,
I'm using libyang to print data trees for NETCONF edit-config. This is kind of a follow up to CESNET/netopeer2#636
I have this schema with a user-ordered list:

module example-schema {
    prefix aha;
    namespace "http://example.com";

    list players {
        key "name";
        ordered-by user;
        leaf name {
            type string;
        }
    }
}

And I want to move /example-schema:players[name='John'] after /example-schema:players[name='Adam']. According to the YANG 1.1 RFC (https://tools.ietf.org/html/rfc7950), I should insert an attribute yang:insert with the value after, and attribute yang:key with the value [aha:name='Adam']. I use this code to create the tree:

#include <libyang/Libyang.hpp>
#include <libyang/Tree_Data.hpp>
#include <iostream>
int main(int argc, char* argv[])
{
    auto ctx = std::make_shared<libyang::Context>();
    ctx->parse_module_path("./example-schema.yang", LYS_IN_YANG);
    auto node = std::make_shared<libyang::Data_Node>(ctx, "/example-schema:players[name='John']", nullptr, LYD_ANYDATA_CONSTSTRING, LYD_PATH_OPT_EDIT);
    node->find_path("/example-schema:players[name='John']")->data().front()->insert_attr(ctx->get_module("yang", nullptr, 0), "insert", "after");
    node->find_path("/example-schema:players[name='John']")->data().front()->insert_attr(ctx->get_module("yang", nullptr, 0), "key", "[aha:name='Adam']");
    std::cout << node->print_mem(LYD_XML, LYP_FORMAT);
    return 0;
}

This code prints this:

<players xmlns="http://example.com" xmlns:yang="urn:ietf:params:xml:ns:yang:1" yang:insert="after" yang:key="[aha:name='Adam']">
  <name>John</name>
</players>

Looking at the example in https://tools.ietf.org/html/rfc7950#page-93, I can see that the key name inside the yang:key attribute should be prefixed with the prefix of my schema (which is "aha"). But, in the example I see one more namespace defined, which seems to be connected to this prefix somehow. So, maybe the XML I want should look like this?

<players xmlns="http://example.com" xmlns:aha="http://example.com" xmlns:yang="urn:ietf:params:xml:ns:yang:1" yang:insert="after" yang:key="[aha:name='Adam']">
  <name>John</name>
</players>

My question is whether the namespace should be defined (as it is in the RFC example), or if it isn't mandatory. And if it is mandatory, then is it possible to tell libyang to insert it?

Thanks for the answer.

michalvasko added a commit that referenced this issue Jun 3, 2020
It should include key prefixes, which need to be
printed properly.
Fixes #1104
@michalvasko
Copy link
Member

Hi,
okay, please try again, the XML print should now work. However, it is quite possible something slightly different may again not work because attributes seem not to be used at all, the code I was changing could not have worked and parts of it could have never be executed. But it still comes down to the fact that types implementation in the current libyang is too naive and it should all work properly in libyang version 2 we are working on.

Regards,
Michal

@syyyr
Copy link
Contributor Author

syyyr commented Jun 3, 2020

Okay I tried it again, and it's working, the namespace thingy gets added. Thank you! The key needs to have the module name specified instead of the prefix ("example-schema" instead of "aha"), but the prefix gets added correctly. I'm not sure if this is intentional, but I like this more - it's less work for me (I don't care about prefixes).

@syyyr syyyr closed this as completed Jun 3, 2020
@michalvasko
Copy link
Member

Hi,
right, sorry, I have forgotten to mention some additional information. Yes, you need to use module names (JSON format prefixes) because those are used in all the other API functions, it is internally transformed as needed. Also, the prefix are mandatory according to specs although completely redundant, strictly speaking, as list keys always belong to the same module as the list itself.

Regards,
Michal

jktjkt added a commit to CESNET/CzechLight-dependencies that referenced this issue Jun 5, 2020
Let's bring in a fix for CESNET/libyang#1104
(and then hopefully be done with this update dance for a few days :) ).

This, of course, also required a follow-up fix in the Netopeer2, so
let's update that one as well.

Change-Id: Id0e3701c23270abfaa0c0a1425161b121c4fde9e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants