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
  #1  
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:

Code:
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:

MSH|^~\&|||||||ADT^A01|||

then we get the following encoded output:

MSH|^~\&|||||||ADT^A01||P|2.3|||||||English

as expected.

However, if we send the following message:

MSH|^~\&|||||||ADT^A01|

then we get:

MSH|^~\&|||||||ADT^A01||||||||||English|P|2.3

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:

Code:
<HL7Message>
     <MSH>
          <MSH.1>|</MSH.1>
          <MSH.2>^~\&amp;</MSH.2>
          <MSH.9>
               <MSH.9.1>ADT</MSH.9.1>
               <MSH.9.2>A01</MSH.9.2>
          </MSH.9>
     </MSH>
</HL7Message>
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.

Code:
function fixHL7NodeOrder(parent,node) {
	for each (child in node.children())
		if (child.hasComplexContent())
			fixHL7NodeOrder(node,child);
	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) {
					newNode.insertChildBefore(newNode.children()[i],child);
					inserted = true;
					break;
				}
			}
			if (!inserted)
				newNode.appendChild(child);
		}
	}
	return newNode;
}
So now, let's consider the following transformer:

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

MSH|^~\&|||||||ADT^A01|

then we get:

MSH|^~\&|||||||ADT^A01||P|2.3|||||||English

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