data tree NEW lyd_list_pos
diff --git a/src/libyang.h.in b/src/libyang.h.in
index 0ee6563..2d480c9 100644
--- a/src/libyang.h.in
+++ b/src/libyang.h.in
@@ -512,6 +512,7 @@
* - lyd_new_output()
* - lyd_new_output_anydata()
* - lyd_new_output_leaf()
+ * - lyd_list_pos()
* - lyd_schema_sort()
* - lyd_unlink()
* - lyd_free()
@@ -702,10 +703,11 @@
* A very small subset of this full XPath is recognized by lyd_new_path(). Basically, only a relative or absolute
* path can be specified to identify a new data node. However, lists must be identified by either all their keys and created
* with all of them or using their relative position on their level starting from 1, so for those cases predicates are
- * allowed. Key predicates must be ordered the way the keys are ordered and all the keys must be specified. Every
- * predicate includes a single key with its value. If an instance with such particular set of keys or with such relative
- * position does not exist or no predicate is specified, list instance is created. Optionally, leaves and leaf-lists can
- * have predicates specifying their value in the path itself. All these paths are valid XPath expressions. Example:
+ * allowed. List positions can be learned using lyd_list_pos(). Key predicates must be ordered the way the keys are
+ * ordered and all the keys must be specified. Every predicate includes a single key with its value. If an instance
+ * with such particular set of keys or with such relative position does not exist or no predicate is specified, list
+ * instance is created. Optionally, leaves and leaf-lists can have predicates specifying their value in the path itself.
+ * All these paths are valid XPath expressions. Example:
*
* /ietf-yang-library:modules-state/module[name='ietf-yang-library'][revision='']/conformance[.='implement']
* /ietf-yang-library:modules-state/module[1]/conformance[.='implement']
diff --git a/src/tree_data.c b/src/tree_data.c
index 0e5d9b7..85d9c28 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -1485,6 +1485,28 @@
return NULL;
}
+API unsigned int
+lyd_list_pos(const struct lyd_node *node)
+{
+ unsigned int pos;
+ struct lys_node *schema;
+
+ if (!node || ((node->schema->nodetype != LYS_LIST) && (node->schema->nodetype != LYS_LEAFLIST))) {
+ return 0;
+ }
+
+ schema = node->schema;
+ pos = 0;
+ do {
+ if (node->schema == schema) {
+ ++pos;
+ }
+ node = node->prev;
+ } while (node->next);
+
+ return pos;
+}
+
struct lyd_node *
lyd_new_dummy(struct lyd_node *root, struct lyd_node *parent, const struct lys_node *schema, const char *value, int dflt)
{
diff --git a/src/tree_data.h b/src/tree_data.h
index d1c653d..f6c9c38 100644
--- a/src/tree_data.h
+++ b/src/tree_data.h
@@ -771,6 +771,15 @@
LYD_ANYDATA_VALUETYPE value_type, int options);
/**
+ * @brief Learn the relative instance position of a list or leaf-list within other instances of the
+ * same schema node.
+ *
+ * @param[in] node List or leaf-list to get the position of.
+ * @return 0 on error or positive integer of the instance position.
+ */
+unsigned int lyd_list_pos(const struct lyd_node *node);
+
+/**
* @brief Create a copy of the specified data tree \p node. Namespaces are copied as needed,
* schema references are kept the same.
*