data FEATURE parser for YANG data in JSON format
diff --git a/src/parser_xml.c b/src/parser_xml.c
index a3c8acf..ffca41f 100644
--- a/src/parser_xml.c
+++ b/src/parser_xml.c
@@ -31,21 +31,37 @@
 
 /**
  * @brief Internal context for XML YANG data parser.
+ *
+ * Note that the structure maps to the lyd_ctx which is common for all the data parsers
  */
 struct lyd_xml_ctx {
-    struct lyxml_ctx *xmlctx;        /**< XML context */
+    uint32_t parse_options;        /**< various @ref dataparseroptions. */
+    uint32_t validate_options;     /**< various @ref datavalidationoptions. */
+    uint32_t int_opts;             /**< internal data parser options */
+    uint32_t path_len;             /**< used bytes in the path buffer */
+    char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
+    struct ly_set unres_node_type; /**< set of nodes validated with LY_EINCOMPLETE result */
+    struct ly_set unres_meta_type; /**< set of metadata validated with LY_EINCOMPLETE result */
+    struct ly_set when_check;      /**< set of nodes with "when" conditions */
+    struct lyd_node *op_node;      /**< if an RPC/action/notification is being parsed, store the pointer to it */
 
-    uint32_t options;                /**< various @ref dataparseroptions. */
-    uint32_t int_opts;               /**< internal data parser options */
-    uint32_t path_len;               /**< used bytes in the path buffer */
-#define LYD_PARSER_BUFSIZE 4078
-    char path[LYD_PARSER_BUFSIZE];   /**< buffer for the generated path */
-    struct ly_set unres_node_type;   /**< set of nodes validated with LY_EINCOMPLETE result */
-    struct ly_set unres_meta_type;   /**< set of metadata validated with LY_EINCOMPLETE result */
-    struct ly_set when_check;        /**< set of nodes with "when" conditions */
-    struct lyd_node *op_ntf;         /**< if an RPC/action/notification is being parsed, store the pointer to it */
+    /* callbacks */
+    lyd_ctx_free_clb free;           /* destructor */
+    ly_resolve_prefix_clb resolve_prefix;
+
+    struct lyxml_ctx *xmlctx;      /**< XML context */
 };
 
+void
+lyd_xml_ctx_free(struct lyd_ctx *lydctx)
+{
+    struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx;
+
+    lyd_ctx_free(lydctx);
+    lyxml_ctx_free(ctx->xmlctx);
+    free(ctx);
+}
+
 /**
  * @brief XML-parser's implementation of ly_type_resolve_prefix() callback to provide mapping between prefixes used
  * in the values to the schema via XML namespaces.
@@ -65,14 +81,14 @@
 }
 
 static LY_ERR
-lydxml_metadata(struct lyxml_ctx *xmlctx, const struct lysc_node *sparent, int strict, struct ly_set *unres_meta_type,
-                struct lyd_meta **meta)
+lydxml_metadata(struct lyd_xml_ctx *lydctx, const struct lysc_node *sparent, struct lyd_meta **meta)
 {
     LY_ERR ret = LY_EVALID;
     const struct lyxml_ns *ns;
     struct lys_module *mod;
     const char *name;
     size_t name_len;
+    struct lyxml_ctx *xmlctx = lydctx->xmlctx;
 
     *meta = NULL;
 
@@ -80,7 +96,7 @@
         if (!xmlctx->prefix_len) {
             /* in XML, all attributes must be prefixed
              * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */
