X12 mapping - How to validate next segments?

Is there a simple way to check whether the next segment within the ST section of an X12 EDI doc matches an expected segment? I’ve got an X12 doc that the schema validates properly, but within the mapping, I need conditional checks where if a certain segment exists, the next segment should be a certain name and also exist. I know how to check for field values matching expected values, but not how to check whether the segment itself is present. Any thoughts for a newbie?


Sounds like you’re duplicating the validation checks. Would it not be okay to just assume that the segments you expect to be there exist? Otherwise, validation would have failed, right?

In any case, do this to check for the existence of a segment:

BRANCH on ‘/ST/N1/REF’ (or whatever segment you want to check)
…$null: SEQUENCE (segment does not exist)
…$default: SEQUENCE (segment exists)

It sounds like you may be checking to see if business rules, rather than just X12 rules, are being followed. For example, if /ST/N1/N101 = “ST” then it should be followed by N2, N3 and N4 segments containing address information, otherwise not. One way to do this within, say, a service to map an X12 850 to an XML purchase order, would be to write the service as if the necessary data exists and validate the resulting XML. If you end up with all the data you need, then it doesn’t matter exactly how it was presented in the 850. If the exact structure of the 850, beyond the X12 requirements, really is an issue, I’d be interested to know why that is. There might be an easier way to accomplish what you need than inserting a lot of extra validation logic to your service.


Appears I am looking too far into the custom validation, and can rely on the X12 validation. Thanks for the knowledge on checking segment existence.