data tree BUGFIX inserting before first node

In this case the first pointer must be moved.
diff --git a/src/tree_data.c b/src/tree_data.c
index da5b51b..5d6c63c 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -1833,6 +1833,10 @@
     anchor = lyd_insert_get_next_anchor(first_sibling, node);
     if (anchor) {
         lyd_insert_before_node(anchor, node);
+        if (!parent && (*first_sibling_p == anchor)) {
+            /* move first sibling */
+            *first_sibling_p = node;
+        }
     } else if (first_sibling) {
         lyd_insert_after_node(first_sibling->prev, node);
     } else if (parent) {
diff --git a/tests/utests/data/test_parser_json.c b/tests/utests/data/test_parser_json.c
index c22eb66..9116e04 100644
--- a/tests/utests/data/test_parser_json.c
+++ b/tests/utests/data/test_parser_json.c
@@ -155,6 +155,7 @@
     data = "{\"a:foo2\":\"default-val\"}";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("foo2", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -170,6 +171,7 @@
     data = "{\"a:foo2\":\"default-val\",\"@a:foo2\":{\"ietf-netconf-with-defaults:default\":true}}";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("foo2", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -239,6 +241,7 @@
 
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_LEAFLIST, tree->schema->nodetype);
     assert_string_equal("ll1", tree->schema->name);
     ll = (struct lyd_node_term*)tree;
@@ -259,6 +262,7 @@
     data = "{\"a:ll1\":[10,11],\"@a:ll1\":[null,{\"a:hint\":2}]}";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_LEAFLIST, tree->schema->nodetype);
     assert_string_equal("ll1", tree->schema->name);
     ll = (struct lyd_node_term*)tree;
@@ -283,6 +287,7 @@
     data = "{\"@a:ll1\" : [{\"a:hint\" : 1, \"x:xxx\" :  { \"value\" : \"/x:no/x:yes\" }, \"a:hint\" : 10},null,{\"a:hint\" : 3}], \"a:ll1\" : [1,2,3]}";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_LEAFLIST, tree->schema->nodetype);
     assert_string_equal("ll1", tree->schema->name);
     ll = (struct lyd_node_term*)tree;
@@ -357,6 +362,7 @@
     data = "{\"a:any\":{\"x:element1\":{\"element2\":\"/a:some/a:path\",\"list\":[{},{\"key\":\"a\"}]}}}";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_ANYDATA, tree->schema->nodetype);
     assert_string_equal("any", tree->schema->name);
 
@@ -463,6 +469,7 @@
     data = "{\"a:cp\":{\"@\":{\"a:hint\":1}}}";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
     assert_string_equal("cp", tree->schema->name);
     assert_non_null(tree->meta);
@@ -507,6 +514,7 @@
     data = "{\"a:cp\":{}}";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
     assert_string_equal("cp", tree->schema->name);
     cont = (struct lyd_node_inner*)tree;
diff --git a/tests/utests/data/test_parser_xml.c b/tests/utests/data/test_parser_xml.c
index baf4608..d383d89 100644
--- a/tests/utests/data/test_parser_xml.c
+++ b/tests/utests/data/test_parser_xml.c
@@ -146,6 +146,7 @@
     data = "<foo2 xmlns=\"urn:tests:a\">default-val</foo2>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("foo2", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -158,6 +159,7 @@
     data = "<foo2 xmlns=\"urn:tests:a\" xmlns:wd=\"urn:ietf:params:xml:ns:yang:ietf-netconf-with-defaults\" wd:default=\"true\" xmlns:x=\"urn:x\" x:xxx=\"false\">default-val</foo2>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("foo2", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -190,6 +192,7 @@
     "</any>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_ANYDATA, tree->schema->nodetype);
     assert_string_equal("any", tree->schema->name);
 
@@ -309,6 +312,7 @@
     data = "<cp xmlns=\"urn:tests:a\"/>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
     assert_non_null(tree);
+    tree = tree->next;
     assert_int_equal(LYS_CONTAINER, tree->schema->nodetype);
     assert_string_equal("cp", tree->schema->name);
     cont = (struct lyd_node_inner*)tree;
diff --git a/tests/utests/data/test_printer_xml.c b/tests/utests/data/test_printer_xml.c
index 75cddd2..8d7138f 100644
--- a/tests/utests/data/test_printer_xml.c
+++ b/tests/utests/data/test_printer_xml.c
@@ -186,7 +186,7 @@
     data = "<int8 xmlns=\"urn:tests:types\">\n 15 \t\n  </int8>";
     result = "<int8 xmlns=\"urn:tests:types\">15</int8>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