-            if (strict) {
+            if (lydctx->parse_options & LYD_PARSE_STRICT) {
                 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
                        xmlctx->name_len, xmlctx->name);
                 goto cleanup;
@@ -104,7 +120,7 @@
         mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
         if (!mod) {
             /* module is not implemented or not present in the schema */
-            if (strict) {
+            if (lydctx->parse_options & LYD_PARSE_STRICT) {
                 LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE,
                        "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
                        ns->uri, xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "", xmlctx->name_len,
@@ -121,15 +137,9 @@
         assert(xmlctx->status == LYXML_ATTR_CONTENT);
 
         /* create metadata */
-        ret = lyd_create_meta(NULL, meta, mod, name, name_len, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic,
-                              lydxml_resolve_prefix, xmlctx, LYD_XML, sparent);
-        if (ret == LY_EINCOMPLETE) {
-            if (unres_meta_type) {
-                ly_set_add(unres_meta_type, *meta, LY_SET_OPT_USEASLIST);
-            }
-        } else if (ret) {
-            goto cleanup;
-        }
+        ret = lyd_parser_create_meta((struct lyd_ctx*)lydctx, NULL, meta, mod, name, name_len, xmlctx->value, xmlctx->value_len,
+                                     &xmlctx->dynamic, 0, lydxml_resolve_prefix, xmlctx, LYD_XML, sparent);
+        LY_CHECK_GOTO(ret, cleanup);
 
         /* next attribute */
         LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
@@ -146,12 +156,12 @@
 }
 
 static LY_ERR
-lydxml_attrs(struct lyxml_ctx *xmlctx, struct ly_attr **attr)
+lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
 {
     LY_ERR ret = LY_SUCCESS;
     const struct lyxml_ns *ns;
     struct ly_prefix *val_prefs;
-    struct ly_attr *attr2;
+    struct lyd_attr *attr2;
     const char *name, *prefix;
     size_t name_len, prefix_len;
 
@@ -189,8 +199,9 @@
         LY_CHECK_GOTO(ret = lyxml_get_prefixes(xmlctx, xmlctx->value, xmlctx->value_len, &val_prefs), cleanup);
 
         /* attr2 is always changed to the created attribute */
-        ret = ly_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, xmlctx->value, xmlctx->value_len,
-                             &xmlctx->dynamic, LYD_XML, val_prefs, prefix, prefix_len, ns ? ns->uri : NULL);
+        ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, xmlctx->value, xmlctx->value_len,
+                              &xmlctx->dynamic, 0, LYD_XML, val_prefs, prefix, prefix_len,
+                              ns ? ns->uri : NULL, ns ? strlen(ns->uri) : 0);
         LY_CHECK_GOTO(ret, cleanup);
 
         if (!*attr) {
@@ -307,7 +318,7 @@
 }
 
 static LY_ERR
-lydxml_data_check_schema(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
+lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
 {
     LY_ERR ret = LY_SUCCESS;
     enum LYXML_PARSER_STATUS prev_status;
@@ -315,40 +326,7 @@
     size_t pprefix_len, pname_len;
     struct lyxml_ctx *xmlctx = lydctx->xmlctx;
 
-    if ((lydctx->options & LYD_PARSE_NO_STATE) && ((*snode)->flags & LYS_CONFIG_R)) {
-        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INNODE, "state", (*snode)->name);
-        return LY_EVALID;
-    }
-
-    if ((*snode)->nodetype & (LYS_RPC | LYS_ACTION)) {
-        if (lydctx->int_opts & LYD_INTOPT_RPC) {
-            if (lydctx->op_ntf) {
-                LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.",
-                       lys_nodetype2str((*snode)->nodetype), (*snode)->name,
-                       lys_nodetype2str(lydctx->op_ntf->schema->nodetype), lydctx->op_ntf->schema->name);
-                return LY_EVALID;
-            }
-        } else {
-            LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Unexpected %s element \"%s\".",
-                   lys_nodetype2str((*snode)->nodetype), (*snode)->name);
-            return LY_EVALID;
-        }
-    } else if ((*snode)->nodetype == LYS_NOTIF) {
-        if (lydctx->int_opts & LYD_INTOPT_NOTIF) {
-            if (lydctx->op_ntf) {
-                LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.",
-                       lys_nodetype2str((*snode)->nodetype), (*snode)->name,
-                       lys_nodetype2str(lydctx->op_ntf->schema->nodetype), lydctx->op_ntf->schema->name);
-                return LY_EVALID;
-            }
-        } else {
-            LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Unexpected %s element \"%s\".",
-                   lys_nodetype2str((*snode)->nodetype), (*snode)->name);
-            return LY_EVALID;
-        }
-    }
-
-    if ((lydctx->options & LYD_PARSE_OPAQ) && ((*snode)->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
+    if ((lydctx->parse_options & LYD_PARSE_OPAQ) && ((*snode)->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
         /* backup parser */
         prev_status = xmlctx->status;
         pprefix = xmlctx->prefix;
@@ -416,7 +394,7 @@
     const struct ly_ctx *ctx;
     const struct lyxml_ns *ns;
     struct lyd_meta *meta = NULL, *m;
-    struct ly_attr *attr = NULL, *a;
+    struct lyd_attr *attr = NULL, *a;
     const struct lysc_node *snode;
     struct lys_module *mod;
     uint32_t prev_opts;
@@ -446,13 +424,13 @@
         }
         mod = ly_ctx_get_module_implemented_ns(ctx, ns->uri);
         if (!mod) {
-            if (lydctx->options & LYD_PARSE_STRICT) {
+            if (lydctx->parse_options & LYD_PARSE_STRICT) {
                 LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.",
                        ns->uri);
                 ret = LY_EVALID;
                 goto cleanup;
             }
-            if (!(lydctx->options & LYD_PARSE_OPAQ)) {
+            if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
                 /* skip element with children */
                 LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), cleanup);
                 continue;
@@ -467,29 +445,30 @@
         if (mod && (!parent || parent->schema)) {
             snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
             if (!snode) {
-                if (lydctx->options & LYD_PARSE_STRICT) {
+                if (lydctx->parse_options & LYD_PARSE_STRICT) {
                     LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.",
                         name_len, name, mod->name);
                     ret = LY_EVALID;
                     goto cleanup;
-                } else if (!(lydctx->options & LYD_PARSE_OPAQ)) {
+                } else if (!(lydctx->parse_options & LYD_PARSE_OPAQ)) {
                     /* skip element with children */
                     LY_CHECK_GOTO(ret = lydxml_data_skip(xmlctx), cleanup);
                     continue;
                 }
             } else {
                 /* check that schema node is valid and can be used */
-                LY_CHECK_GOTO(ret = lydxml_data_check_schema(lydctx, &snode), cleanup);
+                LY_CHECK_GOTO(ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, snode), cleanup);
+                LY_CHECK_GOTO(ret = lydxml_data_check_opaq(lydctx, &snode), cleanup);
             }
         }
 
         /* create metadata/attributes */
         if (xmlctx->status == LYXML_ATTRIBUTE) {
             if (snode) {
-                ret = lydxml_metadata(xmlctx, snode, lydctx->options & LYD_PARSE_STRICT, &lydctx->unres_meta_type, &meta);
+                ret = lydxml_metadata(lydctx, snode, &meta);
                 LY_CHECK_GOTO(ret, cleanup);
             } else {
-                assert(lydctx->options & LYD_PARSE_OPAQ);
+                assert(lydctx->parse_options & LYD_PARSE_OPAQ);
                 ret = lydxml_attrs(xmlctx, &attr);
                 LY_CHECK_GOTO(ret, cleanup);
             }
@@ -497,7 +476,7 @@
 
         assert(xmlctx->status == LYXML_ELEM_CONTENT);
         if (!snode) {
-            assert(lydctx->options & LYD_PARSE_OPAQ);
+            assert(lydctx->parse_options & LYD_PARSE_OPAQ);
 
             if (xmlctx->ws_only) {
                 /* ignore WS-only value */
@@ -510,8 +489,8 @@
             }
 
             /* create node */
-            ret = lyd_create_opaq(ctx, name, name_len, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, LYD_XML,
-                                  val_prefs, prefix, prefix_len, ns->uri, &cur);
+            ret = lyd_create_opaq(ctx, name, name_len, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, 0, LYD_XML,
+                                  val_prefs, prefix, prefix_len, ns->uri, strlen(ns->uri), &cur);
             LY_CHECK_GOTO(ret, cleanup);
 
             /* parser next */
@@ -524,21 +503,14 @@
             }
         } else if (snode->nodetype & LYD_NODE_TERM) {
             /* create node */
-            ret = lyd_create_term(snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, lydxml_resolve_prefix,
-                                  xmlctx, LYD_XML, &cur);
-            if (ret == LY_EINCOMPLETE) {
-                if (!(lydctx->options & LYD_PARSE_ONLY)) {
-                    ly_set_add(&lydctx->unres_node_type, cur, LY_SET_OPT_USEASLIST);
-                }
-            } else if (ret) {
-                goto cleanup;
-            }
+            LY_CHECK_GOTO(ret = lyd_parser_create_term((struct lyd_ctx*)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, 0,
+                                                       lydxml_resolve_prefix, xmlctx, LYD_XML, &cur), cleanup);
 
             if (parent && (cur->schema->flags & LYS_KEY)) {
                 /* check the key order, the anchor must never be a key */
                 anchor = lyd_insert_get_next_anchor(parent->child, cur);
                 if (anchor && (anchor->schema->flags & LYS_KEY)) {
-                    if (lydctx->options & LYD_PARSE_STRICT) {
+                    if (lydctx->parse_options & LYD_PARSE_STRICT) {
                         LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_DATA, "Invalid position of the key \"%s\" in a list.",
                                 cur->schema->name);
                         ret = LY_EVALID;
@@ -586,21 +558,20 @@
                 LY_CHECK_GOTO(ret = lyd_parse_check_keys(cur), cleanup);
             }
 
-            if (!(lydctx->options & LYD_PARSE_ONLY)) {
+            if (!(lydctx->parse_options & LYD_PARSE_ONLY)) {
                 /* new node validation, autodelete CANNOT occur, all nodes are new */
                 ret = lyd_validate_new(lyd_node_children_p(cur), snode, NULL, NULL);
                 LY_CHECK_GOTO(ret, cleanup);
 
                 /* add any missing default children */
-                ret = lyd_new_implicit_r(cur, lyd_node_children_p(cur), NULL, NULL, &lydctx->unres_node_type,
-                                         &lydctx->when_check, (lydctx->options & LYD_VALIDATE_NO_STATE)
-                                            ? LYD_IMPLICIT_NO_STATE : 0, NULL);
+                ret = lyd_new_implicit_r(cur, lyd_node_children_p(cur), NULL, NULL, &lydctx->unres_node_type, &lydctx->when_check,
+                                         (lydctx->validate_options & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
                 LY_CHECK_GOTO(ret, cleanup);
             }
 
             if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
                 /* rememeber the RPC/action/notification */
-                lydctx->op_ntf = cur;
+                lydctx->op_node = cur;
             }
         } else if (snode->nodetype & LYD_NODE_ANY) {
             if (!xmlctx->ws_only) {
@@ -615,12 +586,12 @@
             LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
 
             /* parse any data tree with correct options */
-            prev_opts = lydctx->options;
-            lydctx->options &= ~LYD_PARSE_STRICT;
-            lydctx->options |= LYD_PARSE_OPAQ;
+            prev_opts = lydctx->parse_options;
+            lydctx->parse_options &= ~LYD_PARSE_STRICT;
+            lydctx->parse_options |= LYD_PARSE_OPAQ;
             anchor = NULL;
             ret = lydxml_data_r(lydctx, NULL, &anchor);
-            lydctx->options = prev_opts;
+            lydctx->parse_options = prev_opts;
             LY_CHECK_GOTO(ret, cleanup);
 
             /* create node */
@@ -631,7 +602,7 @@
 
         /* add/correct flags */
         if (snode) {
-            lyd_parse_set_data_flags(cur, &lydctx->when_check, &meta, lydctx->options);
+            lyd_parse_set_data_flags(cur, &lydctx->when_check, &meta, lydctx->parse_options);
         }
 
         /* add metadata/attributes */
@@ -677,73 +648,37 @@
 }
 
 LY_ERR
-lyd_parse_xml_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options, struct lyd_node **tree)
+lyd_parse_xml_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options, struct lyd_node **tree_p, struct lyd_ctx **lydctx_p)
 {
     LY_ERR ret = LY_SUCCESS;
-    struct lyd_xml_ctx lydctx = {0};
-    uint32_t i = 0;
-    const struct lys_module *mod;
-    struct lyd_node *first, *next, **first2;
+    struct lyd_xml_ctx *lydctx;
 
     assert(!(parse_options & ~LYD_PARSE_OPTS_MASK));
     assert(!(validate_options & ~LYD_VALIDATE_OPTS_MASK));
 
-    /* init context and tree */
-    LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, in, &lydctx.xmlctx), cleanup);
-    lydctx.options = parse_options;
-    *tree = NULL;
+    /* init context */
+    lydctx = calloc(1, sizeof *lydctx);
+    LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
+    LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
+    lydctx->parse_options = parse_options;
+    lydctx->validate_options = validate_options;
+    lydctx->free = lyd_xml_ctx_free;
+    lydctx->resolve_prefix = lydxml_resolve_prefix;
 
     /* parse XML data */
-    LY_CHECK_GOTO(ret = lydxml_data_r(&lydctx, NULL, tree), cleanup);
-
-    if (!(parse_options & LYD_PARSE_ONLY)) {
-        next = *tree;
-        while (1) {
-            if (validate_options & LYD_VALIDATE_PRESENT) {
-                mod = lyd_data_next_module(&next, &first);
-            } else {
-                mod = lyd_mod_next_module(next, NULL, ctx, &i, &first);
-            }
-            if (!mod) {
-                break;
-            }
-            if (first == *tree) {
-                /* make sure first2 changes are carried to tree */
-                first2 = tree;
-            } else {
-                first2 = &first;
-            }
-
-            /* validate new top-level nodes, autodelete CANNOT occur, all nodes are new */
-            LY_CHECK_GOTO(ret = lyd_validate_new(first2, NULL, mod, NULL), cleanup);
-
-            /* add all top-level defaults for this module */
-            ret = lyd_new_implicit_r(NULL, first2, NULL, mod, &lydctx.unres_node_type, &lydctx.when_check,
-                                     validate_options & LYD_VALIDATE_NO_STATE ? LYD_IMPLICIT_NO_STATE : 0, NULL);
-            LY_CHECK_GOTO(ret, cleanup);
-
-            /* finish incompletely validated terminal values/attributes and when conditions */
-            ret = lyd_validate_unres(tree, &lydctx.when_check, &lydctx.unres_node_type, &lydctx.unres_meta_type, LYD_XML,
-                                     lydxml_resolve_prefix, lydctx.xmlctx, NULL);
-            LY_CHECK_GOTO(ret, cleanup);
-
-            /* perform final validation that assumes the data tree is final */
-            LY_CHECK_GOTO(ret = lyd_validate_final_r(*first2, NULL, mod, validate_options, 0), cleanup);
-        }
-    }
+    LY_CHECK_GOTO(ret = lydxml_data_r(lydctx, NULL, tree_p), cleanup);
 
 cleanup:
     /* there should be no unresolved types stored */
-    assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx.unres_node_type.count && !lydctx.unres_meta_type.count
-           && !lydctx.when_check.count));
+    assert(!(parse_options & LYD_PARSE_ONLY) || (!lydctx->unres_node_type.count && !lydctx->unres_meta_type.count
+           && !lydctx->when_check.count));
 
-    ly_set_erase(&lydctx.unres_node_type, NULL);
-    ly_set_erase(&lydctx.unres_meta_type, NULL);
-    ly_set_erase(&lydctx.when_check, NULL);
-    lyxml_ctx_free(lydctx.xmlctx);
     if (ret) {
-        lyd_free_all(*tree);
-        *tree = NULL;
+        lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
+        lyd_free_all(*tree_p);
+        *tree_p = NULL;
+    } else {
+        *lydctx_p = (struct lyd_ctx *)lydctx;
     }
     return ret;
 }
@@ -753,7 +688,7 @@
 {
     LY_ERR ret = LY_SUCCESS;
     const struct lyxml_ns *ns = NULL;
-    struct ly_attr *attr = NULL;
+    struct lyd_attr *attr = NULL;
     const char *prefix;
     size_t prefix_len;
 
@@ -795,7 +730,8 @@
     LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
 
     /* create node */
-    ret = lyd_create_opaq(xmlctx->ctx, name, strlen(name), "", 0, NULL, LYD_XML, NULL, prefix, prefix_len, uri, envp);
+    ret = lyd_create_opaq(xmlctx->ctx, name, strlen(name), "", 0, NULL, 0, LYD_XML, NULL, prefix, prefix_len,
+                          uri, strlen(uri), envp);
     LY_CHECK_GOTO(ret, cleanup);
 
     /* assign atributes */
@@ -808,20 +744,17 @@
 }
 
 LY_ERR
-lyd_parse_xml_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree, struct lyd_node **op)
+lyd_parse_xml_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
 {
     LY_ERR ret = LY_SUCCESS;
     struct lyd_xml_ctx lydctx = {0};
     struct lyd_node *rpc_e = NULL, *act_e = NULL;
+    struct lyd_node *tree = NULL;
 
     /* init */
     LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, in, &lydctx.xmlctx), cleanup);
