data tree REFACTOR lyd_has_when -> lysc_has_when

Needed to be called even with no existing data node.
diff --git a/src/tree_data.h b/src/tree_data.h
index 2f48838..133264a 100644
--- a/src/tree_data.h
+++ b/src/tree_data.h
@@ -88,7 +88,6 @@
  * - ::lyd_child_no_keys()
  * - ::lyd_parent()
  * - ::lyd_owner_module()
- * - ::lyd_has_when()
  * - ::lyd_find_xpath()
  * - ::lyd_find_sibling_val()
  * - ::lyd_find_sibling_first()
@@ -772,18 +771,6 @@
 const struct lys_module *lyd_owner_module(const struct lyd_node *node);
 
 /**
- * @brief Check whether this data node existence depends on any when conditions. This node's schema node and
- * any direct parent choice and case schema nodes are also examined for when conditions.
- *
- * Be careful, this function is not recursive and checks only conditions that apply to this node directly.
- * Meaning if there are any conditions associated with any parent of @p node, they are not returned.
- *
- * @param[in] node Data node to examine.
- * @return When condition associated with the node, NULL if there is none.
- */
-const struct lysc_when *lyd_has_when(const struct lyd_node *node);
-
-/**
  * @brief Check whether a node value equals to its default one.
  *
  * @param[in] node Term node to test.
diff --git a/src/tree_data_helpers.c b/src/tree_data_helpers.c
index 2b1b5df..c2134c0 100644
--- a/src/tree_data_helpers.c
+++ b/src/tree_data_helpers.c
@@ -148,35 +148,28 @@
 lyd_owner_module(const struct lyd_node *node)
 {
     const struct lysc_node *schema;
+    const struct lyd_node_opaq *opaq;
 
-    if (!node || !node->schema) {
+    if (!node) {
         return NULL;
     }
 
+    if (!node->schema) {
+        opaq = (struct lyd_node_opaq *)node;
+        switch (opaq->format) {
+        case LY_PREF_XML:
+            return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
+        case LY_PREF_JSON:
+            return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
+        default:
+            return NULL;
+        }
+    }
+
     for (schema = node->schema; schema->parent; schema = schema->parent) {}
     return schema->module;
 }
 
-API const struct lysc_when *
-lyd_has_when(const struct lyd_node *node)
-{
-    const struct lysc_node *schema;
-
-    if (!node || !node->schema) {
-        return NULL;
-    }
-
-    schema = node->schema;
-    do {
-        if (schema->when) {
-            return *schema->when;
-        }
-        schema = schema->parent;
-    } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
-
-    return NULL;
-}
-
 const struct lys_module *
 lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t *i,
         struct lyd_node **first)
diff --git a/src/tree_schema.h b/src/tree_schema.h
index 9458541..97c079c 100644
--- a/src/tree_schema.h
+++ b/src/tree_schema.h
@@ -105,6 +105,8 @@
  *
  * - ::lysc_set_private()
  *
+ * - ::lysc_has_when()
+ *
  * - ::lysc_node_children()
  * - ::lysc_node_children_full()
  * - ::lysc_node_parent_full()
@@ -1763,6 +1765,18 @@
     ((!lysc_node || !(lysc_node->nodetype & (LYS_LEAF)) || !(lysc_node->flags & LYS_KEY)) ? 0 : 1)
 
 /**
+ * @brief Check whether the schema node data instance existence depends on any when conditions.
+ * This node and any direct parent choice and case schema nodes are also examined for when conditions.
+ *
+ * Be careful, this function is not recursive and checks only conditions that apply to this node directly.
+ * Meaning if there are any conditions associated with any data parent instance of @p node, they are not returned.
+ *
+ * @param[in] node Schema node to examine.
+ * @return When condition associated with the node data instance, NULL if there is none.
+ */
+const struct lysc_when *lysc_has_when(const struct lysc_node *node);
+
+/**
  * @brief Get the groupings sized array of the given (parsed) schema node.
  * Decides the node's type and in case it has a groupings array, returns it.
  * @param[in] node Node to examine.
diff --git a/src/tree_schema_helpers.c b/src/tree_schema_helpers.c
index 9b23c94..2efc351 100644
--- a/src/tree_schema_helpers.c
+++ b/src/tree_schema_helpers.c
@@ -981,6 +981,23 @@
     return LY_SUCCESS;
 }
 
+API const struct lysc_when *
+lysc_has_when(const struct lysc_node *node)
+{
+    if (!node) {
+        return NULL;
+    }
+
+    do {
+        if (node->when) {
+            return *node->when;
+        }
+        node = node->parent;
+    } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
+
+    return NULL;
+}
+
 API const char *
 lys_nodetype2str(uint16_t nodetype)
 {
diff --git a/src/xpath.c b/src/xpath.c
index 2defc5a..aa060fc 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -5390,7 +5390,7 @@
     }
 
     /* when check */
-    if (!(options & LYXP_IGNORE_WHEN) && lyd_has_when(node) && !(node->flags & LYD_WHEN_TRUE)) {
+    if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
         return LY_EINCOMPLETE;
     }
 
@@ -5586,7 +5586,7 @@
         }
 
         /* when check */
-        if (!(options & LYXP_IGNORE_WHEN) && sub && lyd_has_when(sub) && !(sub->flags & LYD_WHEN_TRUE)) {
+        if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
             ret = LY_EINCOMPLETE;
             goto cleanup;
         }
@@ -6118,7 +6118,7 @@
         }
 
         /* when check */
-        if (!(options & LYXP_IGNORE_WHEN) && lyd_has_when(iter) && !(iter->flags & LYD_WHEN_TRUE)) {
+        if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
             return LY_EINCOMPLETE;
         }
 
@@ -6327,7 +6327,7 @@
         }
 
         /* when check */
-        if (!(options & LYXP_IGNORE_WHEN) && lyd_has_when(new_node) && !(new_node->flags & LYD_WHEN_TRUE)) {
+        if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && !(new_node->flags & LYD_WHEN_TRUE)) {
             return LY_EINCOMPLETE;
         }