-    assert_int_equal(LY_SUCCESS, lyd_print_tree(out, tree, LYD_XML, LYD_PRINT_SHRINK));
+    assert_int_equal(LY_SUCCESS, lyd_print_tree(out, tree->next, LYD_XML, LYD_PRINT_SHRINK));
     assert_int_equal(strlen(printed), ly_out_printed(out));
     assert_string_equal(printed, result);
     lyd_free_all(tree);
@@ -209,7 +209,7 @@
 
     data = "<any xmlns=\"urn:tests:types\"><somexml xmlns:x=\"url:x\" xmlns=\"example.com\"><x:x/></somexml></any>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
-    assert_int_equal(LY_SUCCESS, lyd_print_tree(out, tree, LYD_XML, LYD_PRINT_SHRINK));
+    assert_int_equal(LY_SUCCESS, lyd_print_tree(out, tree->next, LYD_XML, LYD_PRINT_SHRINK));
     assert_int_equal(strlen(printed), ly_out_printed(out));
     /* canonized */
     data = "<any xmlns=\"urn:tests:types\"><somexml xmlns=\"example.com\"><x xmlns=\"url:x\"/></somexml></any>";
@@ -219,7 +219,7 @@
 
     data = "<any xmlns=\"urn:tests:types\"/>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
-    assert_int_equal(LY_SUCCESS, lyd_print_tree(out, tree, LYD_XML, LYD_PRINT_SHRINK));
+    assert_int_equal(LY_SUCCESS, lyd_print_tree(out, tree->next, LYD_XML, LYD_PRINT_SHRINK));
     assert_int_equal(strlen(printed), ly_out_printed(out));
     assert_string_equal(printed, data);
     ly_out_reset(out);
@@ -236,6 +236,7 @@
         "</any>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(s->ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree));
     /* cont should be normally parsed */
+    tree = tree->next;
     assert_string_equal(tree->schema->name, "any");
     assert_int_equal(((struct lyd_node_any *)tree)->value_type, LYD_ANYDATA_DATATREE);
     assert_string_equal(((struct lyd_node_any *)tree)->value.tree->schema->name, "cont");
diff --git a/tests/utests/data/test_tree_data.c b/tests/utests/data/test_tree_data.c
index 2143904..8456473 100644
--- a/tests/utests/data/test_tree_data.c
+++ b/tests/utests/data/test_tree_data.c
@@ -129,8 +129,8 @@
     data2 = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2));
-    assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
-    assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, 0));
+    assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
+    assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next->next, tree2->next, 0));
     lyd_free_all(tree1);
     lyd_free_all(tree2);
 
@@ -167,7 +167,7 @@
     data2 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data2, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree2));
-    assert_int_equal(LY_ENOT, lyd_compare_single(tree1, tree2, 0));
+    assert_int_equal(LY_ENOT, lyd_compare_single(tree1->next, tree2->next, 0));
     lyd_free_all(tree1);
     data1 = "<any xmlns=\"urn:tests:a\"><x>x</x><x>y</x></any>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data1, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
@@ -207,15 +207,15 @@
     result = "<l2 xmlns=\"urn:tests:a\"><c><x>a</x></c></l2>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
     assert_int_equal(LY_SUCCESS, lyd_dup_siblings(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
-    assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
+    assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2->next, LYD_COMPARE_FULL_RECURSION));
     lyd_free_all(tree2);
-    assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, LYD_DUP_RECURSIVE, &tree2));
+    assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, LYD_DUP_RECURSIVE, &tree2));
     lyd_free_all(tree1);
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
-    assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
+    assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
     lyd_free_all(tree2);
 
-    assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
+    assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
     lyd_free_all(tree1);
     result = "<l2 xmlns=\"urn:tests:a\"/>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, result, LYD_XML, LYD_PARSE_ONLY, 0, &tree1));
@@ -232,11 +232,12 @@
 
     data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
-    assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child, NULL,
+    assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1->next)->child)->child, NULL,
                                          LYD_DUP_WITH_PARENTS, &tree2));
     assert_string_equal("x", tree2->schema->name);
     assert_non_null(tree2->parent);
-    assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, (struct lyd_node*)tree2->parent->parent, LYD_COMPARE_FULL_RECURSION));
+    assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, (struct lyd_node*)tree2->parent->parent,
+            LYD_COMPARE_FULL_RECURSION));
     lyd_free_all(tree1);
     lyd_free_all(tree2);
 
@@ -252,10 +253,10 @@
 
     data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
     assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(ctx, data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree1));
