schema parsers BUGFIX avoid unres (must/when checking) duplication

make sure that must/when xpaths are checked only when they are
instantiated in a data tree, not inside a grouping
diff --git a/src/parser.h b/src/parser.h
index d3074fa..b43782d 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -47,6 +47,14 @@
 
 /**@} jsondata */
 
+/**
+ * internal options values for schema parsers
+ */
+#define LYS_PARSE_OPT_CFG_NOINHERIT 0x01 /**< do not inherit config flag */
+#define LYS_PARSE_OPT_CFG_IGNORE    0x02 /**< ignore config flag (in rpc, actions, notifications) */
+#define LYS_PARSE_OPT_CFG_MASK      0x03
+#define LYS_PARSE_OPT_INGRP         0x04 /**< flag to know that parser is inside a grouping */
+
 /* list of YANG statement strings */
 extern const char *ly_stmt_str[];
 
diff --git a/src/parser_yang.c b/src/parser_yang.c
index ada948a..f160787 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -25,7 +25,7 @@
 static int yang_check_sub_module(struct lys_module *module, struct unres_schema *unres, struct lys_node *node);
 static void free_yang_common(struct lys_module *module, struct lys_node *node);
 static int yang_check_nodes(struct lys_module *module, struct lys_node *parent, struct lys_node *nodes,
-                            int config_opt, struct unres_schema *unres);
+                            int options, struct unres_schema *unres);
 static int yang_fill_ext_substm_index(struct lys_ext_instance_complex *ext, LY_STMT stmt, enum yytokentype keyword);
 static void yang_free_nodes(struct ly_ctx *ctx, struct lys_node *node);
 void lys_iffeature_free(struct ly_ctx *ctx, struct lys_iffeature *iffeature, uint8_t iffeature_size);
@@ -2468,10 +2468,8 @@
 }
 
 int
