data tree BUGFIX searching for mandatory elements

fix searching for instance data if the mandatory element is inside
a (chain of) non-presence containers
diff --git a/src/tree.c b/src/tree.c
index 764f91e..5a046e6 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -139,8 +139,10 @@
 static struct lys_node *
 check_mand_check(struct lys_node *node, struct lys_node *stop, struct lyd_node *data)
 {
-    struct lys_node *siter = NULL, *parent;
+    struct lys_node *siter = NULL, *parent = NULL;
     struct lyd_node *diter = NULL;
+    struct lyd_set *set = NULL;
+    unsigned int i;
     uint32_t minmax;
 
     if (node->flags & LYS_MAND_TRUE) {
@@ -179,17 +181,42 @@
                        /* 7.6.5, rule 1, checking presence is not needed
                         * since it is done in check_mand_getnext()
                         */
+                       lyd_set_free(set);
                        return NULL;
                    }
+                   /* add the parent to the list for searching in data tree */
+                   if (!set) {
+                       set = lyd_set_new();
+                   }
+                   lyd_set_add(set, (struct lyd_node *)parent);
                }
            }
 
            /* search for instance */
+           if (set) {
+               for (i = 0; i < set->number; i++) {
+                   LY_TREE_FOR(data->child, diter) {
+                       if (diter->schema == (struct lys_node *)(set->set[i])) {
+                           break;
+                       }
+                   }
+                   if (!diter) {
+                       /* instance not found */
+                       node = (struct lys_node *)(set->set[i]);
+                       lyd_set_free(set);
+                       return node;
+                   }
+                   data = diter;
+               }
+               lyd_set_free(set);
+           }
+
            LY_TREE_FOR(data->child, diter) {
                if (diter->schema == node) {
                    return NULL;
                }
            }
+
            /* instance not found */
            /* 7.6.5, rule 3 (or 2) */
            /* 7.9.4, rule 2 */