web stats
Mirth Community - View Single Post - Mirth inserts fields out of order if they don't already exist in the XML object
View Single Post
Old 12-20-2011, 08:47 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
Join Date: Oct 2010
Posts: 7,126
narupley is on a distinguished road
Exclamation Mirth inserts fields out of order if they don't already exist in the XML object

Say we have the following transformer in a channel:

msg.MSH['MSH.19'] = 'English';
msg.MSH['MSH.11'] = 'P';
msg.MSH['MSH.12'] = '2.3';
If we send the following message to the channel:


then we get the following encoded output:


as expected.

However, if we send the following message:


then we get:


which is certainly not expected. This is because in the second case, the children <MSH.11> and <MSH.12> under the <MSH> node don't yet exist. Because we initialized MSH.11 and MSH.12 after MSH.19, they appear after MSH.19 in the XML tree. Well, that makes perfect sense to me. What doesn't make sense is why Mirth doesn't convert the XML correctly back into HL7. If we have the following XML:

then Mirth knows that even though <MSH.9> comes right after <MSH.2>, it's not really the next field in the message; Mirth needs to pad some extra field separators in there. However, if some XML children are out of order, then Mirth apparently doesn't know where to put them, and so things get jumbled up.

I went ahead and wrote the following subroutine, which (in my opinion) should probably have been included in the default logic for the HL7 serializer in SerializerFactory.

function fixHL7NodeOrder(parent,node) {
	for each (child in node.children())
		if (child.hasComplexContent())
	if (parent != node)
		parent.children()[node.childIndex()] = sortHL7Node(node);

function sortHL7Node(node) {
	if (node.hasSimpleContent())
		return node;
	var newNode = new XML('<'+node.name().toString()+'/>');
	for each (child in node.children()) {
		if (child.name()) {
			curChildIndex = parseInt(child.name().toString().substring(child.name().toString().lastIndexOf('.')+1),10);
			var inserted = false;
			for (var i = 0; i <= newNode.children().length()-1; i++) {
				loopChildIndex = parseInt(newNode.child(i).name().toString().substring(newNode.child(i).name().toString().lastIndexOf('.')+1),10);
				if (curChildIndex < loopChildIndex) {
					inserted = true;
			if (!inserted)
	return newNode;
So now, let's consider the following transformer:

msg.MSH['MSH.19'] = 'English';
msg.MSH['MSH.11'] = 'P';
msg.MSH['MSH.12'] = '2.3';
When we send the following message:


then we get:


as expected. Hope this helps.....
Attached Files
File Type: xml TESTING.xml (5.0 KB, 16 views)

Last edited by narupley; 12-22-2011 at 07:16 AM. Reason: Found and fixed a small bug...
Reply With Quote