Search code examples
hl7hl7-v2

Does the order of segments in HL7 V2 matter?


I know that the order of fields and components matter, but what about the order of segments in an HL7 message? They all obviously have to have the MSH at the beginning, but is there anything in the HL7 guides that explicitly state that hl7 Segments must be in a particular order. Certainly, the documentation lists segments in a certain order when describing a message type, but isn't that just the order it was written down? Do you need to have your messages in the same order (other than the grouped items)? I would have thought that the PID-1 would be irrelevant if the order was set by the order in the message.

I'm keen to hear any opinions, but I would particularly like to hear from someone that can reference some documentation that specifies this.


Solution

  • Yes it does matter -

    There is a specific requirement that a required segment be in between two identical segments.

    From version 2.5.1 chapter 2:

    A named segment X may occur more than once in an abstract message syntax. This differs from repetition described earlier in this section.

    When this occurs, the following rules must be adhered to: If, within an abstract message syntax, a named segment X appears in two individual or group locations, and a) Either appearance is optional or repeating in an individual location; b) or, either appearance is optional or repeating, in a group location then, the occurrences of segment X must be separated by at least one required segment of a different name so that no ambiguity can exist as to the individual or group location of any occurrence of segment X in a message instance.

    A real world example of this is ROL segments in ADT^A02, one follows PD1, and one follows PV2, but PV1 is required in between the two.

    If you're writing some kind of parser though, I would be wary of anyone actually respecting this rule.