Prevent escaping of HL7 special characters

Verified
Added by iNTERFACEWARE

How to prevent escaping of special characters like "&" in an HL7 message

Source Code
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
Description
How to prevent escaping of special characters like "&" in an HL7 message
Usage Details

This example demonstrates how to prevent escaping special characters in an HL7 message. In this case we need to have an ampersand at the start of each OBX identifier field (this was a legacy requirement from a customer). The code will actually replace “\T\” at the start of any segment, but as only OBX was affected this was sufficient to solve the problem.

First we add the “&” at the start of each the OBX identifier in an HL7 node tree. The node tree encodes this as “\T\” (as specified in the HL7 standard). As a final step before we forward the message we simply convert it to text, and replace “\T\” with “&”.

Note: You will need to adapt the replacement logic to meet your requirements.

How to use the code:

  • Use a Filter or To Translator script
    • Add the code
    • Load the sample messages from SampleData.txt
  • Alternatively you can also load the attached project which already contains the sample messages
  • Inspect the code and annotations to see how it works