Announcement

Collapse
No announcement yet.

How to Count OBX's in an OBR?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to Count OBX's in an OBR?

    Is there an easy way to get all OBX messages under a particular OBR? Example is OBR1 has OBX1, OBX2, OBX3 and OBR2 has OBX1, OBX2. How can i get a count of OBX's (1,2,3) in OBR1 and OBX's (1,2) in OBR2?

    Any help or information anyone can provide is much appreciated.

    Thanks in advance.

  • #2
    In Mirth you use JavaScript and E4X to parse and manipulate messages which are internally represented as XML.

    A nice introduction is "Processing XML with E4X" [Mozilla developer center]

    Code:
    var temp = <bla><obr>
    <obx>11</obx><obx>12</obx><obx>13</obx>
    </obr>
    <obr>
    <obx>21</obx><obx>22</obx>
    </obr>
    </bla>;
    
    logger.error('obr length: ' + temp.obr.length()); //2
    logger.error('number of obx in first obr: ' + temp.obr[0].obx.length()); //3

    Comment


    • #3
      Originally posted by Johan_Karlsson View Post
      In Mirth you use JavaScript and E4X to parse and manipulate messages which are internally represented as XML.

      A nice introduction is "Processing XML with E4X" [Mozilla developer center]

      Code:
      var temp = <bla><obr>
      <obx>11</obx><obx>12</obx><obx>13</obx>
      </obr>
      <obr>
      <obx>21</obx><obx>22</obx>
      </obr>
      </bla>;
      
      logger.error('obr length: ' + temp.obr.length()); //2
      logger.error('number of obx in first obr: ' + temp.obr[0].obx.length()); //3
      Actually, while the code presented there would work given the XML listed, that XML is not an accurate representation of the transformed version of the message in Mirth Connect. Because HL7 messages are essentially "flat", all segments are at the same level in the XML tree. In other words, OBX segments that belong to a particular OBR are *not* child elements--they are at the same level as the OBR.

      Hence, the best way to accomplish what you want is to loop over all segments in your message, keeping track of the segments that you've seen as you go. For a template of the code to loop over segments, drag over the "Iterate over all segments" code template from the Reference tab in a JavaScript transformer step.

      Comment


      • #4
        e4x has some cool ways of getting lists of xml objects:
        Code:
        // get list of all obx segments
        var obxList = msg..*::OBX;
        logger.info("total number of OBX segments: " + obxList.length());
        
        // get number of obx segments in a specific OBR
        var obr = msg['OBR'][0];
        var obxList = obr.*::OBX;
        logger.info("number of OBX segments in OBR: " + obxList.length());
        Daniel Svanstedt
        Software Engineer
        Mirth Corporation

        Want professional services, support, and enterprise or virtual appliances? It's all available from the Mirth Corporation:
        Mirth Support | Mirth Training | Mirth Appliances | Online Training | Developer Q&A

        Don't forget, Mirth Support gives you access to all of our online training videos, and silver support gives you access to developer Q&As!

        Comment


        • #5
          Originally posted by dans View Post
          e4x has some cool ways of getting lists of xml objects:
          Code:
          // get list of all obx segments
          var obxList = msg..*::OBX;
          logger.info("total number of OBX segments: " + obxList.length());
          
          // get number of obx segments in a specific OBR
          var obr = msg['OBR'][0];
          var obxList = obr.*::OBX;
          logger.info("number of OBX segments in OBR: " + obxList.length());
          Please correct me if I am wrong, Dan, but I don't think that works. Particularly these two lines:

          var obr = msg['OBR'][0];
          var obxList = obr.*::OBX;

          First, you're getting a particular OBR segment. Then, you're using the qualified identifiers operator (: to get the elements within that OBR named OBX. But there are no OBXs within the OBR, because the OBX is not a child of the OBR; it is its sibling. Am I missing something? I even tested the code out with a message with multiple OBRs and OBXs, and got "0" as the result for the last logger line.

          Comment


          • #6
            You are right. They are siblings. Best way would probably to iterate over all segments.

            You could probably use childIndex as well:

            Code:
            for(var i=0;i<msg['OBR'].length();i++){
               var obxCount = 0;
               var index = msg['OBR'][i].childIndex();
               while(msg.children()[index+1] != null && msg.children()[index+1].name()=="OBX"){
                  obxCount++;      
                  index++;
               }
               logger.error("obr "+i+" OBX count is " + obxCount);
            }
            Last edited by dans; 10-05-2010, 10:28 AM.
            Daniel Svanstedt
            Software Engineer
            Mirth Corporation

            Want professional services, support, and enterprise or virtual appliances? It's all available from the Mirth Corporation:
            Mirth Support | Mirth Training | Mirth Appliances | Online Training | Developer Q&A

            Don't forget, Mirth Support gives you access to all of our online training videos, and silver support gives you access to developer Q&As!

            Comment


            • #7
              See my code @ http://www.mirthcorp.com/community/f...ad.php?p=15093
              This will take care of OBRs, OBXs and NTEs keeping in mind importance of segment sequencing.

              Comment

              Working...
              X