Sidebar

Match and Remove all segments after the first of a type?

0 votes
807 views
asked Apr 21, 2016 by joshua-t-2800 (150 points)

I think this is a muti part problem, But I have at least one of the parts down.

first I need to analyze my OBX segements, then I need to consolidate the OBX-5 segments into one OBX-5 field. This part i have.

Now I need to remove all OBX segments after OBX[1].

from obx[1] to obx[*] is the end of the file. I tried to do some HPath to get all OBX segments after the first but I am not successfull.


Secondly I need to remove said segments from the message.

 

Since I was unable to succesfully HPath the group I need, I decided to go about it this way:

 
var obx = message.getAllNodes('OBX');
 
for (var i = 0; i < obx.length; i++) {
   message.removeAllNodes('OBX[' + i + ']');
}
 
this however removes every odd OBX segment.
 
 
So the question is as stands:
1. How can i use HPath to get the gorup i am looking for
2. how can I remove repeatign segments after the first?

 

commented Apr 21, 2016 by joshua-t-2800 (150 points)
it looks like I can  use HPATH of OBX[@1!=1] which returns a grouping of all OBX except for OBX[1]

is there  a better way of doing this?

1 Answer

+1 vote
 
Best answer
This code will group all of the OBX segments and delete all of the segments except OBX[1].  It doesn't matter what order the segments are in because it is only looking for OBX[1].
 
 
var obxsegs = message.getAllNodes('OBX');
for (var i = 0; i < obxsegs.length; i++) {
   var obxseg = qie.parseHL7String(obxsegs[i]);
   if (!StringUtils.equals(obxseg.getNode('OBX-1'),1)) {
      message.removeLastNode('OBX');
   }
}
 
answered Apr 21, 2016 by terrie-g-7436 (3,950 points)
selected Apr 25, 2016 by joshua-t-2800
commented Apr 21, 2016 by joshua-t-2800 (150 points)
Ok this works, and I need to read up on parseHL7. . . But now it's time to play devil's advocate.  why is your answer better than the simple solution I came up with:

message.removeAllNodes('OBX[@1!=1]');


Also can you guys implement a syntax highlighter on this site? would make things so much easier to read!
commented Apr 25, 2016 by joshua-t-2800 (150 points)
Ok turns out I did need the more complicated matching feature, but only so I could edit a specific segment. Still using my 1 line code to remove unnecessary segments though!
...