diff --git include/libxml/xmlsave.h include/libxml/xmlsave.h
index 4201b4d..d1b0437 100644
--- include/libxml/xmlsave.h
+++ include/libxml/xmlsave.h
@@ -33,7 +33,8 @@ typedef enum {
     XML_SAVE_NO_XHTML	= 1<<3, /* disable XHTML1 specific rules */
     XML_SAVE_XHTML	= 1<<4, /* force XHTML1 specific rules */
     XML_SAVE_AS_XML     = 1<<5, /* force XML serialization on HTML doc */
-    XML_SAVE_AS_HTML    = 1<<6  /* force HTML serialization on XML doc */
+    XML_SAVE_AS_HTML    = 1<<6,  /* force HTML serialization on XML doc */
+    XML_SAVE_DIFF       = 1<<7 /* format output for ideal diff result */
 } xmlSaveOption;
 
 
diff --git xmllint.c xmllint.c
index 77c1e62..e8d58a3 100644
--- xmllint.c
+++ xmllint.c
@@ -2392,14 +2392,14 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
 		}
 		else if (encoding != NULL) {
 		    if ( format ) {
-			htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
+			htmlSaveFileFormat(output ? output : "-", doc, encoding, format);
 		    }
 		    else {
 			htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
 		    }
 		}
 		else if (format) {
-		    htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
+		    htmlSaveFileFormat(output ? output : "-", doc, NULL, format);
 		}
 		else {
 		    FILE *out;
@@ -2459,13 +2459,13 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
 
 		if (encoding != NULL) {
 		    if ( format ) {
-		        xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
+		        xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, format);
 		    } else { 
 			xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
 		    }
 		} else {
 		    if (format)
-			xmlDocDumpFormatMemory(doc, &result, &len, 1);
+			xmlDocDumpFormatMemory(doc, &result, &len, format);
 		    else
 			xmlDocDumpMemory(doc, &result, &len);
 		}
@@ -2485,7 +2485,7 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
 	        if (encoding != NULL) {
 		    if ( format ) {
 			ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
-						   encoding, 1);
+						   encoding, format);
 		    }
 		    else {
 			ret = xmlSaveFileEnc(output ? output : "-", doc,
@@ -2497,7 +2497,7 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
 			progresult = XMLLINT_ERR_OUT;
 		    }
 		} else if (format) {
-		    ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
+		    ret = xmlSaveFormatFile(output ? output : "-", doc, format);
 		    if (ret < 0) {
 			fprintf(stderr, "failed save to %s\n",
 				output ? output : "-");
@@ -2525,8 +2525,11 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
 	        xmlSaveCtxtPtr ctxt;
 		int saveOpts = 0;
 
-                if (format)
+                if (format == 1)
 		    saveOpts |= XML_SAVE_FORMAT;
+                else if (format == 2)
+            saveOpts |= XML_SAVE_DIFF;
+                
 
 		if (output == NULL)
 		    ctxt = xmlSaveToFd(1, encoding, saveOpts);
@@ -2875,6 +2878,7 @@ static void usage(const char *name) {
     printf("\t--nocdata : replace cdata section with text nodes\n");
 #ifdef LIBXML_OUTPUT_ENABLED
     printf("\t--format : reformat/reindent the input\n");
+    printf("\t--diff : same as --format, but for better diff results\n");
     printf("\t--encode encoding : output in the given encoding\n");
     printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
 #endif /* LIBXML_OUTPUT_ENABLED */
@@ -3181,10 +3185,16 @@ main(int argc, char **argv) {
 	     }
         }
 	else if ((!strcmp(argv[i], "-format")) ||
-	         (!strcmp(argv[i], "--format"))) {
+	         (!strcmp(argv[i], "--format")) ||
+	         (!strcmp(argv[i], "-diff")) ||
+	         (!strcmp(argv[i], "--diff"))) {
 	     noblanks++;
 #ifdef LIBXML_OUTPUT_ENABLED
 	     format++;
+	     if ((!strcmp(argv[i], "-diff")) ||
+	         (!strcmp(argv[i], "--diff"))) {
+             format++;
+         }
 #endif /* LIBXML_OUTPUT_ENABLED */
 	     xmlKeepBlanksDefault(0);
 	}
diff --git xmlsave.c xmlsave.c
index 53b23e6..dce44f4 100644
--- xmlsave.c
+++ xmlsave.c
@@ -408,6 +408,8 @@ xmlNewSaveCtxt(const char *encoding, int options)
     ret->options = options;
     if (options & XML_SAVE_FORMAT)
         ret->format = 1;
+    if (options & XML_SAVE_DIFF)
+        ret->format = 2;
 
     return(ret);
 }
@@ -612,7 +614,17 @@ xmlAttrDumpOutput(xmlSaveCtxtPtr ctxt, xmlAttrPtr cur) {
     if (cur == NULL) return;
     buf = ctxt->buf;
     if (buf == NULL) return;
-    xmlOutputBufferWrite(buf, 1, " ");
+    if ( ctxt->format == 2 ) 
+    {
+        xmlOutputBufferWrite( buf, 1, "\n" );
+
+	    xmlOutputBufferWrite(buf, ctxt->indent_size *
+	                         (ctxt->level > ctxt->indent_nr ? 
+				  ctxt->indent_nr : ctxt->level),
+				 ctxt->indent);
+    }
+    else
+        xmlOutputBufferWrite(buf, 1, " ");
     if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
         xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
 	xmlOutputBufferWrite(buf, 1, ":");
@@ -879,6 +891,7 @@ xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
 	    tmp = tmp->next;
 	}
     }
+
     xmlOutputBufferWrite(buf, 1, "<");
     if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
         xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
@@ -889,8 +902,12 @@ xmlNodeDumpOutputInternal(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
     if (cur->nsDef)
         xmlNsListDumpOutput(buf, cur->nsDef);
     if (cur->properties != NULL)
+    {
+        ctxt->level++;
         xmlAttrListDumpOutput(ctxt, cur->properties);
-
+        ctxt->level--;
+    }
+    
     if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
 	(cur->children == NULL) && ((ctxt->options & XML_SAVE_NO_EMPTY) == 0)) {
         xmlOutputBufferWrite(buf, 2, "/>");
