Skip to main content

HL7 Escape Sequences

Provides reference information for HL7 escape sequences.

Details

When separator characters need to appear within the data contents of an HL7 message, HL7 provides escape sequences to replace the separator characters.

The character most likely to require this feature is the & (ampersand) character. It is often used as an HL7 separator. At the same time, & is likely to appear in HL7 document data, either within the legal names of employers (Parker & Sons) or as shorthand in treatment orders (XR CHEST PA&LAT).

Thus, within the HL7 data stream you often see & characters replaced by an escape sequence, such as the \T\ in the following samples:

Parker \T\ Sons
XR CHEST PA\T\LAT

The following table lists the standard HL7 escape sequences and their meanings. HL7 escape sequences begin and end with the escape character as defined in the MSH segment. HL7 escape sequences are case-sensitive, and they use the specific characters F R S T E as well as .br, X0A, and X0D as shown in the table. In the examples shown, the \ (backslash) is the escape character.

This character sequence... If found within HL7 data, indicates... For example...
\.br\* Carriage return  
\F\ Field separator character |
\R\ Repetition separator character ~
\S\ Component separator character ^
\T\ Subcomponent separator character &
\E\ Escape character \
\X0A\ Line feed  
\X0D\ Carriage return  

* Only used for unescaping.

If you <assign> a literal string to a target field using DTL, and that string needs to include HL7 separator characters, either as separators or as literal characters, some special handling is required. The procedures are as follows.

Suppose you have a source message in which ^, \, and & are separators as listed in the MSH segment:

MSH|^~\&

Now suppose that, for all ADT_A01 messages that arrive from a certain clinical source, your DTL data transformation needs to assign a literal value to the InsuranceCompanyName field. The correct value has been agreed upon by all concerned parties, including the insurance company, whose name is Pierre DuRhône & Cie. The value is to appear in the HL7 data stream as follows:

Pierre DuRho^ne & Cie

In this case, you need HL7 escape sequences to replace ^ and & within the data, as follows:

<assign
 property='target.{PIDgrp.IN1grp(1).IN1:InsuranceCompanyName.organizationname}'
 value='"Pierre DuRho\S\ne \T\ Cie"'/>

The only other case in which you need to be concerned about separator characters is if you wish to migrate data into or out of an Ensemble HL7 virtual document and you are not using an HL7 to HL7 data transformation. For example, you neeed to explicitly deal with separator characters if you a transforming from XML to HL7 or if you are not using a transformation at all. The EnsLib.HL7.SegmentOpens in a new tab class provides explicit Escape() and Unescape() methods that you can invoke to accomplish manually what a DTL data transformation does automatically to unescape and escape HL7 messages. A replace() method also exists.

FeedbackOpens in a new tab