identityref UPDATE support optional module prefix

Fixes #420
diff --git a/src/printer_xml.c b/src/printer_xml.c
index a7f4c73..47b0acc 100644
--- a/src/printer_xml.c
+++ b/src/printer_xml.c
@@ -178,6 +178,8 @@
     struct ly_set ns_list = {0};
     LY_ARRAY_COUNT_TYPE u;
     ly_bool dynamic, filter_attrs = 0;
+    const char *value;
+    uint32_t i;
 
     /* with-defaults */
     if (node->schema->nodetype & LYD_NODE_TERM) {
@@ -205,11 +207,14 @@
     }
 
     for (meta = node->meta; meta; meta = meta->next) {
-        const char *value = meta->value.realtype->plugin->print(LYD_CTX(node), &meta->value, LY_VALUE_XML, &ns_list,
-                &dynamic, NULL);
+        /* store the module of the default namespace, NULL because there is none */
+        ly_set_add(&ns_list, NULL, 0, NULL);
+
+        /* print the value */
+        value = meta->value.realtype->plugin->print(LYD_CTX(node), &meta->value, LY_VALUE_XML, &ns_list, &dynamic, NULL);
 
         /* print namespaces connected with the value's prefixes */
-        for (uint32_t i = 0; i < ns_list.count; ++i) {
+        for (i = 1; i < ns_list.count; ++i) {
             mod = ns_list.objs[i];
             xml_print_ns(pctx, mod->ns, mod->prefix, 1);
         }
@@ -314,22 +319,32 @@
 static LY_ERR
 xml_print_term(struct xmlpr_ctx *pctx, const struct lyd_node_term *node)
 {
+    LY_ERR rc = LY_SUCCESS;
     struct ly_set ns_list = {0};
-    ly_bool dynamic;
-    const char *value;
+    ly_bool dynamic = 0;
+    const char *value = NULL;
+    const struct lys_module *mod;
+    uint32_t i;
 
-    xml_print_node_open(pctx, &node->node);
+    /* store the module of the default namespace */
+    if ((rc = ly_set_add(&ns_list, node->schema->module, 0, NULL))) {
+        LOGMEM(pctx->ctx);
+        goto cleanup;
+    }
+
+    /* print the value */
     value = ((struct lysc_node_leaf *)node->schema)->type->plugin->print(LYD_CTX(node), &node->value, LY_VALUE_XML,
             &ns_list, &dynamic, NULL);
-    LY_CHECK_RET(!value, LY_EINVAL);
+    LY_CHECK_ERR_GOTO(!value, rc = LY_EINVAL, cleanup);
+
+    /* print node opening */
+    xml_print_node_open(pctx, &node->node);
 
     /* print namespaces connected with the values's prefixes */
-    for (uint32_t u = 0; u < ns_list.count; ++u) {
-        const struct lys_module *mod = (const struct lys_module *)ns_list.objs[u];
-
+    for (i = 1; i < ns_list.count; ++i) {
+        mod = ns_list.objs[i];
         ly_print_(pctx->out, " xmlns:%s=\"%s\"", mod->prefix, mod->ns);
     }
-    ly_set_erase(&ns_list, NULL);
 
     if (!value[0]) {
         ly_print_(pctx->out, "/>%s", DO_FORMAT ? "\n" : "");
@@ -338,11 +353,13 @@
         lyxml_dump_text(pctx->out, value, 0);
         ly_print_(pctx->out, "</%s>%s", node->schema->name, DO_FORMAT ? "\n" : "");
     }
+
+cleanup:
+    ly_set_erase(&ns_list, NULL);
     if (dynamic) {
         free((void *)value);
     }
-
-    return LY_SUCCESS;
+    return rc;
 }
 
 /**