Skip to content

3. Element

Angus Millar edited this page May 9, 2019 · 11 revisions

Understanding Element

The concept of Element can be a little foreign. People seldom talk about elements when discussing HL7 V2 messages tending to only refer to Fields. Yet, Element is a defined concept in the HL7 V2 standards and is required in a semantic expression to unequivocally locate data in an HL7 message.

In general conversation, people will say things like this:

"The patient's national identifier can be found in the PID segment, field three, the second repeat."

While this is fine for humans it is not so good for a syntax. The problem with this statement is that it implies that a Field has repeats and that those repeats are Fields, which further implies that those repeated Fields can also have more repeating Fields, and on and on. This is simply not true for HL7 V2 messages.

To resolve this you need to understand the concept of 'Element'. An Element contains many repeating Fields and Fields only contain Components which in turn contain Sub-Components, which in turn contain Content. Let's look at an example.

Below is a very simple Segment

Notice it has three Elements. People would commonly call these Fields; which is true if there is only a single Field. Yet once we have more than single Field, as in the next example, we need to understand these as Elements which can contain many Fields.

Example 1

The example is now expanded by adding four Field repeats into the first Element.

So now we have many Fields in the Element. The Element is the container that holds many Fields. These Fields are often called Field repeats.

Example 2

Now we demonstrate how to access the Field repeats.

Notice in the syntax below there are two different ways to access the first repeat in the Element. The rule for the Field() method is that it always returns the first repeat within an Element. Yet, if the Field repeat you require is not the first repeat then you must reference it via the Element() method followed by the Repeat() method specifying which repeat you want. It's also worth noting here that repeat is a 1 based list, there is no Repeat(0).

Example 3

This example message will be used in the code examples that follow:

MSH|^~\&|GOODLIS|GOODLAB|EMR|GOODHOSP|20150411102500+1000||ORU^R01^ORU_R01|300000000-1|P|2.4|||AL|NE|
PID|1||1000003^^^GOOHOSP^MR~89898989^^^NationalId^NI||PatientSurname^PatientFirstname||194506241031|M|||Unit 1^111 Good Street^Brisbane^^4000^AUS^H||^PRN^PH^^^^93235615|^WPN^CP^^^^0414778341
PV1|1|O
ORC|RE|300001|15100003-330001|3|CM||||201504100800+1000|||DFTR^DrBSurname^DrOrdering^^^Dr^^^GOODLIS||^WPN^PH^^^^0893412041|201504100730+1000
OBR|1|300001|15100003-330001|^^^FBE^Full Blood Count^GOODLIS|||201504100930+1000|||||||201504101100+1000||DFTR^DrBSurname^DrOrdering^^^Dr^^^GOODLIS|^WPN^PH^^^^0893412041|||||201504101115+1000||HM|F||^^^201504100800+1000^^RT|||||8003615833340784&Howser&Kara&&&Dr&&&AUSHIC
OBX|1|NM|^^^HB^Haemoglobin^GOODLAB||145|g/L^^ISO+|130-180||||F
OBX|2|NM|^^^RCC^Red Cell Count^GOODLAB||5.30|x10\S\12/L^^ISO+|4.50-6.50||||F
OBX|3|NM|^^^HCT^HCT^GOODLAB||0.43|L/L^^ISO+|0.40-0.54||||F

Create a new Element

  //Remember to include the following using statement at the top of your source file.
  using PeterPiper.Hl7.V2.Model;

  //Create a new empty element instance and then set its content
  var oNewElement = Creator.Element();
  oNewElement.AsString = "This is an element";

  //Or, provide the content as you create
  oNewElement = Creator.Element("This is an element");      

  //Or, create with some not so basic content
  oNewElement = Creator.Element("One~Two~ThreeOne^ThreeTwo~Four);

Access parts of an Element

//Parse a whole message into the object model from a string 
string MessageExample = "<Example HL7 V2 Message>"
var oHL7 = Creator.Message(MessageExample);

//Setup a variable to store the patient's Medical Record Number
string PatientMrnValue = string.Empty;

//The PatientMrnValue variable will equal '1000003'
PatientMrnValue = oHL7.Segment("PID").Element(3).Repeat(1).Component(1).AsString;

//Or, as this is the first repeat of the element, we can use the Field shortcut
PatientMrnValue = oHL7.Segment("PID").Field(3).Component(1).AsString; 

//Setup a variable to store the patient's National Identifer
string PatientMrnValue = string.Empty;

//The PatientNationalIdentiferValue variable will equal '89898989'
//Here we cannot use the Field shortcut as it always defaults to the first field in the element.
PatientNationalIdentiferValue = oHL7.Segment("PID").Element(3).Repeat(2).Component(1).AsString;