schema compile BUGFIX getting parent of in/out nodes

Also a redundant function was removed.
diff --git a/src/schema_compile_amend.c b/src/schema_compile_amend.c
index 1dfed53..faf2c84 100644
--- a/src/schema_compile_amend.c
+++ b/src/schema_compile_amend.c
@@ -1871,7 +1871,7 @@
 
             if (aug_p->when) {
                 /* pass augment's when to all the children */
-                ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target), node, &when_shared);
+                ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target), node, &when_shared);
                 LY_CHECK_GOTO(ret, cleanup);
 
                 if ((node->nodetype == LYS_CONTAINER) && !(node->flags & LYS_PRESENCE)) {
@@ -1918,7 +1918,7 @@
         if (aug_p->when) {
             /* inherit when */
             LY_ARRAY_FOR(*actions, u) {
-                ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target),
+                ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target),
                         (struct lysc_node *)&(*actions)[u], &when_shared);
                 LY_CHECK_GOTO(ret, cleanup);
             }
@@ -1939,7 +1939,7 @@
         if (aug_p->when) {
             /* inherit when */
             LY_ARRAY_FOR(*notifs, u) {
-                ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_xpath_context(target),
+                ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, lysc_data_node(target),
                         (struct lysc_node *)&(*notifs)[u], &when_shared);
                 LY_CHECK_GOTO(ret, cleanup);
             }
diff --git a/src/schema_compile_node.c b/src/schema_compile_node.c
index ba5b96c..3714754 100644
--- a/src/schema_compile_node.c
+++ b/src/schema_compile_node.c
@@ -217,14 +217,6 @@
     return NULL;
 }
 
-struct lysc_node *
-lysc_xpath_context(struct lysc_node *start)
-{
-    for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC | LYS_ACTION | LYS_NOTIF));
-            start = start->parent) {}
-    return start;
-}
-
 /**
  * @brief Compile information from the when statement
  *
@@ -2371,7 +2363,7 @@
     return ret;
 }
 
-/*
+/**
  * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
  * @param[in] ctx Compile context.
  * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
@@ -3424,7 +3416,7 @@
         for (i = 0; i < uses_child_set.count; ++i) {
             child = uses_child_set.snodes[i];
 
-            ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent), child, &when_shared);
+            ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
             LY_CHECK_GOTO(ret, cleanup);
         }
     }
@@ -3444,7 +3436,7 @@
         if (uses_p->when) {
             /* inherit when */
             LY_ARRAY_FOR(*actions, u) {
-                ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent),
+                ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent),
                         (struct lysc_node *)&(*actions)[u], &when_shared);
                 LY_CHECK_GOTO(ret, cleanup);
             }
@@ -3466,7 +3458,7 @@
         if (uses_p->when) {
             /* inherit when */
             LY_ARRAY_FOR(*notifs, u) {
-                ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_xpath_context(parent),
+                ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent),
                         (struct lysc_node *)&(*notifs)[u], &when_shared);
                 LY_CHECK_GOTO(ret, cleanup);
             }
@@ -3778,7 +3770,7 @@
 
     if (pnode->when) {
         /* compile when */
-        ret = lys_compile_when(ctx, pnode->when, pnode->flags, lysc_xpath_context(node), node, NULL);
+        ret = lys_compile_when(ctx, pnode->when, pnode->flags, lysc_data_node(node), node, NULL);
         LY_CHECK_GOTO(ret, cleanup);
     }
 
diff --git a/src/schema_compile_node.h b/src/schema_compile_node.h
index 38ad6ab..44fb5a9 100644
--- a/src/schema_compile_node.h
+++ b/src/schema_compile_node.h
@@ -27,14 +27,6 @@
 struct lysc_ctx;
 
 /**
- * @brief Get the XPath context node for the given schema node.
- * @param[in] start The schema node where the XPath expression appears.
- * @return The context node to evaluate XPath expression in given schema node.
- * @return NULL in case the context node is the root node.
- */
-struct lysc_node *lysc_xpath_context(struct lysc_node *start);
-
-/**
  * @brief Compile information from the when statement by either standard compilation or by reusing
  * another compiled when structure.
  *
diff --git a/src/tree_schema_helpers.c b/src/tree_schema_helpers.c
index 71d8101..5bbe14b 100644
--- a/src/tree_schema_helpers.c
+++ b/src/tree_schema_helpers.c
@@ -1656,11 +1656,15 @@
 }
 
 const struct lysc_node *
-lysc_data_parent(const struct lysc_node *schema)
+lysc_data_node(const struct lysc_node *schema)
 {
     const struct lysc_node *parent;
 
-    for (parent = schema->parent; parent && (parent->nodetype & (LYS_CHOICE | LYS_CASE)); parent = parent->parent) {}
+    parent = schema;
+    while (parent && !(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_RPC
+            | LYS_ACTION | LYS_NOTIF))) {
+        parent = lysc_node_parent_full(parent);
+    }
 
     return parent;
 }
diff --git a/src/tree_schema_internal.h b/src/tree_schema_internal.h
index a8c8b05..1da203d 100644
--- a/src/tree_schema_internal.h
+++ b/src/tree_schema_internal.h
@@ -700,12 +700,17 @@
         size_t buflen);
 
 /**
- * @brief Get schema parent that can be instantiated in data. In other words, skip any choice or case nodes.
+ * @brief Get nearest @p schema parent (including the node itself) that can be instantiated in data.
  *
- * @param[in] schema Schema node to get the parent for.
- * @return Parent, NULL if top-level (in data).
+ * @param[in] schema Schema node to get the nearest data node for.
+ * @return Schema data node, NULL if top-level (in data).
  */
-const struct lysc_node *lysc_data_parent(const struct lysc_node *schema);
+const struct lysc_node *lysc_data_node(const struct lysc_node *schema);
+
+/**
+ * @brief Same as ::lysc_data_node() but never returns the node itself.
+ */
+#define lysc_data_parent(schema) lysc_data_node(lysc_node_parent_full(schema))
 
 /**
  * @brief Learn whether a node is inside an operation output.