The validation provided by the Simple Validation Profile is very basic, and it is still possible for an invalid message to pass the requirements of this script. Validation comes in very useful, among other situations, when you wish to place further requirements on the messages, or use HL7Connect to broadcast/receive messages from (often legacy) systems that are not HL7 enabled.

Problem Description

The situation we will look at is a simple one. Suppose your facility has a requirement that the text content of any OBX-3 values (if they have text) must be TX or ST. Using scripts, it is very easy to check this.

Before we begin writing scripts to do this, we should understand the OBX segment a little better. The OBX segment has 4 required fields, OBX-2, OBX-3, OBX-4 and OBX-11. To make things simpler for ourselves, we will not validate the message with the Simple Validation Profile, so we don't need to worry about this. OBX-3 is a coded element containing 6 strings. For the purpose of our testing, we can then just add one of the following lines to the message validate7.hl7 (in ER7 format):

This line will validate successfully:


This line won't validate successfully:


ER7 Message Window -- With OBX Segment

XML Message Window -- With OBX Segment


In addition to the VBScript and Javascript languages, we are now also going to consider the XML Transform, or XSLT script. XSL Transforms, using XML syntax, are designed to transfrorm XML documents into other documents.

Both the VBscript and javascript versions test this validation in the same way. First they find out how many OBX segments there are. Then, for each of them, the script checks to see whether each OBX-3 value, if they exist, are equal to 'TX' or 'ST'. An error/exception is raised if they are not. The validation functions must be called validateMessage to run.

The XSLT script is slightly different. As the language is a functional language, the template is pattern matched onto any OBX-3 component. A terminating message gives us our error if the message does not satisfy the conditions.

First, create a new script window for the type of script you wish to use, then cut/paste the following code into the editor window and save the script (as validate7.vbs, validate7.js OR validate7.xslt as appropriate).

Language    Script
Function validateMessage(aEvent)

  aEvent.Msg.AddStructName = false

  OBXNumber = 0

  For i=0 To (aEvent.Msg.SegmentCount - 1)
    If (aEvent.Msg.segmentbyindex(i).code = "OBX") Then
      OBXNumber = OBXNumber + 1
    End If

  For i=0 To (OBXNumber - 1)
    set seg = aEvent.Msg.Segment("OBX",i)

    For j=1 To 6
      elem = seg.Element("3." & j).AsString
      Kernel.WriteToLog DBG_WARNING, elem
      If elem <> "TX" And elem <> "ST" And elem <> "" Then
        Err.Raise 1, "Validation Failed", "OBX-3 Segment does not contain only \'TX\' or \'ST\' values"
      End If

End function
function validateMessage(aEvent)
  var OBXNumber = 0;
  var i;
  var j;
  var seg;
  var elem;
  aEvent.Msg.AddStructName = false;

  for (i=0; i<aEvent.Msg.SegmentCount; i++)
    if (aEvent.Msg.segmentbyindex(i).code=="OBX")

  for (i=0; i<OBXNumber; i++)
    seg = aEvent.Msg.Segment("OBX",i);
    for (j=1; j<=6; j++)
      elem = seg.Element("3." + String(j)).AsString;
      if (elem != 'TX' && elem != 'ST' && elem != '')
        throw "OBX-3 Segment does not contain only \'TX\' or \'ST\' values.";
XML Transform   
      <xsl:template match="hl7:OBX.3/*">
          <xsl:when test=".='ST'" />
          <xsl:when test=".='TX'" />
            <xsl:message terminate="yes">
              <xsl:text>Invalid OBX-3 entry &lt; </xsl:text>
              <xsl:value-of select="name()"/>/<xsl:value-of select="."/>
              <xsl:text> &gt;.  </xsl:text>
              <xsl:text>Must be TX or ST</xsl:text>

Testing of Validation

To test the validation script take the following steps:

  1. Go back to the validate7.hl7 window and go to the Validation tab.
  2. Click on Add Script and select your saved script.
  3. Now, click the checkbox next to the loaded script and click Validate.

Depending on which line (at the top of this page) you inserted into the message, the message will either validate successfully or it will not. Try it with the alternate line to make sure it gives the expected result.

© Kestral Computing P/L 2000-2010.