context FEATURE ly_ctx_get_node() function
diff --git a/src/context.c b/src/context.c
index cd3accb..b694a46 100644
--- a/src/context.c
+++ b/src/context.c
@@ -30,12 +30,14 @@
#include "hash_table.h"
#include "parser.h"
#include "parser_data.h"
+#include "path.h"
#include "plugins_types.h"
#include "set.h"
#include "tree.h"
#include "tree_data.h"
#include "tree_schema.h"
#include "tree_schema_internal.h"
+#include "xpath.h"
#define LY_INTERNAL_MODS_COUNT 6
@@ -527,6 +529,40 @@
return NULL;
}
+API const struct lysc_node *
+ly_ctx_get_node(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *data_path, int output)
+{
+ const struct lysc_node *snode = NULL;
+ struct lyxp_expr *exp = NULL;
+ struct ly_path *p = NULL;
+ LY_ERR ret;
+ uint8_t oper;
+
+ LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
+
+ if (!ctx) {
+ ctx = ctx_node->module->ctx;
+ }
+
+ /* parse */
+ exp = lyxp_expr_parse(ctx, data_path, strlen(data_path), 0);
+ LY_CHECK_GOTO(!exp, cleanup);
+
+ /* compile */
+ oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
+ ret = ly_path_compile(ctx, NULL, ctx_node, exp, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
+ lydjson_resolve_prefix, NULL, LYD_JSON, &p);
+ LY_CHECK_GOTO(ret, cleanup);
+
+ /* get last node */
+ snode = p[LY_ARRAY_COUNT(p) - 1].node;
+
+cleanup:
+ ly_path_free(ctx, p);
+ lyxp_expr_free(ctx, exp);
+ return snode;
+}
+
API void
ly_ctx_reset_latests(struct ly_ctx *ctx)
{
diff --git a/src/context.h b/src/context.h
index cdd5ced..39a2883 100644
--- a/src/context.h
+++ b/src/context.h
@@ -400,6 +400,18 @@
struct lys_module *ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns);
/**
+ * @brief Get a schema node based on the given data path (JSON format, see @ref howtoxpath).
+ *
+ * @param[in] ctx libyang context, set for absolute paths.
+ * @param[in] ctx_node Starting context node for a relative data path, set for relative paths.
+ * @param[in] data_path JSON data path of the node to get.
+ * @param[in] output Search operation output instead of input.
+ * @return Found schema node or NULL.
+ */
+const struct lysc_node *ly_ctx_get_node(const struct ly_ctx *ctx, const struct lysc_node *ctx_node,
+ const char *data_path, int output);
+
+/**
* @brief Reset cached latest revision information of the schemas in the context.
*
* When a (sub)module is imported/included without revision, the latest revision is