How can I prevent HL7 escaping of “&” and other delimiters?

We had a customer with a special case where they needed to include the “&” character in an HL7 code field (the Identifier field in an OBX segment). However the “&” is an HL7 delimiter and therefore it is encoded as “\T\” when it is included within a field in an HL7 node tree (as specified by the HL7 standard). In 99.9% this is the behaviour you want, however in this case the customer was dealing with a legacy system that expected an “&” at the start of the field.

The solution is simple: Convert the message to text and then replace the encoded text “\T\” with an “&”.

Note: This code is not production ready you will need to adapt it to your needs and test it before using it in a production system.

Here is an example that does that:

Here is the code and a sample message containing “\T\” SampleData.txt:

You can also load this project preventEscaping that contains the code and message.

function main(Data)
   local Msg, Name = hl7.parse{vmd='example/demo.vmd', data = Data} 
   local Out = hl7.message{vmd='example/demo.vmd', name = Name} 
   
   Out:mapTree(Msg)
   
   -- message processing goes here...
      
   local S = Out.OBX:S()
   trace(S)
      
   -- This will "unescape" \T\ at the start of any segment 
   -- not just OBX - you can add extra logic if needed
   S = S:gsub('|\\T\\','|&')
   trace(S)
   
end

function string.startsWith(s,c)
   if s:find(c,1,true)==1 then return true end
end

Please contact support at support@interfaceware.com if you need more help.