tree data BUGFIX lyd_owner_module behavior now consistent

It is meant to return the owner module for both
data and opaque nodes. For node modules there
is a new function now.
diff --git a/src/tree_data_common.c b/src/tree_data_common.c
index f2a37a2..d0d3602 100644
--- a/src/tree_data_common.c
+++ b/src/tree_data_common.c
@@ -300,7 +300,12 @@
         return NULL;
     }
 
+    while (!node->schema && node->parent) {
+        node = lyd_parent(node);
+    }
+
     if (!node->schema) {
+        /* top-level opaque node */
         opaq = (struct lyd_node_opaq *)node;
         switch (opaq->format) {
         case LY_VALUE_XML:
@@ -317,13 +322,46 @@
             return NULL;
         }
 
-        /* try a parent */
-        return lyd_owner_module(lyd_parent(node));
+        return NULL;
     }
 
     return lysc_owner_module(node->schema);
 }
 
+LIBYANG_API_DEF const struct lys_module *
+lyd_node_module(const struct lyd_node *node)
+{
+    const struct lyd_node_opaq *opaq;
+
+    while (node) {
+        /* data node */
+        if (node->schema) {
+            return node->schema->module;
+        }
+
+        /* opaque node */
+        opaq = (struct lyd_node_opaq *)node;
+        switch (opaq->format) {
+        case LY_VALUE_XML:
+            if (opaq->name.module_ns) {
+                return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
+            }
+            break;
+        case LY_VALUE_JSON:
+            if (opaq->name.module_name) {
+                return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
+            }
+            break;
+        default:
+            break;
+        }
+
+        node = lyd_parent(node);
+    }
+
+    return NULL;
+}
+
 void
 lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod)
 {
@@ -1072,7 +1110,7 @@
         for (iter = node; lyd_parent(iter) != prev_iter; iter = lyd_parent(iter)) {}
 
         /* get module */
-        mod = lyd_owner_module(iter);
+        mod = lyd_node_module(iter);
         if (!mod) {
             /* unknown module, no schema node */
             schema = NULL;