web stats
Mirth Community - View Single Post - Mirth Tools: User defined functions
View Single Post
  #54  
Old 04-20-2018, 10:11 AM
jridderhoff jridderhoff is offline
OBX.1 Kenobi
 
Join Date: Jan 2015
Posts: 42
jridderhoff is on a distinguished road
Default Get HL7v2 text-delimited representation of field from XML fragment

We had a specific use case where we needed to get the original HL7 "pipe-delimited" representation of selected fields from incoming HL7v2 text messages and use them elsewhere in the channel output. This function allowed us to transform the HL7 message to an E4X message object, pick the specific fields we needed, and convert the XML fragments back to how they appeared in the original HL7 pipe-delimited message.

Function will handle fields with repeating values as well as empty components/sub-components.

The function:
Code:
/**
 * Function to convert an E4X XML fragment representing an HL7 field into its original HL7 text representation
 * 
 * @param XMLList nodeList         XML representing the field to translate.
 * @param array   joinStrArray     Array containing your HL7 delimiters for repeating fields, components, and subcomponents
 * @param int     joinStrIdx       Index representing the current delimiter in joinStrArray to use (should be 0 on first call)
 * @param int     startComponentId Used for recursion; leave null for first call. (Represents the sub-field element the function is starting from.)
 *
 * @return string
 */
function xmlToPipes(nodeList, joinStrArray, joinStrIdx, startComponentId) {
	var elementList = [];
	var componentIndex = null;

	if (startComponentId !== undefined) {
		componentIndex = parseInt(startComponentId);
	}

	for each (node in nodeList) {
		var nodeChildren = node.elements();
		var nodeNameParts = node.name().toString().split('.');
		var nodeComponentIndex = parseInt(nodeNameParts.pop());
		
		if (componentIndex === null) {
			componentIndex = nodeComponentIndex;
		}
		
		var componentIndexDiff = nodeComponentIndex - componentIndex;
		
		if (componentIndexDiff > 0) {
			for (var i = 0; i < componentIndexDiff; i++) {
				elementList.push('');
				componentIndex++;
			}
		}

		if (nodeChildren.length() > 0) {
			elementList.push(xmlToPipes(nodeChildren, joinStrArray, joinStrIdx+1, 1));
		} else {
			elementList.push(node.toString());
		}

		componentIndex++;
	}

	return elementList.join(joinStrArray[joinStrIdx]);
}
Usage example:
Code:
// array should contain HL7 delimiters you use for (in order) repeating fields, components, and subcomponents
var joinStrStarterArray = ["~", "^", "&"];

var msh4 = xmlToPipes(msg['MSH']['MSH.4'], joinStrStarterArray, 0);
var pid3 = xmlToPipes(msg['ORU_R01.PATIENT_RESULT']['ORU_R01.PATIENT']['PID']['PID.3'], joinStrStarterArray, 0);
Reply With Quote