tree data UPDATE print opaque node modules
diff --git a/src/tree_data.c b/src/tree_data.c
index 46279a8..b12580a 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -4166,8 +4166,8 @@
     ly_bool is_static = 0;
     uint32_t i, depth;
     size_t bufused = 0, len;
-    const struct lyd_node *iter;
-    const struct lys_module *mod;
+    const struct lyd_node *iter, *parent;
+    const struct lys_module *mod, *prev_mod;
     LY_ERR rc = LY_SUCCESS;
 
     LY_CHECK_ARG_RET(NULL, node, NULL);
@@ -4191,11 +4191,12 @@
             /* find the right node */
             for (iter = node, i = 1; i < depth; iter = lyd_parent(iter), ++i) {}
 iter_print:
-            /* print prefix and name */
-            mod = NULL;
-            if (iter->schema && (!iter->parent || !iter->parent->schema ||
-                    (iter->schema->module != iter->parent->schema->module))) {
-                mod = iter->schema->module;
+            /* get the module */
+            mod = iter->schema ? iter->schema->module : lyd_owner_module(iter);
+            parent = lyd_parent(iter);
+            prev_mod = (parent && parent->schema) ? parent->schema->module : lyd_owner_module(parent);
+            if (prev_mod == mod) {
+                mod = NULL;
             }
 
             /* realloc string */