web stats
Mirth Community - View Single Post - Assigning Arbitrary Empty XML Node
View Single Post
Old 04-18-2019, 10:16 AM
agermano agermano is offline
Mirth Guru
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,009
agermano is on a distinguished road

You can do it with e4x templating like this:
var hl7FieldName = 'PID.5';
msgXML['PID'][hl7FieldName] = <{hl7FieldName}/>;
The magical mystery function is a mirth helper function called createSegment.
var hl7FieldName = 'PID.5';
var emptyXmlNode = createSegment(hl7FieldName);
msgXML['PID']['PID.5'] = emptyXmlNode;
You can add the parent object and it will insert it for you, but this will only replace the first occurrence if the field or segment is repeating. The previous two examples will replace a repeating PID.5 field with a single empty occurence.
var hl7FieldName = 'PID.5';
var emptyXmlNode = createSegment(hl7FieldName, msgXML['PID']);
And if you know that msgXML['PID']['PID.5'] refers to a single node that already exists in the message, you can do this to empty it. It will complain that setChildren isn't a function if msgXML['PID']['PID.5'] doesn't exist or returns multiple nodes.
msgXML['PID']['PID.5'].setChildren(new XMLList());
Reply With Quote