-    lydctx.options = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
+    lydctx.parse_options = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
     lydctx.int_opts = LYD_INTOPT_RPC;
-    *tree = NULL;
-    if (op) {
-        *op = NULL;
-    }
 
     /* parse "rpc", if any */
     LY_CHECK_GOTO(ret = lydxml_envelope(lydctx.xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", &rpc_e), cleanup);
@@ -832,10 +765,10 @@
     }
 
     /* parse the rest of data normally */
-    LY_CHECK_GOTO(ret = lydxml_data_r(&lydctx, NULL, tree), cleanup);
+    LY_CHECK_GOTO(ret = lydxml_data_r(&lydctx, NULL, &tree), cleanup);
 
     /* make sure we have parsed some operation */
-    if (!lydctx.op_ntf) {
+    if (!lydctx.op_node) {
         LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"rpc\"/\"action\" node.");
         ret = LY_EVALID;
         goto cleanup;
@@ -849,9 +782,9 @@
                    lydctx.xmlctx->name_len, lydctx.xmlctx->name);
             ret = LY_EVALID;
             goto cleanup;
-        } else if (lydctx.op_ntf->schema->nodetype != LYS_ACTION) {
-            LOGVAL(ctx, LY_VLOG_LYD, lydctx.op_ntf, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
-                   lys_nodetype2str(lydctx.op_ntf->schema->nodetype));
+        } else if (lydctx.op_node->schema->nodetype != LYS_ACTION) {
+            LOGVAL(ctx, LY_VLOG_LYD, lydctx.op_node, LYVE_DATA, "Unexpected %s element, an \"action\" expected.",
+                   lys_nodetype2str(lydctx.op_node->schema->nodetype));
             ret = LY_EVALID;
             goto cleanup;
         }
