xpath BUGFIX tree dfs does not set elem to NULL when finished
diff --git a/src/xpath.c b/src/xpath.c
index 874065c..9e672b5 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -1287,6 +1287,7 @@
 {
     const struct lyd_node *elem = NULL, *top_sibling;
     uint32_t pos = 1;
+    ly_bool found = 0;
 
     assert(prev && prev_pos && !root->prev->next);
 
@@ -1297,11 +1298,11 @@
     if (*prev) {
         /* start from the previous element instead from the root */
         pos = *prev_pos;
-        for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {}
+        for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
         goto dfs_search;
     }
 
-    for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
+    LY_LIST_FOR(root, top_sibling) {
         LYD_TREE_DFS_BEGIN(top_sibling, elem) {
 dfs_search:
             if (*prev && !elem) {
@@ -1315,6 +1316,7 @@
                 LYD_TREE_DFS_continue = 1;
             } else {
                 if (elem == node) {
+                    found = 1;
                     break;
                 }
                 ++pos;
@@ -1324,12 +1326,12 @@
         }
 
         /* node found */
-        if (elem) {
+        if (found) {
             break;
         }
     }
 
-    if (!elem) {
+    if (!found) {
         if (!(*prev)) {
             /* we went from root and failed to find it, cannot be */
             LOGINT(LYD_CTX(node));