xpath CHANGE deref() support for schema leafref
diff --git a/src/tree_schema_compile.c b/src/tree_schema_compile.c
index d1c04c7..d79c81b 100644
--- a/src/tree_schema_compile.c
+++ b/src/tree_schema_compile.c
@@ -2676,10 +2676,12 @@
  * @param[in] ctx Compile context
  * @param[in] startnode Path context node (where the leafref path begins/is placed).
  * @param[in] leafref Leafref to validate.
+ * @param[out] target Optional resolved leafref target.
  * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
  */
-static LY_ERR
-lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
+LY_ERR
+lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref,
+                             const struct lysc_node **target)
 {
     const struct lysc_node *node = NULL, *parent = NULL, *tmp_parent;
     const struct lys_module *mod;
@@ -2812,6 +2814,9 @@
 
     ctx->path_len = 1;
     ctx->path[1] = '\0';
+    if (target) {
+        *target = node;
+    }
     return LY_SUCCESS;
 }
 
@@ -7050,13 +7055,13 @@
             type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
             if (type->basetype == LY_TYPE_LEAFREF) {
                 /* validate the path */
-                LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type), error);
+                LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type, NULL), error);
             } else if (type->basetype == LY_TYPE_UNION) {
                 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
                     if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
                         /* validate the path */
                         LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
-                                                                         (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]),
+                                                                         (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL),
                                       error);
                     }
                 }
diff --git a/src/tree_schema_internal.h b/src/tree_schema_internal.h
index de02622..efa05f8 100644
--- a/src/tree_schema_internal.h
+++ b/src/tree_schema_internal.h
@@ -658,6 +658,17 @@
 LY_ERR lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code);
 
 /**
+ * @brief Validate the leafref path.
+ * @param[in] ctx Compile context
+ * @param[in] startnode Path context node (where the leafref path begins/is placed).
+ * @param[in] leafref Leafref to validate.
+ * @param[out] target Optional resolved leafref target.
+ * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
+ */
+LY_ERR lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref,
+                                    const struct lysc_node **target);
+
+/**
  * @brief Internal wrapper around lys_compile_extension() to be able to prepare list of compiled extension definitions
  * even for the parsed (not-implemented) module - see lys_module::off_extensions.
  *
diff --git a/src/xpath.c b/src/xpath.c
index 930b8b2..968cb19 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -3531,8 +3531,10 @@
 static LY_ERR
 xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
 {
+    struct lysc_ctx cctx;
     struct lyd_node_term *leaf;
     struct lysc_node_leaf *sleaf;
+    const struct lysc_node *target;
     const struct lyd_node *node;
     char *errmsg = NULL;
     const char *val;
@@ -3552,6 +3554,15 @@
             rc = LY_EINVAL;
         }
         set_scnode_clear_ctx(set);
+        if ((rc == LY_SUCCESS) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
+            cctx.ctx = set->ctx;
+            rc = lys_compile_leafref_validate(&cctx, (struct lysc_node *)sleaf, (struct lysc_type_leafref *)sleaf->type, &target);
+            /* it was already validated, it must succeed */
+            if (rc == LY_SUCCESS) {
+                set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
+            }
+        }
+
         return rc;
     }