data tree FEATURE function for evaluating xpath on data
Just added a public API for internal function.
diff --git a/src/tree_data.c b/src/tree_data.c
index b7d21b7..fd14dc8 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -2580,3 +2580,48 @@
lyd_free_tree(target);
return rc;
}
+
+API LY_ERR
+lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set)
+{
+ LY_ERR ret = LY_SUCCESS;
+ struct lyxp_set xp_set;
+ struct lyxp_expr *exp;
+ uint32_t i;
+
+ LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
+
+ memset(&xp_set, 0, sizeof xp_set);
+
+ /* compile expression */
+ exp = lyxp_expr_parse((struct ly_ctx *)LYD_NODE_CTX(ctx_node), xpath);
+ LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
+
+ /* evaluate expression */
+ ret = lyxp_eval(exp, LYD_JSON, ctx_node->schema->module, ctx_node, LYXP_NODE_ELEM, ctx_node, &xp_set, 0);
+ LY_CHECK_GOTO(ret, cleanup);
+
+ /* allocate return set */
+ *set = ly_set_new();
+ LY_CHECK_ERR_GOTO(!*set, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
+
+ /* transform into ly_set */
+ if (xp_set.type == LYXP_SET_NODE_SET) {
+ /* allocate memory for all the elements once (even though not all items must be elements but most likely will be) */
+ (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
+ LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(LYD_NODE_CTX(ctx_node)); ret = LY_EMEM, cleanup);
+ (*set)->size = xp_set.used;
+
+ for (i = 0; i < xp_set.used; ++i) {
+ if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
+ ly_set_add(*set, xp_set.val.nodes[i].node, LY_SET_OPT_USEASLIST);
+ }
+ }
+ }
+
+cleanup:
+ //lyxp_set_free_content(&xp_set);
+ lyxp_set_cast(&xp_set, LYXP_SET_EMPTY);
+ lyxp_expr_free((struct ly_ctx *)LYD_NODE_CTX(ctx_node), exp);
+ return ret;
+}
diff --git a/src/tree_data.h b/src/tree_data.h
index ccfe502..b3a2ab4 100644
--- a/src/tree_data.h
+++ b/src/tree_data.h
@@ -1056,6 +1056,18 @@
LY_ERR lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
size_t val_len, struct lyd_node **match);
+/**
+ * @brief Search in the given data for instances of nodes matching the provided XPath.
+ *
+ * @param[in] ctx_node XPath context node.
+ * @param[in] xpath Data XPath expression filtering the matching nodes. ::LYD_JSON format is expected.
+ * @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean,
+ * the returned set is empty.
+ * @return LY_SUCCESS on success, @p set is returned.
+ * @return LY_ERR value if an error occurred.
+ */
+LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set);
+
#ifdef __cplusplus
}
#endif