X12Translator Class

Properties   Methods   Events   Config Settings   Errors  

The X12Translator class converts X12 documents to XML and back, streamlining translation between formats.

Syntax

class edisdk.X12Translator

Remarks

The class will convert a document from the format specified by input_format to the format specified by output_format. This allows for converting to XML from EDI and vice versa.

Before translating from EDI to XML it is recommended to load a schema using the load_schema method. This ensures additional information can be included in the XML or JSON document. If a schema is specified the XML will include types and descriptions as element attributes which are useful for interpreting the data.

After calling translate the resulting output will contain the EDI or XML data as defined by output_format.

Input and Output Properties

The class will determine the source and destination of the input and output based on which properties are set.

The order in which the input properties are checked is as follows:

When a valid source is found the search stops. The order in which the output properties are checked is as follows:

Property List


The following is the full list of the properties of the class with short descriptions. Click on the links for further details.

delimiter_componentThe delimiter separating classes.
delimiter_elementThe delimiter separating elements.
delimiter_repetitionThe delimiter separating classes.
delimiter_segmentThe delimiter separating segments.
file_write_modeDetermines how the output file is opened for writing.
input_dataThe EDI message, specified as a string.
input_fileThe file containing the EDI message.
input_formatThe format of the input data.
output_dataThe translated EDI data.
output_fileThe name of the file to write the translated EDI data to.
output_formatThe format of the output data.
segment_suffixThe suffix that is appended to the end of a segment.

Method List


The following is the full list of the methods of the class with short descriptions. Click on the links for further details.

configSets or retrieves a configuration setting.
load_schemaLoads schema information from a file or string data.
resetResets all properties to their default values.
translateTranslates the specified data.

Event List


The following is the full list of the events fired by the class with short descriptions. Click on the links for further details.

on_errorFires when a parsing error occurs.
on_resolve_schemaFires to request schema information for a specific transaction set.
on_validate_warningFires to warn about validation errors.

Config Settings


The following is a list of config settings for the class with short descriptions. Click on the links for further details.

CloseStreamAfterTranslateWhether to close the output stream after translation.
EncodingThe character encoding to use for the output X12 document.
UseDescriptionsWhether descriptions will be included in the output XML.

delimiter_component property

The delimiter separating classes.

Syntax

def get_delimiter_component() -> str: ...
def set_delimiter_component(value: str) -> None: ...

delimiter_component = property(get_delimiter_component, set_delimiter_component)

Default Value

":"

Remarks

The delimiter separating components.

In X12Reader, this type is used to hold the delimiters found when parsing the component, while in X12Writer the fields can be set to choose the delimiters that will be used when writing

delimiter_element property

The delimiter separating elements.

Syntax

def get_delimiter_element() -> str: ...
def set_delimiter_element(value: str) -> None: ...

delimiter_element = property(get_delimiter_element, set_delimiter_element)

Default Value

"*"

Remarks

The delimiter separating elements.

In X12Reader, this type is used to hold the delimiters found when parsing the element, while in X12Writer the fields can be set to choose the delimiters that will be used when writing

delimiter_repetition property

The delimiter separating classes.

Syntax

def get_delimiter_repetition() -> str: ...
def set_delimiter_repetition(value: str) -> None: ...

delimiter_repetition = property(get_delimiter_repetition, set_delimiter_repetition)

Default Value

"^"

Remarks

The delimiter separating components.

In X12Reader, this type is used to hold the delimiters found when parsing the component, while in X12Writer the fields can be set to choose the delimiters that will be used when writing

delimiter_segment property

The delimiter separating segments.

Syntax

def get_delimiter_segment() -> str: ...
def set_delimiter_segment(value: str) -> None: ...

delimiter_segment = property(get_delimiter_segment, set_delimiter_segment)

Default Value

"~"

Remarks

The delimiter separating segments.

