nHapiNET / nHapi

nHapi is the .Net port of the original Java project HAPI.
Mozilla Public License 2.0
267 stars 155 forks source link

Get property names that have no data while parsing to XML. #478

Open alirizaadiyahsi opened 10 months ago

alirizaadiyahsi commented 10 months ago

I am trying to create a tree by parsing hl7 message to XML.

But XmlParser is checking if the component has DATA, if not then it is not adding that prop to XML tree.

Here is the nhapi code that checks if component has data:

image

Here is the example HL7 message.

MSH|^~\&|FDHL7|JOHNSON LABS||P1055|201007231634||ORU^R01|P1055–0000047907|P|2.3|1||NE|NE PID|1|JQ4988|108512373||SAMPLES^JUNIOR||01/10/1948^53 Y|M|||^******^^|||||||| NTE|1|P|**************************************************************************** ADD|NON FASTING

and here is the output (XML)

<ORU_R01 xmlns="urn:hl7-org:v2xml">
    <MSH>
        <MSH.1>|</MSH.1>
        <MSH.2>^~\&amp;</MSH.2>
        <MSH.3>
            <HD.1>FDHL7</HD.1>
        </MSH.3>
        <MSH.4>
            <HD.1>JOHNSON LABS</HD.1>
        </MSH.4>
        <MSH.6>
            <HD.1>P1055</HD.1>
        </MSH.6>
        <MSH.7>
            <TS.1>201007231634</TS.1>
        </MSH.7>
        <MSH.9>
            <CM_MSG.1>ORU</CM_MSG.1>
            <CM_MSG.2>R01</CM_MSG.2>
        </MSH.9>
        <MSH.10>P1055–0000047907</MSH.10>
        <MSH.11>
            <PT.1>P</PT.1>
        </MSH.11>
        <MSH.12>2.3</MSH.12>
        <MSH.13>1</MSH.13>
        <MSH.15>NE</MSH.15>
        <MSH.16>NE</MSH.16>
    </MSH>

For example, as you see MSH.5, MSH.8, MSH.14 are missing.

Here is the output (tree)

image

And here is my code:

  public IEnumerable<Dictionary<string, object>> Deserialize(string text)
  {
      var parser = new PipeParser();
      var iMessage = parser.Parse(text);

      var xmlParser = new DefaultXMLParser();
      var xmlMessage = xmlParser.Encode(iMessage);

      .....       

      return nodes;
  }

What about adding a parser option to add properties that have no data? Or is there a way/workaround to do this?

alirizaadiyahsi commented 10 months ago

And also we need to add XML tag if there is no child element. For example here:

image

If there is no child element, the line in for-loop is not calling:

image

milkshakeuk commented 10 months ago

Hi, you might want to read the specification on Hl7 v2 as xml http://www.hl7.eu/refactored/encoding02xml.html

10.5 Fields (2.5) ... In the traditional sequence oriented approach, empty fields (containing no data) are denoted as two vertical bars �||� in sequence to express the empty contents. This is essential in sequence-oriented approaches. In v2.xml an element with no contents simply can be omitted (unless explicit use of the "" is required to force a data delete action by the receiving application, see section �2.7.6. Delete Indicators, Empty Values�). In the example above there is no information for EVN.5, thus the element is omitted in the corresponding XML instance.

10.6.2 Composite Data Types (2.6.2) ... Also, empty components may be omitted in the v2.xml encoding, whereas empty components in the traditional encoding must specify an empty component by two component delimiters �^^�in sequence in order to preserve sequence.

10.7.6 Delete Indicators, Empty Values (2.7.6) Where a sending system can ascertain that a data field has been deleted, then the two double quote marks ("") will be used to define the state of that data field. An encoded field with a value of two double quote marks ("") would instruct the receiving system to delete the contents in the database field.

If the state of a blank or null data field cannot be determined, the sending system will send the empty value or omit the element at all. An encoded field with an empty value or a missing element would instruct the receiving system to bypass processing and does not affect an already existing value in the corresponding receiving database.

The occurrence of an empty element is treated as not existing to keep backward compatibility with ER7.

its not clear if encoding empty xml nodes would confuse receiving systems.