Sidebar

What is the best way to remove all segments in a certain condition?

0 votes
636 views
asked Jul 10, 2018 by joe-d-8335 (180 points)
retagged Jul 10, 2018 by joe-d-8335
I've been working through the problem to the point where I am confident that in a condition block I am only seeing the nodes I want, but I don't think that removeAllNodes and removeFirstNode is doing what I expect it to do. Instead of deleting the matching segment, it either deletes all DG1 segments or deletes the first DG1 segment (instead of the matching segments).

In this example below, I have 14 DG1 segments, and the last 2 (13 and 14) match the condition.

var nodes = message.getAllNodes('DG1');

for (var i=0; i < nodes.length; i++) {      
   if (StringUtils.contains(nodes[i], 'I9')) {
      //message.removeAllNodes('DG1['+nodes[i]+']'); // This deletes all DG1 segments
      message.removeFirstNode('DG1['+nodes[i]+']'); // This deletes the first and second DG1 segments.
   }
}

1 Answer

0 votes
The node path in the remove statements is wrong.  It should be:

  message.removeFirstNode('DG1['+(i+1)+']');

 or

 message.removeAllNodes('DG1['+(i+1)+']');
answered Jul 10, 2018 by terrie-g-7436 (3,950 points)
commented Jul 10, 2018 by joe-d-8335 (180 points)
This was much closer than I had, but it was still one off. It removed the segment before the match (12 and 13 instead of 13 and 14). I also discovered how to drill down deeper in the nodepath and I was able to handle it without iteration:

message.removeAllNodes('DG1[@2=I9]');
...