--- opensync_xmlformat.c.orig	2009-10-23 20:16:52.746782746 +0200
+++ opensync_xmlformat.c	2010-02-07 20:48:29.560173871 +0100
@@ -247,19 +247,24 @@ OSyncXMLFieldList *osync_xmlformat_searc
 
 osync_bool osync_xmlformat_assemble(OSyncXMLFormat *xmlformat, char **buffer, unsigned int *size, OSyncError **error)
 {
+	osync_trace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, (void *)xmlformat, (void *)buffer, (void *)size, (void *)error);
+
 	osync_assert(xmlformat);
 	osync_assert(buffer);
 	osync_assert(size);
 	
 	xmlDocDumpFormatMemoryEnc(xmlformat->doc, (xmlChar **)buffer, (int *)size, NULL, 1);
+
+	osync_trace(TRACE_EXIT, "%s", __func__);
 	return TRUE;
 }
 
 osync_bool osync_xmlformat_sort(OSyncXMLFormat *xmlformat, OSyncError **error)
 {
-	int index;
-	OSyncXMLField *cur;
-	void **list = NULL;
+	int index = 0, final_index = 0;
+	OSyncXMLField *cur = NULL, *final_cur = NULL;
+	void **sorted_list = NULL;
+	int new_child_count = 0;
 
 	osync_trace(TRACE_ENTRY, "%s(%p)", __func__, xmlformat);
 	osync_assert(xmlformat);
@@ -268,40 +273,97 @@ osync_bool osync_xmlformat_sort(OSyncXML
 		osync_trace(TRACE_INTERNAL, "child_count <= 1 - no need to sort");
 		goto end;
 	}
+
+	new_child_count = xmlformat->child_count;
 	
-	list = osync_try_malloc0(sizeof(OSyncXMLField *) * xmlformat->child_count, error);
-	if (!list)
+	sorted_list = osync_try_malloc0(sizeof(OSyncXMLField *) * xmlformat->child_count, error);
+	if (!sorted_list)
 		goto error;
-	
+
+	// Fill sorted_list with the elements in an unsorted way	
 	index = 0;
 	cur = osync_xmlformat_get_first_field(xmlformat);
 	for(; cur != NULL; cur = osync_xmlfield_get_next(cur)) {
-		list[index] = cur;
+		sorted_list[index] = cur;
 		index++;
 		xmlUnlinkNode(cur->node);
 	}
 	
-	qsort(list, xmlformat->child_count, sizeof(OSyncXMLField *), osync_xmlfield_compare_stdlib);
+	// Bring sorted_list in a sorted state
+	qsort(sorted_list, xmlformat->child_count, sizeof(OSyncXMLField *), osync_xmlfield_compare_stdlib);
 	
-	/** bring the xmlformat and the xmldoc in a consistent state */
-	xmlformat->first_child = ((OSyncXMLField *)list[0])->node->_private;
-	xmlformat->last_child = ((OSyncXMLField *)list[xmlformat->child_count-1])->node->_private;
-
-	for(index = 0; index < xmlformat->child_count; index++) {
-		cur = (OSyncXMLField *)list[index];
-		xmlAddChild(xmlDocGetRootElement(xmlformat->doc), cur->node);
-			
-		if(index < xmlformat->child_count-1)
-			cur->next = (OSyncXMLField *)list[index+1];
-		else
-			cur->next = NULL;
+	
+	// Read from sorted_list and eliminate those elements, 
+	// that have the same node pointer as the preceding element.
+	// This can happen because of the way xmlAddChild() works.
+	for (index = 0, final_index = 0, final_cur = (OSyncXMLField *)sorted_list[0]; 
+		   index < xmlformat->child_count; 
+		   index++) 
+	{
+		cur = (OSyncXMLField *)sorted_list[index];
+		final_cur = (OSyncXMLField *)sorted_list[final_index];
+
+
+		cur->node = xmlAddChild(xmlDocGetRootElement(xmlformat->doc), cur->node);
+		if (cur->node == NULL) {
+			osync_error_set(error, OSYNC_ERROR_GENERIC, "xmlAddChild() has failed.");
+
+			goto error;
+		}	
+	
+
+		// Eliminate those elements that have the same node pointer as
+		// their preceding element.  Only relevant for index > 0.
+		if (index) {
+			if (((OSyncXMLField *)sorted_list[index])->node == ((OSyncXMLField *)sorted_list[index - 1])->node) {
+
+				// Now we have one child less
+				new_child_count--;
+
+				// Just in case, a further loop wouldn't be performed, any more...
+				final_cur->next = NULL;
+				((OSyncXMLField *)sorted_list[index - 1])->next = NULL;
+
+				// Proceed in for-loop
+				continue;
+			}
+		}
+
+		// Set parent, child and node and sorted
+		// As long as index == final_index, these assignments are redundant.
+		// As soon as final_index lags behind index, these assignments move
+		// in effect each element to positions with lower indexes.
+		final_cur->parent = ((OSyncXMLField *)sorted_list[index])->parent;
+		final_cur->child = ((OSyncXMLField *)sorted_list[index])->child;
+		final_cur->node = ((OSyncXMLField *)sorted_list[index])->node;
+		final_cur->sorted = ((OSyncXMLField *)sorted_list[index])->sorted;
+
+		// Set prev and next
+		if (final_index < new_child_count - 1) {
+			final_cur->next = (OSyncXMLField *)sorted_list[final_index + 1];
+			final_cur->next->prev = final_cur;
+		} else {
+			final_cur->next = NULL;
+ 		}
 		
-		if(index)
-			cur->prev = (OSyncXMLField *)list[index-1];
-		else
-			cur->prev = NULL;
+		if (final_index) {
+			final_cur->prev = (OSyncXMLField *)sorted_list[final_index - 1];
+			final_cur->prev->next = final_cur;
+		} else {
+			final_cur->prev = NULL;
+		}
+
+		// Increment final_index, when an element has been added, only.  
+		final_index++;
 	}
-	g_free(list);
+
+
+	/** bring the xmlformat and the xmldoc in a consistent state */
+	xmlformat->first_child = ((OSyncXMLField *)sorted_list[0])->node->_private;
+	xmlformat->last_child = ((OSyncXMLField *)sorted_list[new_child_count - 1])->node->_private;
+	xmlformat->child_count = final_index;
+
+	g_free(sorted_list);
 
  end:	
 	xmlformat->sorted = TRUE;

