Syntax notation
The syntax for X12 is defined in a BNF (Backus-Naur Form). Each definition is accompanied by explanatory text. Sometimes, actual use, that is, the actual data stream, of an X12 structure might not be the same as the definition. This is because some structures are optional.
All data element values, except those of the binary data element, are constructed using a character set. The character set of this standard is grouped according to common characteristics.
The X12 standards are graphic-character oriented, so any common character-encoding schemes may be used as long as a common mapping is available. No collating sequence is to be assumed in any definition used in the standard. This is because no single character code is specified and no other means of specifying a sequence is provided.
Delimiters consist of two levels of separators and a terminator. The delimiters are an integral part of the transferred data stream. Delimiters are specified in the interchange header. They are not used in a data element value elsewhere in the interchange, with the exception of their use in the binary data element. From highest to lowest level, the separators and terminator are:
- Segment terminator
<tr> ::
- Data element separator
<gs> ::
- Component element separator
<us> ::
To set encoding separators for outbound messages, elements and an ENCODE
keyed list are added to the SEPCHARS
keyed list. This make the new SEPCHARS
look similar to:
{FIELD "XXX"}
{COMPONENT "XXX"}
{SEGMENT "XXX"}
{REPEAT "XXX"}
{ENCODE
{
{FIELD "XXX"}
{COMPONENT "XXX"}
{SEGMENT "XXX"}
{REPEAT "XXX"}
}
}
To change the segment separator of an outbound message, set the ENCODE.SEGMENT
value in an Xlate CALL
action:
set sepchars [xpmmetaget $xlateId SEPCHARS] keylset sepchars
ENCODE.SEGMENT "\r\n"
xpmmetaset $xlateId SEPCHARS $sepchars
You can also set the ENCODE.FIELD
, ENCODE.COMPONENT
, and ENCODE.REPEAT
values in an Xlate CALL
action to encode.
This is an example for encoding an X12 message using SEGMENT "@"
and FIELD "%"
.
This is only in a Tcl program.
set msgId [msgcreate]
msginsert $msgId "ST*835~BPR*RA*9491.85~"
set seps [msgmetaget $msgId SEPCHARS]
keylset seps ENCODE.SEGMENT "@"
keylset seps ENCODE.FIELD "%"
msgmetaset $msgId SEPCHARS $seps
set grmId [grmcreate -msg $msgId x12 004030 {} 835]
set msgId [grmencode $grmId]
Data elements
The data element is the smallest named unit of information in the standard. Data elements are identified as "simple" or "component".
A data element that happens as an ordinally-positioned member of a composite data structure is identified as a component data element.
A data element that happens in a segment outside of the defined boundaries of a composite data structure is identified as a simple data element.
The distinction between simple and component data elements is only a matter of context because a data element can be used in both capacities.
These definitions apply equally to each class of data element:
simple_data_element :: data_element
component_data_element :: data_element
data_element :: numeric | decimal_number | id |
string | date | time | binary |
fixed_length_string
Composite data structure
The composite data structure is an intermediate unit of information in a
segment. This consists of two or more component data elements preceded by a data
element separator. In use a composite data structure may display as only one
component data element. Each component data element with the composite data
structure, except the last, is followed by a component element separator. The final
component data element is followed by the next data element separator or the segment
terminator. Trailing component data element separators <us>
are suppressed.
Composite data structures are defined in a composite data structure directory. The directory defines each composite data structure by its name, purpose, reference identifier, and included component data elements in a specified sequence.
A composite data structure is constructed in this manner:
Definition:
composite_data_structure ::= component_data_element
<us> component_data_element
{<us> component_data_element}
Usage:
composite_data_structure ::= {[component_data_element]
<us>} component_data_element
Data segment
The data segment is an intermediate unit of information in a transaction set. A data segment consists of a segment identifier; one or more composite data structures or simple data elements, each preceded by a data element separator; and ending with a segment terminator. Trailing data element separators <gs> are suppressed.
Data segments are defined in a data segment directory. The directory defines each segment including the segment's name, purpose, and identifier. The directory also defines composite data structures and data elements that a segment contains in their specified order.
A data segment is constructed in this manner:
Definition:
data_segment ::= seg_id <gs> data_segment_unit
{<gs> data_segment_unit} <tr>
Usage:
data_segment ::= seg_id {<gs> [data_segment_unit]}
<gs> data_segment_unit <tr>
data_segment_unit ::= simple_data_element |
composite_data_structure
Transaction set
A transaction set is a semantically meaningful unit of information exchanged between trading partners.
The transaction set consists of:
- A transaction set header segment.
- A transaction security header segment. This is optional.
- One or more data segments and loop-control segments, if bounded loops exist, in a specified order.
- A transaction security trailer segment whenever the security header segment is present.
- A transaction set trailer segment.
A transaction set is constructed in this manner:
transaction_set ::= unsecured_trans_set |
secured_trans_set
unsecured_trans_set ::= trans_set_header
data_segment_group {data_segment_group}
trans_set_trailer
secured_trans_set ::= trans_set_header
trans_security_header data_segment_group
{data_segment_group} trans_security_trailer
trans_set_trailer