xml parser BUGFIX freeing node not inserted inmto parent ht

Fixes #591
diff --git a/src/parser_xml.c b/src/parser_xml.c
index 52fdbf6..a872b20 100644
--- a/src/parser_xml.c
+++ b/src/parser_xml.c
@@ -336,12 +336,12 @@
 
         r = lyp_fill_attr(ctx, *result, str, NULL, attr->name, attr->value, xml, options, &dattr);
         if (r == -1) {
-            goto error;
+            goto unlink_node_error;
         } else if (r == 1) {
 attr_error:
             if (options & LYD_OPT_STRICT) {
                 LOGVAL(ctx, LYE_INATTR, LY_VLOG_LYD, *result, attr->name);
-                goto error;
+                goto unlink_node_error;
             }
 
             LOGWRN(ctx, "Unknown \"%s:%s\" metadata with value \"%s\", ignoring.",
@@ -355,7 +355,7 @@
             if (!dattr->value.string) {
                 /* problem with resolving value as xpath */
                 dattr->value.string = dattr->value_str;
-                goto error;
+                goto unlink_node_error;
             }
             lydict_remove(ctx, dattr->value_str);
             dattr->value_str = dattr->value.string;
@@ -374,7 +374,7 @@
     /* check insert attribute and its values */
     if (options & LYD_OPT_EDIT) {
         if (lyp_check_edit_attr(ctx, (*result)->attr, *result, &editbits)) {
-            goto error;
+            goto unlink_node_error;
         }
 
     /* check correct filter extension attributes */
@@ -384,14 +384,14 @@
             if (!strcmp(dattr_iter->name, "type")) {
                 if ((found == 1) || (found == 2) || (found == 4)) {
                     LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, (*result), "type", xml->name);
-                    goto error;
+                    goto unlink_node_error;
                 }
                 switch (dattr_iter->value.enm->value) {
                 case 0:
                     /* subtree */
                     if (found == 3) {
                         LOGVAL(ctx, LYE_INATTR, LY_VLOG_LYD, (*result), dattr_iter->name);
-                        goto error;
+                        goto unlink_node_error;
                     }
 
                     assert(!found);
@@ -408,7 +408,7 @@
                     break;
                 default:
                     LOGINT(ctx);
-                    goto error;
+                    goto unlink_node_error;
                 }
             } else if (!strcmp(dattr_iter->name, "select")) {
                 switch (found) {
@@ -417,17 +417,17 @@
                     break;
                 case 1:
                     LOGVAL(ctx, LYE_INATTR, LY_VLOG_LYD, (*result), dattr_iter->name);
-                    goto error;
+                    goto unlink_node_error;
                 case 2:
                     found = 4;
                     break;
                 case 3:
                 case 4:
                     LOGVAL(ctx, LYE_TOOMANY, LY_VLOG_LYD, (*result), "select", xml->name);
-                    goto error;
+                    goto unlink_node_error;
                 default:
                     LOGINT(ctx);
-                    goto error;
+                    goto unlink_node_error;
                 }
             }
         }
@@ -440,13 +440,13 @@
             break;
         case 2:
             LOGVAL(ctx, LYE_MISSATTR, LY_VLOG_LYD, (*result), "select", xml->name);
-            goto error;
+            goto unlink_node_error;
         case 3:
             LOGVAL(ctx, LYE_MISSATTR, LY_VLOG_LYD, (*result), "type", xml->name);
-            goto error;
+            goto unlink_node_error;
         default:
             LOGINT(ctx);
-            goto error;
+            goto unlink_node_error;
         }
     }
 
@@ -454,7 +454,7 @@
     if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
         /* type detection and assigning the value */
         if (xml_get_value(*result, xml, editbits, options & LYD_OPT_TRUSTED)) {
-            goto error;
+            goto unlink_node_error;
         }
     } else if (schema->nodetype & LYS_ANYDATA) {
         /* store children values */
@@ -478,14 +478,14 @@
             LOGVAL(ctx, LYE_INELEM, LY_VLOG_LYD, (*result), schema->name);
             LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected %s node \"%s\".",
                    (schema->nodetype == LYS_RPC ? "rpc" : "action"), schema->name);
-            goto error;
+            goto unlink_node_error;
         }
         *act_notif = *result;
     } else if (schema->nodetype == LYS_NOTIF) {
         if (!(options & LYD_OPT_NOTIF) || *act_notif) {
             LOGVAL(ctx, LYE_INELEM, LY_VLOG_LYD, (*result), schema->name);
             LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected notification node \"%s\".", schema->name);
-            goto error;
+            goto unlink_node_error;
         }
         *act_notif = *result;
     }
@@ -541,6 +541,8 @@
 
     return ret;
 
+unlink_node_error:
+    lyd_unlink_internal(*result, 2);
 error:
     /* cleanup */
     for (i = unres->count - 1; i >= 0; i--) {