-    assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1, NULL, 0, &tree2));
-    assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1)->child)->child,
+    assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
+    assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner*)((struct lyd_node_inner*)tree1->next)->child)->child,
                                  (struct lyd_node_inner*)tree2, LYD_DUP_WITH_PARENTS, NULL));
-    assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1, tree2, LYD_COMPARE_FULL_RECURSION));
+    assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
     lyd_free_all(tree1);
     lyd_free_all(tree2);
 
diff --git a/tests/utests/data/test_types.c b/tests/utests/data/test_types.c
index 818a9c0..4cee219 100644
--- a/tests/utests/data/test_types.c
+++ b/tests/utests/data/test_types.c
@@ -179,6 +179,7 @@
 
     /* valid data */
     TEST_DATA("<int8 xmlns=\"urn:tests:types\">\n 15 \t\n  </int8>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("int8", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -219,6 +220,7 @@
 
     /* valid data */
     TEST_DATA("<uint8 xmlns=\"urn:tests:types\">\n 150 \t\n  </uint8>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("uint8", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -259,6 +261,7 @@
 
     /* valid data */
     TEST_DATA("<dec64 xmlns=\"urn:tests:types\">\n +8 \t\n  </dec64>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("dec64", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -273,6 +276,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<dec64 xmlns=\"urn:tests:types\">8.00</dec64>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("dec64", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -281,6 +285,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<dec64-norestr xmlns=\"urn:tests:types\">-9.223372036854775808</dec64-norestr>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("dec64-norestr", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -289,6 +294,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<dec64-norestr xmlns=\"urn:tests:types\">9.223372036854775807</dec64-norestr>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("dec64-norestr", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -323,6 +329,7 @@
 
     /* valid data */
     TEST_DATA("<str xmlns=\"urn:tests:types\">teststring</str>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("str", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -331,6 +338,7 @@
 
     /* multibyte characters (€ encodes as 3-byte UTF8 character, length restriction is 2-5) */
     TEST_DATA("<str-utf8 xmlns=\"urn:tests:types\">€€</str-utf8>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("str-utf8", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -364,6 +372,7 @@
 
     /* valid data */
     TEST_DATA("<bits xmlns=\"urn:tests:types\">\n two    \t\nzero\n  </bits>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("bits", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -382,6 +391,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<bits xmlns=\"urn:tests:types\">zero  two</bits>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("bits", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -420,6 +430,7 @@
 
     /* valid data */
     TEST_DATA("<enums xmlns=\"urn:tests:types\">white</enums>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("enums", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -460,6 +471,7 @@
 
     /* valid data (hello) */
     TEST_DATA("<binary xmlns=\"urn:tests:types\">\n   aGVs\nbG8=  \t\n  </binary><binary-norestr xmlns=\"urn:tests:types\">TQ==</binary-norestr>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("binary", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -478,18 +490,21 @@
 
     /* no data */
     TEST_DATA("<binary-norestr xmlns=\"urn:tests:types\">\n    \t\n  </binary-norestr>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("binary-norestr", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
     assert_string_equal("</binary-norestr>", leaf->value.canonical);
     lyd_free_all(tree);
     TEST_DATA("<binary-norestr xmlns=\"urn:tests:types\"></binary-norestr>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("binary-norestr", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
     assert_string_equal("", leaf->value.canonical);
     lyd_free_all(tree);
     TEST_DATA("<binary-norestr xmlns=\"urn:tests:types\"/>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("binary-norestr", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -530,6 +545,7 @@
 
     /* valid data */
     TEST_DATA("<bool xmlns=\"urn:tests:types\">true</bool>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("bool", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -538,6 +554,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<bool xmlns=\"urn:tests:types\">false</bool>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("bool", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -546,6 +563,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<tbool xmlns=\"urn:tests:types\">false</tbool>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("tbool", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -574,6 +592,7 @@
     /* valid data */
 
     TEST_DATA("<empty xmlns=\"urn:tests:types\"></empty>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("empty", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -581,6 +600,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<empty xmlns=\"urn:tests:types\"/>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("empty", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -588,6 +608,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<tempty xmlns=\"urn:tests:types\"/>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("tempty", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -629,6 +650,7 @@
 
     /* valid data */
     TEST_DATA("<ident xmlns=\"urn:tests:types\">gigabit-ethernet</ident>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("ident", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -644,6 +666,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<ident xmlns=\"urn:tests:types\" xmlns:x=\"urn:tests:defs\">x:fast-ethernet</ident>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("ident", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -714,7 +737,7 @@
 
     TEST_DATA("<list xmlns=\"urn:tests:types\"><id>a</id></list><list xmlns=\"urn:tests:types\"><id>b</id></list>"
               "<xdf:inst xmlns:xdf=\"urn:tests:types\">/xdf:list[xdf:id='b']/xdf:id</xdf:inst>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("inst", tree->schema->name);
     leaf = (const struct lyd_node_term*)tree;
@@ -723,7 +746,7 @@
 
     TEST_DATA("<leaflisttarget xmlns=\"urn:tests:types\">1</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">2</leaflisttarget>"
               "<xdf:inst xmlns:xdf=\"urn:tests:types\">/xdf:leaflisttarget[.='1']</xdf:inst>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("inst", tree->schema->name);
     leaf = (const struct lyd_node_term*)tree;
@@ -734,7 +757,7 @@
            "<list_inst xmlns=\"urn:tests:types\"><id xmlns:b=\"urn:tests:types\">/b:leaflisttarget[.='b']</id><value>y</value></list_inst>"
            "<leaflisttarget xmlns=\"urn:tests:types\">a</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">b</leaflisttarget>"
            "<a:inst xmlns:a=\"urn:tests:types\">/a:list_inst[a:id=\"/a:leaflisttarget[.='b']\"]/a:value</a:inst>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("inst", tree->schema->name);
     leaf = (const struct lyd_node_term*)tree;
@@ -760,7 +783,7 @@
 
     TEST_DATA("<list xmlns=\"urn:tests:types\"><id>a</id></list><list xmlns=\"urn:tests:types\"><id>b</id><value>x</value></list>"
               "<xdf:inst xmlns:xdf=\"urn:tests:types\">/xdf:list[xdf:id='b']/xdf:value</xdf:inst>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("inst", tree->schema->name);
     leaf = (const struct lyd_node_term*)tree;
@@ -771,7 +794,7 @@
               "<list_inst xmlns=\"urn:tests:types\"><id xmlns:b=\"urn:tests:types\">/b:leaflisttarget[.='b']</id><value>y</value></list_inst>"
               "<leaflisttarget xmlns=\"urn:tests:types\">a</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">b</leaflisttarget>"
               "<a:inst xmlns:a=\"urn:tests:types\">/a:list_inst[a:id=\"/a:leaflisttarget[.='a']\"]/a:value</a:inst>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("inst", tree->schema->name);
     leaf = (const struct lyd_node_term*)tree;
@@ -781,7 +804,7 @@
     TEST_DATA("<list_ident xmlns=\"urn:tests:types\"><id xmlns:dfs=\"urn:tests:defs\">dfs:ethernet</id><value>x</value></list_ident>"
               "<list_ident xmlns=\"urn:tests:types\"><id xmlns:dfs=\"urn:tests:defs\">dfs:fast-ethernet</id><value>y</value></list_ident>"
               "<a:inst xmlns:a=\"urn:tests:types\" xmlns:d=\"urn:tests:defs\">/a:list_ident[a:id='d:fast-ethernet']/a:value</a:inst>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("inst", tree->schema->name);
     leaf = (const struct lyd_node_term*)tree;
@@ -792,7 +815,7 @@
     TEST_DATA("<list2 xmlns=\"urn:tests:types\"><id>types:xxx</id><value>x</value></list2>"
               "<list2 xmlns=\"urn:tests:types\"><id>a:xxx</id><value>y</value></list2>"
               "<a:inst xmlns:a=\"urn:tests:types\">/a:list2[a:id='a:xxx'][a:value='y']/a:value</a:inst>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("inst", tree->schema->name);
     leaf = (const struct lyd_node_term*)tree;
@@ -803,7 +826,7 @@
     TEST_DATA("<list xmlns=\"urn:tests:types\"><id>types:xxx</id><value>x</value></list>"
               "<list xmlns=\"urn:tests:types\"><id>a:xxx</id><value>y</value></list>"
               "<a:inst xmlns:a=\"urn:tests:types\">/a:list[a:id='a:xxx']/a:value</a:inst>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("inst", tree->schema->name);
     leaf = (const struct lyd_node_term*)tree;
@@ -815,7 +838,7 @@
               "<list2 xmlns=\"urn:tests:types\"><id>c</id><value>b</value></list2>"
               "<list2 xmlns=\"urn:tests:types\"><id>a</id><value>b</value></list2>"
               "<a:inst xmlns:a=\"urn:tests:types\">/a:list2[a:id='a'][a:value='b']/a:id</a:inst>", LY_SUCCESS, "");
-    leaf = (const struct lyd_node_term*)tree->prev->prev;
+    leaf = (const struct lyd_node_term*)tree->prev;
     assert_int_equal(LYS_LEAF, leaf->schema->nodetype);
     assert_string_equal("inst", leaf->schema->name);
     assert_non_null(leaf->value.canonical);
@@ -918,17 +941,17 @@
 
     /* key-predicate */
     data = "/types:list2[id='a'][value='b']/id";
-    assert_int_equal(LY_ENOTFOUND, lyd_value_validate(s->ctx, (const struct lyd_node_term*)tree->prev->prev, data, strlen(data),
+    assert_int_equal(LY_ENOTFOUND, lyd_value_validate(s->ctx, (const struct lyd_node_term*)tree->prev, data, strlen(data),
                                                    tree, NULL));
     logbuf_assert("Invalid instance-identifier \"/types:list2[id='a'][value='b']/id\" value - required instance not found. /types:inst");
     /* leaf-list-predicate */
     data = "/types:leaflisttarget[.='c']";
-    assert_int_equal(LY_ENOTFOUND, lyd_value_validate(s->ctx, (const struct lyd_node_term*)tree->prev->prev, data, strlen(data),
+    assert_int_equal(LY_ENOTFOUND, lyd_value_validate(s->ctx, (const struct lyd_node_term*)tree->prev, data, strlen(data),
                                                    tree, NULL));
     logbuf_assert("Invalid instance-identifier \"/types:leaflisttarget[.='c']\" value - required instance not found. /types:inst");
     /* position predicate */
     data = "/types:list_keyless[4]";
-    assert_int_equal(LY_ENOTFOUND, lyd_value_validate(s->ctx, (const struct lyd_node_term*)tree->prev->prev, data, strlen(data),
+    assert_int_equal(LY_ENOTFOUND, lyd_value_validate(s->ctx, (const struct lyd_node_term*)tree->prev, data, strlen(data),
                                                    tree, NULL));
     logbuf_assert("Invalid instance-identifier \"/types:list_keyless[4]\" value - required instance not found. /types:inst");
 
@@ -968,7 +991,7 @@
 
     /* valid data */
     TEST_DATA("<leaflisttarget xmlns=\"urn:tests:types\">x</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">y</leaflisttarget><lref xmlns=\"urn:tests:types\">y</lref>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("lref", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -979,7 +1002,7 @@
     TEST_DATA("<list xmlns=\"urn:tests:types\"><id>x</id><targets>a</targets><targets>b</targets></list>"
               "<list xmlns=\"urn:tests:types\"><id>y</id><targets>x</targets><targets>y</targets></list>"
               "<str-norestr xmlns=\"urn:tests:types\">y</str-norestr><lref2 xmlns=\"urn:tests:types\">y</lref2>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("lref2", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -1072,7 +1095,7 @@
 
     /* valid data */
     TEST_DATA("<int8 xmlns=\"urn:tests:types\">12</int8><un1 xmlns=\"urn:tests:types\">12</un1>", LY_SUCCESS, "");
-    tree = tree->next;
+    tree = tree->next->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("un1", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -1097,7 +1120,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<int8 xmlns=\"urn:tests:types\">12</int8><un1 xmlns=\"urn:tests:types\">2</un1>", LY_SUCCESS, "");
-    tree = tree->next;
+    tree = tree->next->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("un1", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -1110,6 +1133,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<un1 xmlns=\"urn:tests:types\" xmlns:x=\"urn:tests:defs\">x:fast-ethernet</un1>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("un1", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -1134,6 +1158,7 @@
     lyd_free_all(tree);
 
     TEST_DATA("<un1 xmlns=\"urn:tests:types\" xmlns:d=\"urn:tests:defs\">d:superfast-ethernet</un1>", LY_SUCCESS, "");
+    tree = tree->next;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("un1", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -1147,7 +1172,7 @@
 
     TEST_DATA("<leaflisttarget xmlns=\"urn:tests:types\">x</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">y</leaflisttarget>"
             "<un1 xmlns=\"urn:tests:types\" xmlns:a=\"urn:tests:types\">/a:leaflisttarget[.='y']</un1>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("un1", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;
@@ -1161,7 +1186,7 @@
 
     TEST_DATA("<leaflisttarget xmlns=\"urn:tests:types\">x</leaflisttarget><leaflisttarget xmlns=\"urn:tests:types\">y</leaflisttarget>"
               "<un1 xmlns=\"urn:tests:types\" xmlns:a=\"urn:tests:types\">/a:leaflisttarget[3]</un1>", LY_SUCCESS, "");
-    tree = tree->prev->prev;
+    tree = tree->prev;
     assert_int_equal(LYS_LEAF, tree->schema->nodetype);
     assert_string_equal("un1", tree->schema->name);
     leaf = (struct lyd_node_term*)tree;