Build a CDA document

Create a new "set" or "add" function

As you can see from the CDA API Guide some of the “add” functions do not have a corresponding “set” function. This is part of our intentional minimalist approach, where we only created the “set” functions we actually needed. However it is very easy to create a new “set” or “add” function if you need one.

So when would you need to do this? How about if you need to process a CDA (rather than building one from scratch) and want to add or modify some elements. Or if you have a custom CDA that uses an element that is not included in our templates (though this should be unlikely as we have included all the standard CDA elements).

We chose to create a new function, because it is a simple example. Also we use two ids in the <patientrole> element of the CDA, so we can demonstrate how to update both of them.

This is the result:

It is very similar to, which we modified:

This is our new function:

All we needed to do was change the target parameter to point to the element to be changed (rather than the parent element), and change the check() to point to the new help table.

Here is the code from the main module that calls to change both of the ids:

Creating a new add function is also simple, just modify a similar add function.

Here is the code so you can try it.

The function from the module (including partially completed help):

local cdaIdSetHelp = {
   usage=[[{target=<{ELEMENT}>, value=<EXTENSION>, id_type=<ROOT>} ]],
   description=[[CHANGE DESCRIPTION]],
   returns={"A parsed tree representing the populated XML element"},
      [1]={['parameter']='target',['description']='The element to be updated'},
      [1]={['parameter']='value',['description']='The extension (or specific value) of the ID'},
      [2]={['parameter']='id_type',['description']='The root identifier (object ID) of the desired ID'},
   examples={[[CHANGE EXAMPLES]]}

   check(I, cdaIdSetHelp) -- modified

   local Id =    -- modified
   if I.id_type then
      if I.value then
         Id:setAttr('extension', I.value)
      Id:setAttr('root', I.id_type)

   return Id
end{, info=cdaIdSetHelp}

The FillPatient() function from the main module:

local function FillPatient(RT)
   local function FillGuardian(G)
      cda.code.add{target=G, element='code',["HL7 Role Class"],
         value=cda.codeset.personalRelationshipRole.Parent, lookup=cda.codeset.personalRelationshipRole.reverse}
      cda.demographic.address.add{target=G, use=cda.codeset.address.Home, 
         street='1357 Amber Drive', city='Beaverton', state='OR', zip='97867', country='US'}{target=G, phone='(816)276-6909', use=cda.codeset.address.Home}
      local GP = addElement(G, 'guardianPerson'){target=GP, given='Ralph', family='Jones'}

      return G

   local function FillBirthPlace(B)
      local P = addElement(B, 'place')
      cda.demographic.address.add{target=P, city='Beaverton', state='OR', zip='97867', country='US'}  

      return B

   local function FillLanguageCommunication(L)
      cda.code.simple.add{target=L, element='languageCode', value=cda.codeset.language['English - US']}
      cda.code.add{target=L, element='modeCode',["LanguageAbilityMode"],
         value=cda.codeset.proficiencyLevel["Good"], lookup=cda.codeset.proficiencyLevel.reverse}
      cda.value.add{target=L, element='preferenceInd', datatype='BL', value='true'}
      return L

   local function FillProviderOrganization(O){target=O,["National Provider Identifier"]}{target=O, name='Community Health and Hospitals'}{target=O, phone='(555)555-5000', use=cda.codeset.address.Work}
      cda.demographic.address.add{target=O, use=cda.codeset.address.Work, 
         street='1001 Village Avenue', city='Beaverton', state='OR', zip='99123', country='US'}  

      return O

   -- original code to add the ids
   local PR = addElement(RT, 'patientRole'){target=PR, value='998991', id_type='2.16.840.1.113883.4.6'} -- dummy OID change or map value{target=PR, value='111-00-2330',}

   -- code to set (change) the ids

   trace(PR) -- inspect the ids before changing
   -- set first id{, value='Use the new', id_type='to change the first id'}
   -- set second id{target=PR:child("id", 2), value='and to ', id_type='change the second id'}
   trace(PR)-- inspect the ids after changing

   cda.demographic.address.add{target=PR, use=cda.codeset.address.Home, street='1357 Amber Drive', 
      city='Beaverton', state='OR', zip='97867', country='US'}{target=PR, phone='(816)276-6909', use=cda.codeset.address.Home}  
   local P = addElement(PR, 'patient'){target=P, given='Isabella', nickname='Isa',
      family='Jones', use=cda.codeset.nameUses.Legal}
   cda.code.add{target=P, element='administrativeGenderCode',["HL7 AdministrativeGender"],,}
   cda.time.add{target=P, element='birthTime', time='19750501000000+0500'}   
   cda.code.add{target=P, element='maritalStatusCode',["HL7 Marital status"],
      value=cda.codeset.marriage.Married, lookup=cda.codeset.marriage.reverse}
   cda.code.add{target=P, element='religiousAffiliationCode',,
      value=cda.codeset.religion.Atheism, lookup=cda.codeset.religion.reverse}
   cda.code.add{target=P, element='raceCode',["HL7 Race and Ethnicity"],
      value=cda.codeset.race.White, lookup=cda.codeset.race.reverse}
   cda.code.add{target=P, element='ethnicGroupCode',["HL7 Race and Ethnicity"], 
      value=cda.codeset.ethnicGroup["Not Hispanic or Latino"], 

   local G = addElement(P, 'guardian')
   local BP = addElement(P, 'birthplace')
   local LC = addElement(P, 'languageCommunication')
   local PO = addElement(PR, 'providerOrganization')

   return RT

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.