Delete A Group Of Nodes If It Contains a Certain Node And Segment

0 votes
asked May 26 by chris-m-3214 (320 points)
I have X12 files with groups of nodes, INS.  Each X12 files contains numerous groups of INS nodes. Each group contains an address row with state.  I would like to split each group of INS nodes into separate files, by in state (Oregon) and out of state.  I tried the code below but it's removing every group of INS nodes, not just the group I need to get rid of.  This is the mapping that I tried to use to remove any group that is out of state.  How do I split these files properly?

//Get all INS groups. Group up to the next INS node
var insGroups = message.getAllNodes('INS[group=!INS]');

//Loop through each INS group
for (var i = 0; i < insGroups.length; i++) {
   //Parse the string as x12 to allow lookup.
   var insGroup = qie.parseX12String(insGroups[i]);
   //Check if an N4 segment is an Oregon member.
   if (insGroup.checkNodeExists('N4[@2=OR]')) {
      // just set a variable so we don't get an empty block error
      var myvar = '';
   else {

1 Answer

0 votes

The message.removeAllNodes() function will remove all the nodes in the message object, not just the specific one you're working with in your loop.  The node path 'INS[group=!INS]' will match all the nodes from one INS segment to the next as a series of groups, but when you pass that node path to a removeAllNodes() function, it will find those groups and remove them all.

The best way to split messages is to first get the groups (which you're doing with your message.getAllNodes() function), remove all the groups from the message object, and then add back in just the items you want included in the message.  You'll likely want to use qie.spawnNewMessage() to create a new message object with your segments added back in.

For an example of splitting a single message into multiple messages, see this KB, which is for HL7 but will be useful as an example.

answered May 26 by jon-t-7005 (4,890 points)
commented May 26 by chris-m-3214 (320 points)
Thank you for that.  That KB link is helpful but it looks like it requires me to code for each type of node in the group.  It does create an additional problem, in that, each INS group can contain a varying quantity and type of nodes.  How can I take every node from an acceptable INS group and add it to a new message?
commented May 27 by jon-t-7005 (4,890 points)
It might be best if we get connected to take a look at your specific scenario.  Please send an email to to coordinate a meeting.