@@ -864,28 +797,35 @@
                    lydctx.xmlctx->name_len, lydctx.xmlctx->name);
             ret = LY_EVALID;
             goto cleanup;
-        } else if (!act_e && (lydctx.op_ntf->schema->nodetype != LYS_RPC)) {
-            LOGVAL(ctx, LY_VLOG_LYD, lydctx.op_ntf, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
-                   lys_nodetype2str(lydctx.op_ntf->schema->nodetype));
+        } else if (!act_e && (lydctx.op_node->schema->nodetype != LYS_RPC)) {
+            LOGVAL(ctx, LY_VLOG_LYD, lydctx.op_node, LYVE_DATA, "Unexpected %s element, an \"rpc\" expected.",
+                   lys_nodetype2str(lydctx.op_node->schema->nodetype));
             ret = LY_EVALID;
             goto cleanup;
         }
         LY_CHECK_GOTO(ret = lyxml_ctx_next(lydctx.xmlctx), cleanup);
     }
 
-    if (op) {
-        *op = lydctx.op_ntf;
+    if (op_p) {
+        *op_p = lydctx.op_node;
     }
-    assert(*tree);
+    assert(tree);
+    if (tree_p) {
+        *tree_p = tree;
+    }
     if (act_e) {
         /* connect to the action */
-        lyd_insert_node(act_e, NULL, *tree);
-        *tree = act_e;
+        lyd_insert_node(act_e, NULL, *tree_p);
+        if (tree_p) {
+            *tree_p = act_e;
+        }
     }
     if (rpc_e) {
         /* connect to the rpc */
-        lyd_insert_node(rpc_e, NULL, *tree);
-        *tree = rpc_e;
+        lyd_insert_node(rpc_e, NULL, *tree_p);
+        if (tree_p) {
+            *tree_p = rpc_e;
+        }
     }
 
 cleanup:
@@ -893,10 +833,9 @@
     assert(!lydctx.unres_node_type.count && !lydctx.unres_meta_type.count && !lydctx.when_check.count);
     lyxml_ctx_free(lydctx.xmlctx);
     if (ret) {
-        lyd_free_all(*tree);
+        lyd_free_all(tree);
         lyd_free_tree(act_e);
         lyd_free_tree(rpc_e);
-        *tree = NULL;
     }
     return ret;
 }