-store_config_flag(struct lys_node *node, int config_opt)
+store_config_flag(struct lys_node *node, int options)
 {
-    int ret = config_opt;
-
     switch (node->nodetype) {
     case LYS_CONTAINER:
     case LYS_LEAF:
@@ -2480,9 +2478,9 @@
     case LYS_CHOICE:
     case LYS_ANYDATA:
     case LYS_ANYXML:
-        if (config_opt == CONFIG_IGNORE) {
+        if (options & LYS_PARSE_OPT_CFG_IGNORE) {
             node->flags |= node->flags & (~(LYS_CONFIG_MASK | LYS_CONFIG_SET));
-        } else if (config_opt == CONFIG_INHERIT_ENABLE) {
+        } else if (!(options & LYS_PARSE_OPT_CFG_NOINHERIT)) {
             if (!(node->flags & LYS_CONFIG_MASK)) {
                 /* get config flag from parent */
                 if (node->parent) {
@@ -2495,7 +2493,7 @@
         }
         break;
     case LYS_CASE:
-        if (config_opt == CONFIG_INHERIT_ENABLE) {
+        if (!(options & (LYS_PARSE_OPT_CFG_IGNORE | LYS_PARSE_OPT_CFG_NOINHERIT))) {
             if (!(node->flags & LYS_CONFIG_MASK)) {
                 /* get config flag from parent */
                 if (node->parent) {
@@ -2507,16 +2505,11 @@
             }
         }
         break;
-    case LYS_RPC:
-    case LYS_ACTION:
-    case LYS_NOTIF:
-        ret = CONFIG_IGNORE;
-        break;
     default:
         break;
     }
 
-    return ret;
+    return EXIT_SUCCESS;
 }
 
 int
@@ -2585,7 +2578,7 @@
         ret = -1;
     } else {
         /* success parse, but it needs some sematic controls */
-        if (node && yang_check_nodes(module, (struct lys_node *)ext, node, CONFIG_INHERIT_DISABLE, unres)) {
+        if (node && yang_check_nodes(module, (struct lys_node *)ext, node, LYS_PARSE_OPT_CFG_NOINHERIT, unres)) {
             ret = -1;
         }
     }
@@ -3650,7 +3643,7 @@
 
 static int
 yang_check_container(struct lys_module *module, struct lys_node_container *cont, struct lys_node **child,
-                     int config_opt, struct unres_schema *unres)
+                     int options, struct unres_schema *unres)
 {
     if (yang_check_typedef(module, (struct lys_node *)cont, unres)) {
         goto error;
@@ -3660,7 +3653,7 @@
         goto error;
     }
 
-    if (yang_check_nodes(module, (struct lys_node *)cont, *child, config_opt, unres)) {
+    if (yang_check_nodes(module, (struct lys_node *)cont, *child, options, unres)) {
         *child = NULL;
         goto error;
     }
@@ -3674,7 +3667,8 @@
     }
 
     /* check XPath dependencies */
-    if ((cont->when || cont->must_size) && (unres_schema_add_node(module, unres, cont, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (cont->when || cont->must_size) &&
+            (unres_schema_add_node(module, unres, cont, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -3684,7 +3678,7 @@
 }
 
 static int
-yang_check_leaf(struct lys_module *module, struct lys_node_leaf *leaf, struct unres_schema *unres)
+yang_check_leaf(struct lys_module *module, struct lys_node_leaf *leaf, int options, struct unres_schema *unres)
 {
     if (yang_fill_type(module, &leaf->type, (struct yang_type *)leaf->type.der, leaf, unres)) {
             yang_type_free(module->ctx, &leaf->type);
@@ -3711,7 +3705,8 @@
         goto error;
     }
     /* check XPath dependencies */
-    if ((leaf->when || leaf->must_size) && (unres_schema_add_node(module, unres, leaf, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (leaf->when || leaf->must_size) &&
+            (unres_schema_add_node(module, unres, leaf, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -3721,7 +3716,8 @@
 }
 
 static int
-yang_check_leaflist(struct lys_module *module, struct lys_node_leaflist *leaflist, struct unres_schema *unres)
+yang_check_leaflist(struct lys_module *module, struct lys_node_leaflist *leaflist, int options,
+                    struct unres_schema *unres)
 {
     int i, j;
 
@@ -3766,7 +3762,8 @@
     }
 
     /* check XPath dependencies */
-    if ((leaflist->when || leaflist->must_size) && (unres_schema_add_node(module, unres, leaflist, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (leaflist->when || leaflist->must_size) &&
+            (unres_schema_add_node(module, unres, leaflist, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -3777,7 +3774,7 @@
 
 static int
 yang_check_list(struct lys_module *module, struct lys_node_list *list, struct lys_node **child,
-                int config_opt, struct unres_schema *unres)
+                int options, struct unres_schema *unres)
 {
     struct lys_node *node;
 
@@ -3803,7 +3800,7 @@
         goto error;
     }
 
-    if (yang_check_nodes(module, (struct lys_node *)list, *child, config_opt, unres)) {
+    if (yang_check_nodes(module, (struct lys_node *)list, *child, options, unres)) {
         *child = NULL;
         goto error;
     }
@@ -3824,7 +3821,8 @@
         goto error;
     }
     /* check XPath dependencies */
-    if ((list->when || list->must_size) && (unres_schema_add_node(module, unres, list, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (list->when || list->must_size) &&
+            (unres_schema_add_node(module, unres, list, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -3835,7 +3833,7 @@
 
 static int
 yang_check_choice(struct lys_module *module, struct lys_node_choice *choice, struct lys_node **child,
-                  int config_opt, struct unres_schema *unres)
+                  int options, struct unres_schema *unres)
 {
     char *value;
 
@@ -3845,7 +3843,7 @@
         goto error;
     }
 
-    if (yang_check_nodes(module, (struct lys_node *)choice, *child, config_opt,  unres)) {
+    if (yang_check_nodes(module, (struct lys_node *)choice, *child, options, unres)) {
         *child = NULL;
         free(choice->dflt);
         choice->dflt = NULL;
@@ -3868,7 +3866,8 @@
     }
 
     /* check XPath dependencies */
-    if ((choice->when) && (unres_schema_add_node(module, unres, choice, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (choice->when) &&
+            (unres_schema_add_node(module, unres, choice, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -3879,7 +3878,7 @@
 
 static int
 yang_check_rpc_action(struct lys_module *module, struct lys_node_rpc_action *rpc, struct lys_node **child,
-                      int config_opt, struct unres_schema *unres)
+                      int options, struct unres_schema *unres)
 {
     struct lys_node *node;
 
@@ -3900,7 +3899,7 @@
         goto error;
     }
 
-    if (yang_check_nodes(module, (struct lys_node *)rpc, *child, config_opt, unres)) {
+    if (yang_check_nodes(module, (struct lys_node *)rpc, *child, options | LYS_PARSE_OPT_CFG_IGNORE, unres)) {
         *child = NULL;
         goto error;
     }
@@ -3913,7 +3912,7 @@
 
 static int
 yang_check_notif(struct lys_module *module, struct lys_node_notif *notif, struct lys_node **child,
-                 int config_opt, struct unres_schema *unres)
+                 int options, struct unres_schema *unres)
 {
     if (yang_check_typedef(module, (struct lys_node *)notif, unres)) {
         goto error;
@@ -3923,7 +3922,7 @@
         goto error;
     }
 
-    if (yang_check_nodes(module, (struct lys_node *)notif, *child, config_opt, unres)) {
+    if (yang_check_nodes(module, (struct lys_node *)notif, *child, options | LYS_PARSE_OPT_CFG_IGNORE, unres)) {
         *child = NULL;
         goto error;
     }
@@ -3934,7 +3933,8 @@
             goto error;
         }
         /* check XPath dependencies */
-        if (unres_schema_add_node(module, unres, notif, UNRES_XPATH, NULL) == -1) {
+        if (!(options & LYS_PARSE_OPT_INGRP) &&
+                unres_schema_add_node(module, unres, notif, UNRES_XPATH, NULL) == -1) {
             goto error;
         }
     }
@@ -3945,7 +3945,7 @@
 }
 
 static int
-yang_check_augment(struct lys_module *module, struct lys_node_augment *augment, int config_opt, struct unres_schema *unres)
+yang_check_augment(struct lys_module *module, struct lys_node_augment *augment, int options, struct unres_schema *unres)
 {
     struct lys_node *child;
 
@@ -3957,7 +3957,7 @@
         goto error;
     }
 
-    if (yang_check_nodes(module, (struct lys_node *)augment, child, config_opt, unres)) {
+    if (yang_check_nodes(module, (struct lys_node *)augment, child, options, unres)) {
         goto error;
     }
 
@@ -3970,7 +3970,8 @@
     }
 
     /* check XPath dependencies */
-    if (augment->when && (unres_schema_add_node(module, unres, augment, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && augment->when &&
+            (unres_schema_add_node(module, unres, augment, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -3980,7 +3981,7 @@
 }
 
 static int
-yang_check_uses(struct lys_module *module, struct lys_node_uses *uses, int config_opt, struct unres_schema *unres)
+yang_check_uses(struct lys_module *module, struct lys_node_uses *uses, int options, struct unres_schema *unres)
 {
     uint i, size;
 
@@ -4005,7 +4006,7 @@
 
     for (i = 0; i < size; ++i) {
         uses->augment_size++;
-        if (yang_check_augment(module, &uses->augment[i], config_opt, unres)) {
+        if (yang_check_augment(module, &uses->augment[i], options, unres)) {
             goto error;
         }
     }
@@ -4019,7 +4020,8 @@
     }
 
     /* check XPath dependencies */
-    if (uses->when && (unres_schema_add_node(module, unres, uses, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && uses->when &&
+            (unres_schema_add_node(module, unres, uses, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -4033,13 +4035,13 @@
 
 static int
 yang_check_anydata(struct lys_module *module, struct lys_node_anydata *anydata, struct lys_node **child,
-                   int config_opt, struct unres_schema *unres)
+                   int options, struct unres_schema *unres)
 {
     if (yang_check_iffeatures(module, NULL, anydata, ANYDATA_KEYWORD, unres)) {
         goto error;
     }
 
-    if (yang_check_nodes(module, (struct lys_node *)anydata, *child, config_opt, unres)) {
+    if (yang_check_nodes(module, (struct lys_node *)anydata, *child, options, unres)) {
         *child = NULL;
         goto error;
     }
@@ -4053,7 +4055,8 @@
     }
 
     /* check XPath dependencies */
-    if ((anydata->when || anydata->must_size) && (unres_schema_add_node(module, unres, anydata, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (anydata->when || anydata->must_size) &&
+            (unres_schema_add_node(module, unres, anydata, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
     return EXIT_SUCCESS;
@@ -4063,7 +4066,7 @@
 
 static int
 yang_check_nodes(struct lys_module *module, struct lys_node *parent, struct lys_node *nodes,
-                 int config_opt, struct unres_schema *unres)
+                 int options, struct unres_schema *unres)
 {
     struct lys_node *node = nodes, *sibling, *child;
 
@@ -4079,7 +4082,7 @@
             yang_free_nodes(module->ctx, node);
             goto error;
         }
-        config_opt = store_config_flag(node, config_opt);
+        store_config_flag(node, options);
         if (yang_check_ext_instance(module, &node->ext, node->ext_size, node, unres)) {
             goto error;
         }
@@ -4092,35 +4095,35 @@
             if (yang_check_iffeatures(module, NULL, node, GROUPING_KEYWORD, unres)) {
                 goto error;
             }
-            if (yang_check_nodes(module, node, child, config_opt, unres)) {
+            if (yang_check_nodes(module, node, child, options | LYS_PARSE_OPT_INGRP, unres)) {
                 child = NULL;
                 goto error;
             }
             break;
         case LYS_CONTAINER:
-            if (yang_check_container(module, (struct lys_node_container *)node, &child, config_opt, unres)) {
+            if (yang_check_container(module, (struct lys_node_container *)node, &child, options, unres)) {
                 goto error;
             }
             break;
         case LYS_LEAF:
-            if (yang_check_leaf(module, (struct lys_node_leaf *)node, unres)) {
+            if (yang_check_leaf(module, (struct lys_node_leaf *)node, options, unres)) {
                 child = NULL;
                 goto error;
             }
             break;
         case LYS_LEAFLIST:
-            if (yang_check_leaflist(module, (struct lys_node_leaflist *)node, unres)) {
+            if (yang_check_leaflist(module, (struct lys_node_leaflist *)node, options, unres)) {
                 child = NULL;
                 goto error;
             }
             break;
         case LYS_LIST:
-            if (yang_check_list(module, (struct lys_node_list *)node, &child, config_opt, unres)) {
+            if (yang_check_list(module, (struct lys_node_list *)node, &child, options, unres)) {
                 goto error;
             }
             break;
         case LYS_CHOICE:
-            if (yang_check_choice(module, (struct lys_node_choice *)node, &child, config_opt, unres)) {
+            if (yang_check_choice(module, (struct lys_node_choice *)node, &child, options, unres)) {
                 goto error;
             }
             break;
@@ -4128,20 +4131,31 @@
             if (yang_check_iffeatures(module, NULL, node, CASE_KEYWORD, unres)) {
                 goto error;
             }
-            if (yang_check_nodes(module, node, child, config_opt, unres)) {
+            if (yang_check_nodes(module, node, child, options, unres)) {
                 child = NULL;
                 goto error;
             }
+            if (((struct lys_node_case *)node)->when) {
+                if (yang_check_ext_instance(module, &((struct lys_node_case *)node)->when->ext,
+                        ((struct lys_node_case *)node)->when->ext_size, ((struct lys_node_case *)node)->when, unres)) {
+                    goto error;
+                }
+                /* check XPath dependencies */
+                if (!(options & LYS_PARSE_OPT_INGRP) &&
+                        (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1)) {
+                    goto error;
+                }
+            }
             break;
         case LYS_ANYDATA:
         case LYS_ANYXML:
-            if (yang_check_anydata(module, (struct lys_node_anydata *)node, &child, config_opt, unres)) {
+            if (yang_check_anydata(module, (struct lys_node_anydata *)node, &child, options, unres)) {
                 goto error;
             }
             break;
         case LYS_RPC:
         case LYS_ACTION:
-            if (yang_check_rpc_action(module, (struct lys_node_rpc_action *)node, &child, config_opt, unres)){
+            if (yang_check_rpc_action(module, (struct lys_node_rpc_action *)node, &child, options, unres)){
                 goto error;
             }
             break;
@@ -4150,7 +4164,7 @@
             if (yang_check_typedef(module, node, unres)) {
                 goto error;
             }
-            if (yang_check_nodes(module, node, child, config_opt, unres)) {
+            if (yang_check_nodes(module, node, child, options, unres)) {
                 child = NULL;
                 goto error;
             }
@@ -4159,18 +4173,19 @@
                     goto error;
                 }
                 /* check XPath dependencies */
-                if (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1) {
+                if (!(options & LYS_PARSE_OPT_INGRP) &&
+                        (unres_schema_add_node(module, unres, node, UNRES_XPATH, NULL) == -1)) {
                     goto error;
                 }
             }
             break;
         case LYS_NOTIF:
-            if (yang_check_notif(module, (struct lys_node_notif *)node, &child, config_opt, unres)) {
+            if (yang_check_notif(module, (struct lys_node_notif *)node, &child, options, unres)) {
                 goto error;
             }
             break;
         case LYS_USES:
-            if (yang_check_uses(module, (struct lys_node_uses *)node, config_opt, unres)) {
+            if (yang_check_uses(module, (struct lys_node_uses *)node, options, unres)) {
                 child = NULL;
                 goto error;
             }
@@ -4449,7 +4464,7 @@
         goto error;
     }
     erase_nodes = 0;
-    if (yang_check_nodes(module, NULL, node, CONFIG_INHERIT_ENABLE, unres)) {
+    if (yang_check_nodes(module, NULL, node, 0, unres)) {
         goto error;
     }
 
@@ -4464,7 +4479,7 @@
     /* check augments */
     for (i = 0; i < aug_size; ++i) {
         module->augment_size++;
-        if (yang_check_augment(module, &module->augment[i], CONFIG_INHERIT_ENABLE, unres)) {
+        if (yang_check_augment(module, &module->augment[i], 0, unres)) {
             goto error;
         }
         if (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1) {
diff --git a/src/parser_yang.h b/src/parser_yang.h
index a3f3db6..02c1049 100644
--- a/src/parser_yang.h
+++ b/src/parser_yang.h
@@ -31,10 +31,6 @@
 #define LYS_RPC_OUTPUT 0x02
 #define LYS_DATADEF 0x04
 #define LYS_TYPE_DEF 0x08
-#define CONFIG_INHERIT_DISABLE 0x00
-#define CONFIG_INHERIT_ENABLE  0x01
-#define CONFIG_IGNORE 0x02
-#define CONFIG_MASK 0x03
 #define LYS_CHOICE_DEFAULT 0x10
 #define LYS_NO_ERASE_IDENTITY 0x20
 #define LY_YANG_ARRAY_SIZE 8
diff --git a/src/parser_yin.c b/src/parser_yin.c
index c244ef8..4bdd09c 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -48,27 +48,27 @@
                            struct unres_schema *);
 
 static struct lys_node *read_yin_choice(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
-                                        int valid_config, struct unres_schema *unres);
+                                        int options, struct unres_schema *unres);
 static struct lys_node *read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
-                                      int valid_config, struct unres_schema *unres);
+                                      int options, struct unres_schema *unres);
 static struct lys_node *read_yin_anydata(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
                                          LYS_NODE type, int valid_config, struct unres_schema *unres);
 static struct lys_node *read_yin_container(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
-                                           int valid_config, struct unres_schema *unres);
+                                           int options, struct unres_schema *unres);
 static struct lys_node *read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
                                       int valid_config, struct unres_schema *unres);
 static struct lys_node *read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
                                           int valid_config, struct unres_schema *unres);
 static struct lys_node *read_yin_list(struct lys_module *module,struct lys_node *parent, struct lyxml_elem *yin,
-                                      int valid_config, struct unres_schema *unres);
+                                      int options, struct unres_schema *unres);
 static struct lys_node *read_yin_uses(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
-                                      struct unres_schema *unres);
+                                      int options, struct unres_schema *unres);
 static struct lys_node *read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
-                                          int valid_config, struct unres_schema *unres);
+                                          int options, struct unres_schema *unres);
 static struct lys_node *read_yin_rpc_action(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
-                                            struct unres_schema *unres);
+                                            int options, struct unres_schema *unres);
 static struct lys_node *read_yin_notif(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
-                                       struct unres_schema *unres);
+                                       int options, struct unres_schema *unres);
 static struct lys_when *read_yin_when(struct lys_module *module, struct lyxml_elem *yin, struct unres_schema *unres);
 
 /*
@@ -3018,7 +3018,7 @@
 /* logs directly */
 static int
 fill_yin_augment(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_node_augment *aug,
-                 struct unres_schema *unres)
+                 int options, struct unres_schema *unres)
 {
     const char *value;
     struct lyxml_elem *sub, *next;
@@ -3062,27 +3062,27 @@
 
         /* check allowed data sub-statements */
         } else if (!strcmp(sub->name, "container")) {
-            node = read_yin_container(module, (struct lys_node *)aug, sub, 1, unres);
+            node = read_yin_container(module, (struct lys_node *)aug, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf-list")) {
-            node = read_yin_leaflist(module, (struct lys_node *)aug, sub, 1, unres);
+            node = read_yin_leaflist(module, (struct lys_node *)aug, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf")) {
-            node = read_yin_leaf(module, (struct lys_node *)aug, sub, 1, unres);
+            node = read_yin_leaf(module, (struct lys_node *)aug, sub, options, unres);
         } else if (!strcmp(sub->name, "list")) {
-            node = read_yin_list(module, (struct lys_node *)aug, sub, 1, unres);
+            node = read_yin_list(module, (struct lys_node *)aug, sub, options, unres);
         } else if (!strcmp(sub->name, "uses")) {
-            node = read_yin_uses(module, (struct lys_node *)aug, sub, unres);
+            node = read_yin_uses(module, (struct lys_node *)aug, sub, options, unres);
         } else if (!strcmp(sub->name, "choice")) {
-            node = read_yin_choice(module, (struct lys_node *)aug, sub, 1, unres);
+            node = read_yin_choice(module, (struct lys_node *)aug, sub, options, unres);
         } else if (!strcmp(sub->name, "case")) {
-            node = read_yin_case(module, (struct lys_node *)aug, sub, 1, unres);
+            node = read_yin_case(module, (struct lys_node *)aug, sub, options, unres);
         } else if (!strcmp(sub->name, "anyxml")) {
-            node = read_yin_anydata(module, (struct lys_node *)aug, sub, LYS_ANYXML, 1, unres);
+            node = read_yin_anydata(module, (struct lys_node *)aug, sub, LYS_ANYXML, options, unres);
         } else if (!strcmp(sub->name, "anydata")) {
-            node = read_yin_anydata(module, (struct lys_node *)aug, sub, LYS_ANYDATA, 1, unres);
+            node = read_yin_anydata(module, (struct lys_node *)aug, sub, LYS_ANYDATA, options, unres);
         } else if (!strcmp(sub->name, "action")) {
-            node = read_yin_rpc_action(module, (struct lys_node *)aug, sub, unres);
+            node = read_yin_rpc_action(module, (struct lys_node *)aug, sub, options, unres);
         } else if (!strcmp(sub->name, "notification")) {
-            node = read_yin_notif(module, (struct lys_node *)aug, sub, unres);
+            node = read_yin_notif(module, (struct lys_node *)aug, sub, options, unres);
         } else {
             LOGVAL(LYE_INSTMT, LY_VLOG_NONE, NULL, sub->name);
             goto error;
@@ -3147,7 +3147,8 @@
     }
 
     /* check XPath dependencies */
-    if (aug->when && (unres_schema_add_node(module, unres, (struct lys_node *)aug, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && aug->when &&
+            (unres_schema_add_node(module, unres, (struct lys_node *)aug, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -3956,7 +3957,7 @@
 
 /* logs directly */
 static struct lys_node *
-read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
+read_yin_case(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
               struct unres_schema *unres)
 {
     struct lyxml_elem *sub, *next, root;
@@ -3978,7 +3979,7 @@
     retval = (struct lys_node *)cs;
 
     if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
-            OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_INHERIT : 0), unres)) {
+            OPT_IDENT | OPT_MODULE | (!(options & LYS_PARSE_OPT_CFG_MASK) ? OPT_CFG_INHERIT : 0), unres)) {
         goto error;
     }
 
@@ -4066,21 +4067,21 @@
     /* last part - process data nodes */
     LY_TREE_FOR_SAFE(root.child, next, sub) {
         if (!strcmp(sub->name, "container")) {
-            node = read_yin_container(module, retval, sub, valid_config, unres);
+            node = read_yin_container(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf-list")) {
-            node = read_yin_leaflist(module, retval, sub, valid_config, unres);
+            node = read_yin_leaflist(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf")) {
-            node = read_yin_leaf(module, retval, sub, valid_config, unres);
+            node = read_yin_leaf(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "list")) {
-            node = read_yin_list(module, retval, sub, valid_config, unres);
+            node = read_yin_list(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "choice")) {
-            node = read_yin_choice(module, retval, sub, valid_config, unres);
+            node = read_yin_choice(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "uses")) {
-            node = read_yin_uses(module, retval, sub, unres);
+            node = read_yin_uses(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "anyxml")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, options, unres);
         } else if (!strcmp(sub->name, "anydata")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, options, unres);
         }
         if (!node) {
             goto error;
@@ -4090,7 +4091,8 @@
     }
 
     /* check XPath dependencies */
-    if (cs->when && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && cs->when &&
+            (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -4108,7 +4110,7 @@
 
 /* logs directly */
 static struct lys_node *
-read_yin_choice(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
+read_yin_choice(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
                 struct unres_schema *unres)
 {
     struct lyxml_elem *sub, *next, *dflt = NULL;
@@ -4129,7 +4131,9 @@
     retval = (struct lys_node *)choice;
 
     if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
-            OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
+            OPT_IDENT | OPT_MODULE | ((options & LYS_PARSE_OPT_CFG_IGNORE) ? OPT_CFG_IGNORE :
+                (options & LYS_PARSE_OPT_CFG_NOINHERIT) ? OPT_CFG_PARSE : OPT_CFG_PARSE | OPT_CFG_INHERIT),
+            unres)) {
         goto error;
     }
 
@@ -4148,31 +4152,31 @@
             /* keep it for later processing, skip lyxml_free() */
             continue;
         } else if (!strcmp(sub->name, "container")) {
-            if (!(node = read_yin_container(module, retval, sub, valid_config, unres))) {
+            if (!(node = read_yin_container(module, retval, sub, options, unres))) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "leaf-list")) {
-            if (!(node = read_yin_leaflist(module, retval, sub, valid_config, unres))) {
+            if (!(node = read_yin_leaflist(module, retval, sub, options, unres))) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "leaf")) {
-            if (!(node = read_yin_leaf(module, retval, sub, valid_config, unres))) {
+            if (!(node = read_yin_leaf(module, retval, sub, options, unres))) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "list")) {
-            if (!(node = read_yin_list(module, retval, sub, valid_config, unres))) {
+            if (!(node = read_yin_list(module, retval, sub, options, unres))) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "case")) {
-            if (!(node = read_yin_case(module, retval, sub, valid_config, unres))) {
+            if (!(node = read_yin_case(module, retval, sub, options, unres))) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "anyxml")) {
-            if (!(node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres))) {
+            if (!(node = read_yin_anydata(module, retval, sub, LYS_ANYXML, options, unres))) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "anydata")) {
-            if (!(node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres))) {
+            if (!(node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, options, unres))) {
                 goto error;
             }
         } else if (!strcmp(sub->name, "default")) {
@@ -4229,7 +4233,7 @@
             /* skip lyxml_free() at the end of the loop, the sub node is processed later */
             continue;
         } else if (module->version >= 2 && !strcmp(sub->name, "choice")) {
-            if (!(node = read_yin_choice(module, retval, sub, valid_config, unres))) {
+            if (!(node = read_yin_choice(module, retval, sub, options, unres))) {
                 goto error;
             }
         } else {
@@ -4295,7 +4299,8 @@
     }
 
     /* check XPath dependencies */
-    if (choice->when && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && choice->when &&
+            (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -4312,7 +4317,7 @@
 /* logs directly */
 static struct lys_node *
 read_yin_anydata(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, LYS_NODE type,
-                 int valid_config, struct unres_schema *unres)
+                 int options, struct unres_schema *unres)
 {
     struct lys_node *retval;
     struct lys_node_anydata *anyxml;
@@ -4333,7 +4338,9 @@
     retval = (struct lys_node *)anyxml;
 
     if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
-            OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
+            OPT_IDENT | OPT_MODULE | ((options & LYS_PARSE_OPT_CFG_IGNORE) ? OPT_CFG_IGNORE :
+                (options & LYS_PARSE_OPT_CFG_NOINHERIT) ? OPT_CFG_PARSE : OPT_CFG_PARSE | OPT_CFG_INHERIT),
+            unres)) {
         goto error;
     }
 
@@ -4448,7 +4455,8 @@
     }
 
     /* check XPath dependencies */
-    if ((anyxml->when || anyxml->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (anyxml->when || anyxml->must_size) &&
+            (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -4463,7 +4471,7 @@
 
 /* logs directly */
 static struct lys_node *
-read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
+read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
               struct unres_schema *unres)
 {
     struct lys_node *retval;
@@ -4484,7 +4492,9 @@
     retval = (struct lys_node *)leaf;
 
     if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
-            OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
+            OPT_IDENT | OPT_MODULE | ((options & LYS_PARSE_OPT_CFG_IGNORE) ? OPT_CFG_IGNORE :
+                (options & LYS_PARSE_OPT_CFG_NOINHERIT) ? OPT_CFG_PARSE : OPT_CFG_PARSE | OPT_CFG_INHERIT),
+            unres)) {
         goto error;
     }
 
@@ -4658,7 +4668,8 @@
     }
 
     /* check XPath dependencies */
-    if ((leaf->when || leaf->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (leaf->when || leaf->must_size) &&
+            (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -4673,7 +4684,7 @@
 
 /* logs directly */
 static struct lys_node *
-read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
+read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
                   struct unres_schema *unres)
 {
     struct lys_node *retval;
@@ -4697,7 +4708,9 @@
     retval = (struct lys_node *)llist;
 
     if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
-            OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
+            OPT_IDENT | OPT_MODULE | ((options & LYS_PARSE_OPT_CFG_IGNORE) ? OPT_CFG_IGNORE :
+                (options & LYS_PARSE_OPT_CFG_NOINHERIT) ? OPT_CFG_PARSE : OPT_CFG_PARSE | OPT_CFG_INHERIT),
+            unres)) {
         goto error;
     }
 
@@ -4963,7 +4976,8 @@
     }
 
     /* check XPath dependencies */
-    if ((llist->when || llist->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (llist->when || llist->must_size) &&
+            (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -4978,7 +4992,7 @@
 
 /* logs directly */
 static struct lys_node *
-read_yin_list(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
+read_yin_list(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
               struct unres_schema *unres)
 {
     struct lys_node *retval, *node;
@@ -5006,7 +5020,9 @@
     retval = (struct lys_node *)list;
 
     if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
-            OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
+            OPT_IDENT | OPT_MODULE | ((options & LYS_PARSE_OPT_CFG_IGNORE) ? OPT_CFG_IGNORE :
+                (options & LYS_PARSE_OPT_CFG_NOINHERIT) ? OPT_CFG_PARSE : OPT_CFG_PARSE | OPT_CFG_INHERIT),
+            unres)) {
         goto error;
     }
 
@@ -5267,27 +5283,27 @@
     /* last part - process data nodes */
     LY_TREE_FOR_SAFE(root.child, next, sub) {
         if (!strcmp(sub->name, "container")) {
-            node = read_yin_container(module, retval, sub, valid_config, unres);
+            node = read_yin_container(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf-list")) {
-            node = read_yin_leaflist(module, retval, sub, valid_config, unres);
+            node = read_yin_leaflist(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf")) {
-            node = read_yin_leaf(module, retval, sub, valid_config, unres);
+            node = read_yin_leaf(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "list")) {
-            node = read_yin_list(module, retval, sub, valid_config, unres);
+            node = read_yin_list(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "choice")) {
-            node = read_yin_choice(module, retval, sub, valid_config, unres);
+            node = read_yin_choice(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "uses")) {
-            node = read_yin_uses(module, retval, sub, unres);
+            node = read_yin_uses(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "grouping")) {
-            node = read_yin_grouping(module, retval, sub, valid_config, unres);
+            node = read_yin_grouping(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "anyxml")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, options, unres);
         } else if (!strcmp(sub->name, "anydata")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, options, unres);
         } else if (!strcmp(sub->name, "action")) {
-            node = read_yin_rpc_action(module, retval, sub, unres);
+            node = read_yin_rpc_action(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "notification")) {
-            node = read_yin_notif(module, retval, sub, unres);
+            node = read_yin_notif(module, retval, sub, options, unres);
         } else {
             LOGINT;
             goto error;
@@ -5331,7 +5347,8 @@
     }
 
     /* check XPath dependencies */
-    if ((list->when || list->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (list->when || list->must_size) &&
+            (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -5352,7 +5369,7 @@
 
 /* logs directly */
 static struct lys_node *
-read_yin_container(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
+read_yin_container(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
                    struct unres_schema *unres)
 {
     struct lyxml_elem *sub, *next, root;
@@ -5377,7 +5394,9 @@
     retval = (struct lys_node *)cont;
 
     if (read_yin_common(module, parent, retval, LYEXT_PAR_NODE, yin,
-            OPT_IDENT | OPT_MODULE | (valid_config ? OPT_CFG_PARSE | OPT_CFG_INHERIT : OPT_CFG_IGNORE), unres)) {
+            OPT_IDENT | OPT_MODULE | ((options & LYS_PARSE_OPT_CFG_IGNORE) ? OPT_CFG_IGNORE :
+                (options & LYS_PARSE_OPT_CFG_NOINHERIT) ? OPT_CFG_PARSE : OPT_CFG_PARSE | OPT_CFG_INHERIT),
+            unres)) {
         goto error;
     }
 
@@ -5513,27 +5532,27 @@
     /* last part - process data nodes */
     LY_TREE_FOR_SAFE(root.child, next, sub) {
         if (!strcmp(sub->name, "container")) {
-            node = read_yin_container(module, retval, sub, valid_config, unres);
+            node = read_yin_container(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf-list")) {
-            node = read_yin_leaflist(module, retval, sub, valid_config, unres);
+            node = read_yin_leaflist(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf")) {
-            node = read_yin_leaf(module, retval, sub, valid_config, unres);
+            node = read_yin_leaf(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "list")) {
-            node = read_yin_list(module, retval, sub, valid_config, unres);
+            node = read_yin_list(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "choice")) {
-            node = read_yin_choice(module, retval, sub, valid_config, unres);
+            node = read_yin_choice(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "uses")) {
-            node = read_yin_uses(module, retval, sub, unres);
+            node = read_yin_uses(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "grouping")) {
-            node = read_yin_grouping(module, retval, sub, valid_config, unres);
+            node = read_yin_grouping(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "anyxml")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, options, unres);
         } else if (!strcmp(sub->name, "anydata")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, options, unres);
         } else if (!strcmp(sub->name, "action")) {
-            node = read_yin_rpc_action(module, retval, sub, unres);
+            node = read_yin_rpc_action(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "notification")) {
-            node = read_yin_notif(module, retval, sub, unres);
+            node = read_yin_notif(module, retval, sub, options, unres);
         }
         if (!node) {
             goto error;
@@ -5543,7 +5562,8 @@
     }
 
     /* check XPath dependencies */
-    if ((cont->when || cont->must_size) && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && (cont->when || cont->must_size) &&
+            (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -5561,7 +5581,7 @@
 
 /* logs directly */
 static struct lys_node *
-read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int valid_config,
+read_yin_grouping(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, int options,
                   struct unres_schema *unres)
 {
     struct lyxml_elem *sub, *next, root;
@@ -5666,29 +5686,30 @@
     if (!root.child) {
         LOGWRN("Grouping \"%s\" without children.", retval->name);
     }
+    options |= LYS_PARSE_OPT_INGRP;
     LY_TREE_FOR_SAFE(root.child, next, sub) {
         if (!strcmp(sub->name, "container")) {
-            node = read_yin_container(module, retval, sub, valid_config, unres);
+            node = read_yin_container(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf-list")) {
-            node = read_yin_leaflist(module, retval, sub, valid_config, unres);
+            node = read_yin_leaflist(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf")) {
-            node = read_yin_leaf(module, retval, sub, valid_config, unres);
+            node = read_yin_leaf(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "list")) {
-            node = read_yin_list(module, retval, sub, valid_config, unres);
+            node = read_yin_list(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "choice")) {
-            node = read_yin_choice(module, retval, sub, valid_config, unres);
+            node = read_yin_choice(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "uses")) {
-            node = read_yin_uses(module, retval, sub, unres);
+            node = read_yin_uses(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "grouping")) {
-            node = read_yin_grouping(module, retval, sub, valid_config, unres);
+            node = read_yin_grouping(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "anyxml")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, valid_config, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, options, unres);
         } else if (!strcmp(sub->name, "anydata")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, valid_config, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, options, unres);
         } else if (!strcmp(sub->name, "action")) {
-            node = read_yin_rpc_action(module, retval, sub, unres);
+            node = read_yin_rpc_action(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "notification")) {
-            node = read_yin_notif(module, retval, sub, unres);
+            node = read_yin_notif(module, retval, sub, options, unres);
         }
         if (!node) {
             goto error;
@@ -5712,7 +5733,7 @@
 /* logs directly */
 static struct lys_node *
 read_yin_input_output(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
-                      struct unres_schema *unres)
+                      int options, struct unres_schema *unres)
 {
     struct lyxml_elem *sub, *next, root;
     struct lys_node *node = NULL;
@@ -5833,25 +5854,26 @@
     }
 
     /* last part - process data nodes */
+    options |= LYS_PARSE_OPT_CFG_IGNORE;
     LY_TREE_FOR_SAFE(root.child, next, sub) {
         if (!strcmp(sub->name, "container")) {
-            node = read_yin_container(module, retval, sub, 0, unres);
+            node = read_yin_container(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf-list")) {
-            node = read_yin_leaflist(module, retval, sub, 0, unres);
+            node = read_yin_leaflist(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf")) {
-            node = read_yin_leaf(module, retval, sub, 0, unres);
+            node = read_yin_leaf(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "list")) {
-            node = read_yin_list(module, retval, sub, 0, unres);
+            node = read_yin_list(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "choice")) {
-            node = read_yin_choice(module, retval, sub, 0, unres);
+            node = read_yin_choice(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "uses")) {
-            node = read_yin_uses(module, retval, sub, unres);
+            node = read_yin_uses(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "grouping")) {
-            node = read_yin_grouping(module, retval, sub, 0, unres);
+            node = read_yin_grouping(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "anyxml")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, 0, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, options, unres);
         } else if (!strcmp(sub->name, "anydata")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, 0, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, options, unres);
         }
         if (!node) {
             goto error;
@@ -5861,7 +5883,8 @@
     }
 
     /* check XPath dependencies */
-    if (inout->must_size && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && inout->must_size &&
+            (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -5880,7 +5903,7 @@
 /* logs directly */
 static struct lys_node *
 read_yin_notif(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
-               struct unres_schema *unres)
+               int options, struct unres_schema *unres)
 {
     struct lyxml_elem *sub, *next, root;
     struct lys_node *node = NULL;
@@ -6015,25 +6038,26 @@
     }
 
     /* last part - process data nodes */
+    options |= LYS_PARSE_OPT_CFG_IGNORE;
     LY_TREE_FOR_SAFE(root.child, next, sub) {
         if (!strcmp(sub->name, "container")) {
-            node = read_yin_container(module, retval, sub, 0, unres);
+            node = read_yin_container(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf-list")) {
-            node = read_yin_leaflist(module, retval, sub, 0, unres);
+            node = read_yin_leaflist(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "leaf")) {
-            node = read_yin_leaf(module, retval, sub, 0, unres);
+            node = read_yin_leaf(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "list")) {
-            node = read_yin_list(module, retval, sub, 0, unres);
+            node = read_yin_list(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "choice")) {
-            node = read_yin_choice(module, retval, sub, 0, unres);
+            node = read_yin_choice(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "uses")) {
-            node = read_yin_uses(module, retval, sub, unres);
+            node = read_yin_uses(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "grouping")) {
-            node = read_yin_grouping(module, retval, sub, 0, unres);
+            node = read_yin_grouping(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "anyxml")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, 0, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYXML, options, unres);
         } else if (!strcmp(sub->name, "anydata")) {
-            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, 0, unres);
+            node = read_yin_anydata(module, retval, sub, LYS_ANYDATA, options, unres);
         }
         if (!node) {
             goto error;
@@ -6043,7 +6067,8 @@
     }
 
     /* check XPath dependencies */
-    if (notif->must_size && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && notif->must_size &&
+            (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -6062,7 +6087,7 @@
 /* logs directly */
 static struct lys_node *
 read_yin_rpc_action(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
-                    struct unres_schema *unres)
+                    int options, struct unres_schema *unres)
 {
     struct lyxml_elem *sub, *next, root;
     struct lys_node *node = NULL;
@@ -6202,9 +6227,9 @@
     /* last part - process data nodes */
     LY_TREE_FOR_SAFE(root.child, next, sub) {
         if (!strcmp(sub->name, "grouping")) {
-            node = read_yin_grouping(module, retval, sub, 0, unres);
+            node = read_yin_grouping(module, retval, sub, options, unres);
         } else if (!strcmp(sub->name, "input") || !strcmp(sub->name, "output")) {
-            node = read_yin_input_output(module, retval, sub, unres);
+            node = read_yin_input_output(module, retval, sub, options, unres);
         }
         if (!node) {
             goto error;
@@ -6232,7 +6257,8 @@
  * we just get information but we do not apply augment or refine to it.
  */
 static struct lys_node *
-read_yin_uses(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct unres_schema *unres)
+read_yin_uses(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin,
+              int options, struct unres_schema *unres)
 {
     struct lyxml_elem *sub, *next;
     struct lys_node *retval;
@@ -6345,7 +6371,7 @@
                 goto error;
             }
         } else if (!strcmp(sub->name, "augment")) {
-            r = fill_yin_augment(module, retval, sub, &uses->augment[uses->augment_size], unres);
+            r = fill_yin_augment(module, retval, sub, &uses->augment[uses->augment_size], options, unres);
             uses->augment_size++;
             if (r) {
                 goto error;
@@ -6364,7 +6390,8 @@
     }
 
     /* check XPath dependencies */
-    if (uses->when && (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
+    if (!(options & LYS_PARSE_OPT_INGRP) && uses->when &&
+            (unres_schema_add_node(module, unres, retval, UNRES_XPATH, NULL) == -1)) {
         goto error;
     }
 
@@ -6979,7 +7006,7 @@
      * main module data tree.
      */
     LY_TREE_FOR_SAFE(grps.child, next, child) {
-        node = read_yin_grouping(trg, NULL, child, 1, unres);
+        node = read_yin_grouping(trg, NULL, child, 0, unres);
         if (!node) {
             goto error;
         }
@@ -6991,25 +7018,25 @@
     LY_TREE_FOR_SAFE(root.child, next, child) {
 
         if (!strcmp(child->name, "container")) {
-            node = read_yin_container(trg, NULL, child, 1, unres);
+            node = read_yin_container(trg, NULL, child, 0, unres);
         } else if (!strcmp(child->name, "leaf-list")) {
-            node = read_yin_leaflist(trg, NULL, child, 1, unres);
+            node = read_yin_leaflist(trg, NULL, child, 0, unres);
         } else if (!strcmp(child->name, "leaf")) {
-            node = read_yin_leaf(trg, NULL, child, 1, unres);
+            node = read_yin_leaf(trg, NULL, child, 0, unres);
         } else if (!strcmp(child->name, "list")) {
-            node = read_yin_list(trg, NULL, child, 1, unres);
+            node = read_yin_list(trg, NULL, child, 0, unres);
         } else if (!strcmp(child->name, "choice")) {
-            node = read_yin_choice(trg, NULL, child, 1, unres);
+            node = read_yin_choice(trg, NULL, child, 0, unres);
         } else if (!strcmp(child->name, "uses")) {
-            node = read_yin_uses(trg, NULL, child, unres);
+            node = read_yin_uses(trg, NULL, child, 0, unres);
         } else if (!strcmp(child->name, "anyxml")) {
-            node = read_yin_anydata(trg, NULL, child, LYS_ANYXML, 1, unres);
+            node = read_yin_anydata(trg, NULL, child, LYS_ANYXML, 0, unres);
         } else if (!strcmp(child->name, "anydata")) {
-            node = read_yin_anydata(trg, NULL, child, LYS_ANYDATA, 1, unres);
+            node = read_yin_anydata(trg, NULL, child, LYS_ANYDATA, 0, unres);
         } else if (!strcmp(child->name, "rpc")) {
-            node = read_yin_rpc_action(trg, NULL, child, unres);
+            node = read_yin_rpc_action(trg, NULL, child, 0, unres);
         } else if (!strcmp(child->name, "notification")) {
-            node = read_yin_notif(trg, NULL, child, unres);
+            node = read_yin_notif(trg, NULL, child, 0, unres);
         }
         if (!node) {
             goto error;
@@ -7020,7 +7047,7 @@
 
     /* ... and finally augments (last, so we can augment our data, for instance) */
     LY_TREE_FOR_SAFE(augs.child, next, child) {
-        r = fill_yin_augment(trg, NULL, child, &trg->augment[trg->augment_size], unres);
+        r = fill_yin_augment(trg, NULL, child, &trg->augment[trg->augment_size], 0, unres);
         trg->augment_size++;
 
         if (r) {
@@ -7643,7 +7670,7 @@
 #define YIN_EXTCOMPLEX_PARSE_SNODE(STMT, FUNC, ARGS...)                              \
     pp = (void**)yin_getplace_for_extcomplex_node(node, ext, STMT);                  \
     if (!pp) { goto error; }                                                         \
-    if (!FUNC(mod, (struct lys_node*)ext, node, ##ARGS, unres)) { goto error; }
+    if (!FUNC(mod, (struct lys_node*)ext, node, ##ARGS, LYS_PARSE_OPT_CFG_NOINHERIT, unres)) { goto error; }
 #define YIN_EXTCOMPLEX_PARSE_RESTR(STMT)                                             \
     YIN_EXTCOMPLEX_GETPLACE(STMT, struct lys_restr*);                                \
     GETVAL(value, node, "value");                                                    \
@@ -7996,27 +8023,27 @@
         } else if (!strcmp(node->name, "action")) {
             YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_ACTION, read_yin_rpc_action);
         } else if (!strcmp(node->name, "anydata")) {
-            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_ANYDATA, read_yin_anydata, LYS_ANYDATA, 0);
+            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_ANYDATA, read_yin_anydata, LYS_ANYDATA);
         } else if (!strcmp(node->name, "anyxml")) {
-            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_ANYXML, read_yin_anydata, LYS_ANYXML, 0);
+            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_ANYXML, read_yin_anydata, LYS_ANYXML);
         } else if (!strcmp(node->name, "case")) {
-            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_CASE, read_yin_case, 0);
+            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_CASE, read_yin_case);
         } else if (!strcmp(node->name, "choice")) {
-            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_CHOICE, read_yin_choice, 0);
+            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_CHOICE, read_yin_choice);
         } else if (!strcmp(node->name, "container")) {
-            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_CONTAINER, read_yin_container, 0);
+            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_CONTAINER, read_yin_container);
         } else if (!strcmp(node->name, "grouping")) {
-            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_GROUPING, read_yin_grouping, 0);
+            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_GROUPING, read_yin_grouping);
         } else if (!strcmp(node->name, "output")) {
             YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_OUTPUT, read_yin_input_output);
         } else if (!strcmp(node->name, "input")) {
             YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_INPUT, read_yin_input_output);
         } else if (!strcmp(node->name, "leaf")) {
-            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_LEAF, read_yin_leaf, 0);
+            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_LEAF, read_yin_leaf);
         } else if (!strcmp(node->name, "leaf-list")) {
-            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_LEAFLIST, read_yin_leaflist, 0);
+            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_LEAFLIST, read_yin_leaflist);
         } else if (!strcmp(node->name, "list")) {
-            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_LIST, read_yin_list, 0);
+            YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_LIST, read_yin_list);
         } else if (!strcmp(node->name, "notification")) {
             YIN_EXTCOMPLEX_PARSE_SNODE(LY_STMT_NOTIFICATION, read_yin_notif);
         } else if (!strcmp(node->name, "uses")) {
diff --git a/src/tree_schema.c b/src/tree_schema.c
index b0f7a18..6a96e8b 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -3189,7 +3189,7 @@
     result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize);
     if (finalize) {
         /* check xpath expressions in the instantiated tree */
-        for (iter = next = parent->child; iter; iter = next) {
+        for (iter = next = result; iter; iter = next) {
             if (lys_has_xpath(iter) && unres_schema_add_node(module, unres, iter, UNRES_XPATH, NULL) == -1) {
                 /* invalid xpath */
                 return NULL;
@@ -3204,12 +3204,16 @@
             }
             if (!next) {
                 /* no children, try siblings */
+                if (iter == result) {
+                    /* we are done, no next element to process */
+                    break;
+                }
                 next = iter->next;
             }
             while (!next) {
                 /* parent is already processed, go to its sibling */
                 iter = lys_parent(iter);
-                if (iter == parent) {
+                if (lys_parent(iter) == lys_parent(result)) {
                     /* we are done, no next element to process */
                     break;
                 }