This topic contains 3 replies, has 4 voices, and was last updated by  Julian Muir 1 year, 9 months ago.

Can I add a composite XML element?

  • Is there a simple way to add a composite XML element to my XML structure?
    For example, I am using an HL7 to populate an XML and I want to add data from multiple AL1 segments.
    I have a corresponding Allergies parent element that is optional and I want it to repeat corresponding to the number of AL1 segments.
    So is there a way I can append a blank Allergies element with all the children, populate it, add another, populate it…and so on. Right now the only way I see is to use the node.append() to add the parent and all subsequent children as I populate the data from the HL7 message. And repeat for all instances of my incoming AL1 segment.
    I would prefer something similar to the append function that added an entire substructure.
    As I don’t want blank XML complex elements in my final message I would prefer not to try and create a single, large XML template with all possible elements. I just want to add the ones I have data for as I receive the HL7 message.

    I thought I would ask if anyone has done this before I build my own functions.

    Thanks!

    Being able to insert a compound element/hierarchy/document would indeed be a nice feature, but alas it is not supported . . .

    You’ll need to build your structure piecemeal. Don’t fret, though; you can make it fairly painless with a little forethought.

    Let’s assume a message format like this:

    MSH|^~\&||001||EXBH|20071004003250||ADT^A04|71286|P|2.2|||AL|NE
    EVN|A04|20071004003226|||ABOU
    PID|0001||00308018^^^BH||DOE^JANE^""^""^""||19260125|F||1W|1313 MOCKINGBIRD LANE^""^SPRINGFIELD^CT^06053^USA^""^""|""|(860)555-1212|""||W|CH|3833541^^^|123-45-6789||""^^^""|""|""||||||""|N
    NK1|0001|DOE,JANET^""^""|03|""^""^""^""^""^USA^""^000|(860)555-1212||NR||||||||""|00000000
    NK1|0002|DOE,JANE^""^""|03|""^""^""^""^""^USA^""^000|(860)555-1212||EC||||||||""|00000000
    PV1|0001|O|R5E^502^B^""|""|""||00730^MACANNUCO, ANNETTE|""^""|""^""~""^""~""^""~""^""~""^""~""^""~""^""~""^""~""^""~""^""|ANE|||""|1||""|""^""|1|001|M||||||||||||||||""||""||||||200710041545|""|0000000.00|0000000.00|0000000.00|0000000.00
    PV2||""|^PM/FOLLOW UP|||||""||||||||||||||N
    IN1|0001|01|765|HEALTH NET - MEDICARE MC|1183 NEW HAVEN RD^""^NAUGATUCK^CT^06770-0000^USA|||RUBY 1|HLTH NET-MEDIMC|""|""|""|""|""^^""||DOE^JANE^""|01|19260125|1313 MOCKINGBIRD LANE^""^SPRINGFIELD^CT^06053^USA^""^""|Y|Y|""||||""|Y|Y|20071001||||""||76501|HN495883401||||||""|F|""^""^""^""^""^USA^^""
    IN2|""|041205082|""^""|||""|||||||||||||||N||||||""|SPR^PC^100~ICU^PC^100|||||||||||||||||||||||||||||||||||""
    ACC|""|""|""
    AL1|1|AL|Albumen|H
    AL1|2|RW|Ragweed|M
    AL1|3|PN|Peanut|H

    This code . . .

    function main(Data)
       local p = xml.parse{data='<Patient></Patient>'}
       local msg = hl7.parse{data=Data,vmd="HL7Demo.vmd"}
       local pat = p.Patient
       addDemo(msg.PID,pat)
       addVisit(msg.PV1,pat)
       addAllergies(msg.AL1,pat)
       queue.push{data=p:S()} -- might as well do something with it
    end
    
    function addDemo(pid,doc)
       local d = doc:append(xml.ELEMENT,"Demo")
       addElement(d,"LastName",pid[5][1]:S())
       addElement(d,"FirstName",pid[5][2]:S())
       addElement(d,"BirthDate",pid[7]:S())
       addElement(d,"Gender",pid[8]:S())
       addElement(d,"Race",pid[10]:S())
       addElement(d,"Addr1",pid[11][1][1]:S())
       addElement(d,"Addr2",pid[11][1][2]:S())
       addElement(d,"City",pid[11][1][3]:S())
       addElement(d,"State",pid[11][1][4]:S())
       addElement(d,"Zip",pid[11][1][5]:S())
    end
    
    function addVisit(pv1,doc)
       local v = doc:append(xml.ELEMENT,"Visit")
       addElement(v,"Unit",pv1[3][1]:S())
       addElement(v,"Room",pv1[3][2]:S())
       addElement(v,"Bed",pv1[3][3]:S())
       addElement(v,"Admitted",pv1[44]:S())
       addElement(v,"AdmitType",pv1[4]:S())
       addElement(v,"PatientType",pv1[18]:S())
       addElement(v,"AdmitSource",pv1[14]:S())
    end
    
    function addAllergies(al1,doc)
       if #al1 > 0 then
          local l = doc:append(xml.ELEMENT,"Allergies")
          for i=1,#al1 do
             local r = l:append(xml.ELEMENT,"Allergy")
             addElement(r,"Type",al1[i][2]:S())
             addElement(r,"Description",al1[i][3][1]:S())
             addElement(r,"Severity",al1[i][4]:S())
          end
       end
    end
    
    function addElement(node,tag,value)
       local e = node
       local n = e:append(xml.ELEMENT,tag)
       local v = n:append(xml.TEXT,value)
    end

    will produce this:

    <Patient>
       <Demo>
          <LastName>DOE</LastName>
          <FirstName>JANE</FirstName>
          <BirthDate>19260125</BirthDate>
          <Gender>F</Gender>
          <Race>1W</Race>
          <Addr1>1313 MOCKINGBIRD LANE</Addr1>
          <Addr2>""</Addr2>
          <City>SPRINGFIELD</City>
          <State>CT</State>
          <Zip>06053</Zip>
       </Demo>
       <Visit>
          <Unit>R5E</Unit>
          <Room>502</Room>
          <Bed>B</Bed>
          <Admitted>200710041545</Admitted>
          <AdmitType>""</AdmitType>
          <PatientType>1</PatientType>
          <AdmitSource>1</AdmitSource>
       </Visit>
       <Allergies>
          <Allergy>
             <Type>AL</Type>
             <Description>Albumen</Description>
             <Severity>H</Severity>
          </Allergy>
          <Allergy>
             <Type>RW</Type>
             <Description>Ragweed</Description>
             <Severity>M</Severity>
          </Allergy>
          <Allergy>
             <Type>PN</Type>
             <Description>Peanut</Description>
             <Severity>H</Severity>
          </Allergy>
       </Allergies>
    </Patient>
    

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊ http://www.hicgrp.com

    I think it can be a bit easier. Use setInner().

    Really simple example:-

    function main(Data)
       x = xml.parse{data='<Allergies/>'}
       addAllergy(x.Allergies, 'AL', 'Albumen' , 'H')
       addAllergy(x.Allergies, 'AS', 'Asprin' , 'L')
       return x
    end
    
    function addAllergy(al, t, d, s)
       a = al:append(xml.ELEMENT, 'Allergy')
       a:setInner('<Type>' .. t .. [[</Type>
             <Description>]] .. d .. [[</Description>
             <Severity>]] .. s .. '</Severity>')
       return a
    end

    Produces:-

     <Allergies>
        <Allergy>
          <Type>AL</Type>
          <Description>Albumen</Description>
          <Severity>H</Severity>
       </Allergy>
       <Allergy>
          <Type>AS</Type>
          <Description>Asprin</Description>
          <Severity>L</Severity>
       </Allergy>
    </Allergies>`

    You may be interested in the following article Working with XML that explains to to add add and update XML fields, and how to avoid some issues that can occur.

You must be logged in to reply to this topic.