Accessing Message Data with E4X
<?xml version="1.0"?>
<HL7Message>
<MSH>
<MSH.1>|</MSH.1>
<MSH.2>|^~\&</MSH.2>
<MSH.3>
<MSH.3.1>SENDAPP</MSH.3.1>
</MSH.3>
<MSH.4>
<MSH.4.1>General Hospital</MSH.4.1>
</MSH.4>
<MSH.5>
<MSH.5.1>RECAPP</MSH.5.1>
</MSH.5>
<MSH.6/>
…
</MSH>
…
</HL7Message>
The XML variables msg and tmp represent root-level elements. Use JavaScript bracket notation for each element level in the document below the root (this works with any message data, not only HL7):
var sendingFacility = msg ['MSH']['MSH.4'][MSH.4.1'].toString();
Most values in an HL7 message go three levels deep. The first level is a segment; the second level is a field within the segment; the third level is a component within a segment field. If a field or component does not exist, it is created automatically. Examples for repeating segments and fields include using bracket notation to index:
var obx = msg['OBX'];
var obx5 = obx['OBX.5'];
var obx5_1 = obx5['OBX.5.1'].toString();
All E4X methods available on the msg and tmp variables can be accessed through the auto-completion dialog in the JavaScript Editor.