In X12Reader, this type is used to hold the delimiters found when parsing the segment, while in X12Writer the fields can be set to choose the delimiters that will be used when writing

file_write_mode property

Determines how the output file is opened for writing.

Syntax

def get_file_write_mode() -> int: ...
def set_file_write_mode(value: int) -> None: ...

file_write_mode = property(get_file_write_mode, set_file_write_mode)

Possible Values

0   # Create
1 # Overwrite
2 # Append

Default Value

0

Remarks

When output_file is set, this property determines how the file will be opened.

fwmCreateCreates a new file. If the file already exists, the translation will fail.
fwmOverwriteOpens an existing file and overwrites its contents. If the file does not exist, a new file will be created.
fwmAppendOpens an existing file and appends data to the end of the file. If the file does not exist, a new file will be created.

input_data property

The EDI message, specified as a string.

Syntax

def get_input_data() -> str: ...
def set_input_data(value: str) -> None: ...

input_data = property(get_input_data, set_input_data)

Default Value

""

Remarks

Set this property to specify the input EDI message as a string. The message can then be parsed with parse.

input_file property

The file containing the EDI message.

Syntax

def get_input_file() -> str: ...
def set_input_file(value: str) -> None: ...

input_file = property(get_input_file, set_input_file)

Default Value

""

Remarks

Set this property to specify the input EDI message as a file. The message can then be parsed with parse.

If the file is not found or cannot be opened, an error will be returned.

Note: If both input_file and input_data are set, input_data takes precedence.

input_format property

The format of the input data.

Syntax

def get_input_format() -> int: ...
def set_input_format(value: int) -> None: ...

input_format = property(get_input_format, set_input_format)

Possible Values

0   # EDI
1 # XML

Default Value

0

Remarks

The value set here, along with output_format, determines how the data is converted when translate is called.

Possible values are:

  • 0 (ifEDI)
  • 1 (ifXML)

output_data property

The translated EDI data.

Syntax

def get_output_data() -> str: ...
def set_output_data(value: str) -> None: ...

output_data = property(get_output_data, set_output_data)

Default Value

""

Remarks

This property will be populated with the translated data after calling translate if output_file and set_output_stream are not set.

output_file property

The name of the file to write the translated EDI data to.

Syntax

def get_output_file() -> str: ...
def set_output_file(value: str) -> None: ...

output_file = property(get_output_file, set_output_file)

Default Value

""

Remarks

This file specified by this property will be populated with the output after calling translate if set_output_stream is not set.

If a file with the specified name already exists translate will fail unless file_write_mode is set to "fwmOverwrite" or "fwmAppend".

output_format property

The format of the output data.

Syntax

def get_output_format() -> int: ...
def set_output_format(value: int) -> None: ...

output_format = property(get_output_format, set_output_format)

Possible Values

0   # EDI
1 # XML

Default Value

0

Remarks

The value set here, along with input_format, determines how the data is converted when translate is called.

Possible values are:

  • 0 (ofEDI)
  • 1 (ofXML)

segment_suffix property

The suffix that is appended to the end of a segment.

Syntax

def get_segment_suffix() -> int: ...
def set_segment_suffix(value: int) -> None: ...

segment_suffix = property(get_segment_suffix, set_segment_suffix)

Possible Values

0   # None
1 # CR
2 # LF
3 # CRLF

Default Value

3

Remarks

This property determines the suffix that is appended to the end of a segment.

Possible values are:

ssNone (0)No suffix is appended to the segment.
ssCR (1)A carriage return (ASCII 13) is appended to the segment.
ssLF (2)A line feed (ASCII 10) is appended to the segment.
ssCRLF (3)A carriage return followed by a line feed (ASCII 13 and 10) is appended to the segment. This is the default value.

config method

Sets or retrieves a configuration setting.

Syntax

def config(configuration_string: str) -> str: ...

Remarks

config is a generic method available in every class. It is used to set and retrieve configuration settings for the class.

These settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the class, access to these internal properties is provided through the config method.

To set a configuration setting named PROPERTY, you must call Config("PROPERTY=VALUE"), where VALUE is the value of the setting expressed as a string. For boolean values, use the strings "True", "False", "0", "1", "Yes", or "No" (case does not matter).

To read (query) the value of a configuration setting, you must call Config("PROPERTY"). The value will be returned as a string.

load_schema method

Loads schema information from a file or string data.

Syntax

def load_schema(file_or_data: str, messagename: str) -> None: ...

Remarks

This method loads schema information for use when parsing, validating, and translating messages.

The FileOrData parameter can be set to the path of a file containing schema data, or can be set to the contents of a schema file directly.

MessageName can be set to the name of a specific message to load only the specified message's schema information. If left empty or set to "*", the entire schema will be loaded.

reset method

Resets all properties to their default values.

Syntax

def reset() -> None: ...

Remarks

This method resets all properties of the component to their default values as specified in the property definitions.

translate method

Translates the specified data.

Syntax

def translate() -> None: ...

Remarks

This method translates the specified data.

on_error event

Fires when a parsing error occurs.

Syntax

class X12TranslatorErrorEventParams(object):
  @property
  def error_code() -> int: ...

  @property
  def description() -> str: ...

# In class X12Translator:
@property
def on_error() -> Callable[[X12TranslatorErrorEventParams], None]: ...
@on_error.setter
def on_error(event_hook: Callable[[X12TranslatorErrorEventParams], None]) -> None: ...

Remarks

This event fires when the class encounters a parsing error while processing a document.

on_resolve_schema event

Fires to request schema information for a specific transaction set.

Syntax

class X12TranslatorResolveSchemaEventParams(object):
  @property
  def version_number() -> str: ...

  @property
  def transaction_set_id_code() -> str: ...

# In class X12Translator:
@property
def on_resolve_schema() -> Callable[[X12TranslatorResolveSchemaEventParams], None]: ...
@on_resolve_schema.setter
def on_resolve_schema(event_hook: Callable[[X12TranslatorResolveSchemaEventParams], None]) -> None: ...

Remarks

load_schema can be called from within this event to load the schema information for the specified transaction set.

on_validate_warning event

Fires to warn about validation errors.

Syntax

class X12TranslatorValidateWarningEventParams(object):
  @property
  def position() -> int: ...

  @property
  def line() -> int: ...

  @property
  def column() -> int: ...

  @property
  def error_code() -> str: ...

  @property
  def error_message() -> str: ...

  @property
  def error_type() -> int: ...

# In class X12Translator:
@property
def on_validate_warning() -> Callable[[X12TranslatorValidateWarningEventParams], None]: ...
@on_validate_warning.setter
def on_validate_warning(event_hook: Callable[[X12TranslatorValidateWarningEventParams], None]) -> None: ...

Remarks

When the class encounters a non-blocking error while validating a document, this event fires with information about the error.

X12Translator Config Settings

The class accepts one or more of the following configuration settings. Configuration settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the class, access to these internal properties is provided through the config method.

X12Translator Config Settings

CloseStreamAfterTranslate:   Whether to close the output stream after translation.

This setting controls whether the output stream is closed after the translation process is complete. The default value is true.

Encoding:   The character encoding to use for the output X12 document.

This setting specifies the character encoding to be used for the output X12 document. The default value is "UTF-8".

UseDescriptions:   Whether descriptions will be included in the output XML.

This setting controls whether descriptions are included in the output XML. The default value is true.

X12Translator Errors

X12Translator Errors

202   Invalid argument.
304   Cannot write to file.
1003   Could not find the selected schema in the chosen file.
1099   Encountered an unexpected segment tag.
1100   Attempted to perform an action while the component was busy, or close an incomplete interchange.
1044   Tried to load an invalid schema file.
1203   Encountered an error while validating the UNA segment.