@@ -906,7 +845,7 @@
 {
     LY_ERR ret = LY_SUCCESS;
     const struct lyxml_ns *ns = NULL;
-    struct ly_attr *attr = NULL;
+    struct lyd_attr *attr = NULL;
     struct lyd_node *et;
     const char *prefix;
     size_t prefix_len;
@@ -961,8 +900,8 @@
     }*/
 
     /* create node */
-    ret = lyd_create_opaq(xmlctx->ctx, "eventTime", 9, xmlctx->value, xmlctx->value_len, NULL, LYD_XML, NULL,
-                          prefix, prefix_len, ns->uri, &et);
+    ret = lyd_create_opaq(xmlctx->ctx, "eventTime", 9, xmlctx->value, xmlctx->value_len, NULL, 0, LYD_XML, NULL,
+                          prefix, prefix_len, ns->uri, strlen(ns->uri), &et);
     LY_CHECK_GOTO(ret, cleanup);
 
     /* assign atributes */
@@ -992,29 +931,26 @@
 }
 
 LY_ERR
-lyd_parse_xml_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree, struct lyd_node **ntf)
+lyd_parse_xml_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p)
 {
     LY_ERR ret = LY_SUCCESS;
     struct lyd_xml_ctx lydctx = {0};
     struct lyd_node *ntf_e = NULL;
+    struct lyd_node *tree = NULL;
 
     /* init */
     LY_CHECK_GOTO(ret = lyxml_ctx_new(ctx, in, &lydctx.xmlctx), cleanup);
-    lydctx.options = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
+    lydctx.parse_options = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
     lydctx.int_opts = LYD_INTOPT_NOTIF;
-    *tree = NULL;
-    if (ntf) {
-        *ntf = NULL;
-    }
 
     /* parse "notification" and "eventTime", if present */
     LY_CHECK_GOTO(ret = lydxml_notif_envelope(lydctx.xmlctx, &ntf_e), cleanup);
 
     /* parse the rest of data normally */
-    LY_CHECK_GOTO(ret = lydxml_data_r(&lydctx, NULL, tree), cleanup);
+    LY_CHECK_GOTO(ret = lydxml_data_r(&lydctx, NULL, &tree), cleanup);
 
     /* make sure we have parsed some notification */
-    if (!lydctx.op_ntf) {
+    if (!lydctx.op_node) {
         LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node.");
         ret = LY_EVALID;
         goto cleanup;
@@ -1032,14 +968,19 @@
         LY_CHECK_GOTO(ret = lyxml_ctx_next(lydctx.xmlctx), cleanup);
     }
 
-    if (ntf) {
-        *ntf = lydctx.op_ntf;
+    if (ntf_p) {
+        *ntf_p = lydctx.op_node;
     }
-    assert(*tree);
+    assert(tree);
+    if (tree_p) {
+        *tree_p = tree;
+    }
     if (ntf_e) {
         /* connect to the notification */
-        lyd_insert_node(ntf_e, NULL, *tree);
-        *tree = ntf_e;
+        lyd_insert_node(ntf_e, NULL, *tree_p);
+        if (tree_p) {
+            *tree_p = ntf_e;
+        }
     }
 
 cleanup:
@@ -1047,15 +988,14 @@
     assert(!lydctx.unres_node_type.count && !lydctx.unres_meta_type.count && !lydctx.when_check.count);
     lyxml_ctx_free(lydctx.xmlctx);
     if (ret) {
-        lyd_free_all(*tree);
+        lyd_free_all(tree);
         lyd_free_tree(ntf_e);
-        *tree = NULL;
     }
     return ret;
 }
 
 LY_ERR
-lyd_parse_xml_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree, struct lyd_node **op)
+lyd_parse_xml_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p)
 {
     LY_ERR ret = LY_SUCCESS;
     struct lyd_xml_ctx lydctx = {0};
@@ -1063,12 +1003,8 @@
 
     /* init */
     LY_CHECK_GOTO(ret = lyxml_ctx_new(LYD_NODE_CTX(request), in, &lydctx.xmlctx), cleanup);
-    lydctx.options = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
+    lydctx.parse_options = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
     lydctx.int_opts = LYD_INTOPT_REPLY;
-    *tree = NULL;
-    if (op) {
-        *op = NULL;
-    }
 
     /* find request OP */
     LYD_TREE_DFS_BEGIN((struct lyd_node *)request, req_op) {
@@ -1105,15 +1041,19 @@
         LY_CHECK_GOTO(ret = lyxml_ctx_next(lydctx.xmlctx), cleanup);
     }
 
-    if (op) {
-        *op = rep_op;
+    if (op_p) {
+        *op_p = rep_op;
     }
     for (iter = rep_op; iter->parent; iter = (struct lyd_node *)iter->parent);
-    *tree = iter;
+    if (tree_p) {
+        *tree_p = iter;
+    }
     if (rpcr_e) {
         /* connect to the operation */
-        lyd_insert_node(rpcr_e, NULL, *tree);
-        *tree = rpcr_e;
+        lyd_insert_node(rpcr_e, NULL, *tree_p);
+        if (tree_p) {
+            *tree_p = rpcr_e;
+        }
     }
 
 cleanup: