jamin on December 4th, 2002

I’m working on an integration interface at work where I’m going to be converting an XML file into ultimately, HL7. However, first I need to build a formatted report with fields filled in from the XML file. Later on I’m going to use the Cloverleaf engine to mutilate the report and end up with HL7. But I thought I’d demonstrate the cool part, using Perl to parse the XML and generate a formatted report.

Suppose we have a bit of XML that looks like this:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE REPORT>
<REPORT>
	<ERRORCODE>0</ERRORCODE>
	<DATABASE>Jamin</DATABASE>
	<LAYOUT></LAYOUT>
	<LAST_NAME>GRAY</LAST_NAME>
	<FIRST_NAME>JAMIN P</FIRST_NAME>
	<DATE>11-27-2002</DATE>
	<ROOM>47</ROOM>
	<BIRTHDATE>01-26-1978</BIRTHDATE>
	<SEX>M</SEX>
	<WEIGHT>150</WEIGHT>
	<HEIGHT>70</HEIGHT>
</REPORT>

We can use a simple script to generate the formatted report:


#!/hci/ActivePerl-5.8/bin/perl -lw
use XML::Simple;
use Data::Dumper;
$debug = 0;
# Parse the XML
$xml = XMLin('/hci/root3.7.1P/dev15/xml/report.xml', suppressempty => '');
print Dumper($xml) if $debug;
# Extract the elements we need for the report
$first_name = $xml->{'FIRST_NAME'};
$last_name  = $xml->{'LAST_NAME'};
$height     = $xml->{'HEIGHT'};
$weight     = $xml->{'WEIGHT'};
$birthdate  = $xml->{'BIRTHDATE'};
$sex        = $xml->{'SEX'};
$room       = $xml->{'ROOM'};
$date       = $xml->{'DATE'};
# Format our report
formline q{
BARNES-JEWISH                10 Hosital Drive             CARDIOPULMONARY LAB
ST. PETERS HOSPITAL      St. Peters, Missouri 63376
                              636-916-9150                STRESS ECHO REPORT
BJC HEALTH SYSTEM
-----------------------------------------------------------------------------
Name: @<<<<<<<<<<<<<<<,@<<<<<<<<<<<<<<<  Date: @<<<<<<<<<<<<<<<  Room#: @<<<<
Birthdate: @<<<<<<<<<<<<<<<              Sex: @<
Weight: @<<<< lb.                        Height: @<<< in.
}, $last_name, $first_name, $date, $room, $birthdate, $sex, $weight, $height;
# Store the report in a variable
$report = $^A;
for (keys %$xml) {
    print "[$_]$xml->{$_}";
}
print $report;

Which gives us output that looks like this:


[HEIGHT]70
[BIRTHDATE]01-26-1978
[DATABASE]Jamin
[DATE]11-27-2002
[LAST_NAME]GRAY
[FIRST_NAME]JAMIN P
[WEIGHT]150
[LAYOUT]
[ROOM]47
[ERRORCODE]0
[SEX]M
BARNES-JEWISH                10 Hosital Drive             CARDIOPULMONARY LAB
ST. PETERS HOSPITAL      St. Peters, Missouri 63376
                              636-916-9150                STRESS ECHO REPORT
BJC HEALTH SYSTEM
-----------------------------------------------------------------------------
Name: GRAY            ,JAMIN P           Date: 11-27-2002        Room#: 47
Birthdate: 01-26-1978                    Sex: M
Weight: 150   lb.                        Height: 70   in.

As you can see I’ve also tagged the document. Cloverleaf will be able to read those tags and I can use them to do a VRL (Virtual Record Layout) to HL7 translation with ease. Incidentally, Cloverleaf does support XML, however I’d have to create a DTD for each report type and it’s not trivial to layout the report in Cloverleaf. Perl makes this so much easier.

Tags:

9 Responses to “Using Perl to generate formatted reports from XML”

  1. Hasn’t HL7 V3.0 with extremely long tag names been standardized yet? (That’d mean the first progressive healthcare software vendor would implement it sometime after 2008.) I remember the output ended up looking something like

    <patient>
    <name>Ben Cochell</name>
    <left_testicle_diameter_in_millimeters>3<left_testicle_diameter_in_millimeters>
    </patient>

    Just outrageously verbose. But the HL7 idiots got one thing right: they focused on tags and limited the use of attributes (if they use them at all). It makes the language much more extensible. The GEDCOM folks (GEDCOM is the defacto standard for genealogical data exchange) have an XML-based schema in the works that looks pretty sweet. Geeky specs are such wonderful spare time reading…

  2. I think it has been standardized, but as you say, no one actually implements it yet. Even HL7 2.4 is spec’d to support XML encoding in place of the usual HL7 encoding where you have Segment, Field, and Subfield delimiters.

    BJC is one of the more progressive Healthcare vendors, but that isn’t saying too much. And they made the unfortunate choice of choosing Cloverleaf as their Interfac e Engine. Maybe there were no better alternatives at the time, I don’t know. I just feel that something could be written in-house that would be more flexible and less buggy. I’ve already started working on my own interface engine which will be pretty simple but flexible. Of course for more than just basic translation it’s not trivial. And reading the HL7 specs is a nightmare.

    Would that everything we did were XML! But we have to support a nightmarish amount of formats and protocols. We have interfaces which read in Postscript and generate HL7, others which accept XML and generate HL7, flat files for charges, VRL’s, HL7 in ASCII and EBCDIC…It’s just crazy.

    I’ve been advocating Perl around the office more and more because of the simplification and speed (believe it or not) it offers over the other methods of message translation we use. We still use the traditional HL7 to HL7 translation using Cloverleaf all the time, but I’ve written modules for Cloverleaf which allow us to use Perl in those translations. And for more complex things, such as the interface I’m working on now where we’re generating formatted reports embedded in HL7 from an XML data file, Perl is ideal.

  3. Healthcare informatics is a nightmare. HL7 is the unholy spawn of Satan and people who read too many executive summaries. ‘nuf said.

    And I don’t doubt Perl is faster. I remember at $FORMER_COMPANY, we wrote our own parsing code for these awful messages. For a while, I advocated something a bit more efficient (OK, maybe lexx and yacc would have been slight overkill). But using existing tools - in your case, Perl for translating messages from format to format - makes sense.

  4. It is good to know that a few others are also using Perl like we do here at SpectraMedi.

    Now, we are working to return transcription files to Cloverleaf. If any of you have done any work in Perl and would like to share with us, please let me know.

    Thanks

  5. how to calculate the total no. of ‘NAME’?

  6. 1. read each line and read and count up ‘NAME’
    2. loop 1. over all lines adding the count.

  7. Hello All,

    Downloaded ActivePerl 5.8.8.8 Build 887 Today. About 10 Minutes ago actually. Incomming Crash Course…..

    My company has decided to upgrade from Access Databases to SQL Databases. 65 Or so Databases. Today is Friday need them done by Monday…..key words no cost we “NEED” to do this for FREE.
    Did I forget to mention I only make 22,000 per year.
    Anyways so…to transfer client demographics from a cingle XML exported Access Table to HL7 (hardly know what it is) I’m going to attempt to modify the above code to do so for me…….. *cry*

    Its now 4:54 pm EST on Friday Evening they’ve come to this decision.

    Wish Me Luck……

  8. Right now, I’m working on a similar issue….one vendor supports XML exports and the new software only takes in ASCII or HL7. Gee, nobody bother to forward the actual specs to the geek in charge until last month, but nevermind…….

    Anyway, I’ve been using (and yes addicted) to using PERL for solving data and text problem since I worked in biotech way back in ‘01. I’m currently the only person who uses perl in house. But if I can pull this off, that may change.

    - CL

  9. Could you please tell me how to take the prinouts after generating reports. i wont display anywhere , directly i need to print on the paper

    Regards
    Srinivas