Skip to content

stopNodes gets thrown off if the same tag is present inside the stopped node #455

@patrickshipe

Description

@patrickshipe
  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?

Description

When using stopNodes, if the content of the stopped node includes the same tag as the stop node itself, the stopped node will be incorrectly stopped early.

Input

Code

Input XML:

<ClinicalDocument>
  <component>
    <structuredBody>
      <component>
        <section>
          <title>
                        DISCHARGE SUMMARY NOTE
          </title>
          <text>
            <list>
              <item>
                <paragraph>
                  <text ID="noKnownAllergy1">
                                            No known allergies
                  </text>
                  <statusCode nullFlavor="NI"/>
                  <effectiveTime xsi:type="IVL_TS">
                    <low nullFlavor="NI"/>
                  </effectiveTime>
                  <br/>
                </paragraph>
              </item>
              <item>
                <item>
                  <paragraph>
                    <b>
                                            Referrals:
                    </b>
                    <table>
                      <thead>
                        <tr>
                          <th>
                                                        First Name
                          </th>
                          <th>
                                                        Last Name
                          </th>
                          <th>
                                                        NPI#
                          </th>
                          <th>
                                                        Reason
                          </th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr>
                          <td>
                                                        None recorded
                          </td>
                          <td>
                                                        None recorded
                          </td>
                          <td>
                                                        None recorded
                          </td>
                          <td>
                                                        None recorded
                          </td>
                        </tr>
                      </tbody>
                    </table>
                    <br/>

                  </paragraph>
                </item>
              </list>
            </text>
            <entry>
              <act classCode="ACT" moodCode="EVN">
                <text>
                                DISCHARGE SUMMARY NOTE
                </text>
                <statusCode code="completed"/>
              </act>
            </entry>
          </section>
        </component>
      </structuredBody>
    </component>
  </ClinicalDocument>

Code:

import { XMLParser } from 'fast-xml-parser';
import { readFileSync, writeFileSync } from 'fs';

const parser = new XMLParser({
  allowBooleanAttributes: true,
  alwaysCreateTextNode: true,
  ignoreAttributes: false,
  parseAttributeValue: false,
  removeNSPrefix: true,
  isArray: (_, __, ___, isAttr) => !isAttr,
  parseTagValue: false,
  stopNodes: ['ClinicalDocument.component.structuredBody.component.section.text'],
});

const xml = readFileSync('./input/doc1.xml').toString();

const output = parser.parse(xml);

writeFileSync('./output/doc1.json', JSON.stringify(output, null, 2));

Output

Notice that since there is a <text> element inside the stopped node, we incorrectly break out of that and parse the rest of the document wrong.

{
  "ClinicalDocument": [
    {
      "component": [
        {
          "structuredBody": [
            {
              "component": [
                {
                  "section": [
                    {
                      "title": [
                        {
                          "#text": "DISCHARGE SUMMARY NOTE"
                        }
                      ],
                      "text": [
                        {
                          "#text": "\n            <list>\n              <item>\n                <paragraph>\n                  <text ID=\"noKnownAllergy1\">\n                                            No known allergies\n                  "
                        }
                      ],
                      "statusCode": [
                        {
                          "@_nullFlavor": "NI"
                        }
                      ],
                      "effectiveTime": [
                        {
                          "low": [
                            {
                              "@_nullFlavor": "NI"
                            }
                          ],
                          "@_type": "IVL_TS"
                        }
                      ],
                      "br": [
                        {
                          "#text": ""
                        }
                      ]
                    }
                  ]
                }
              ],
              "item": [
                {
                  "item": [
                    {
                      "paragraph": [
                        {
                          "b": [
                            {
                              "#text": "Referrals:"
                            }
                          ],
                          "table": [
                            {
                              "thead": [
                                {
                                  "tr": [
                                    {
                                      "th": [
                                        {
                                          "#text": "First Name"
                                        },
                                        {
                                          "#text": "Last Name"
                                        },
                                        {
                                          "#text": "NPI#"
                                        },
                                        {
                                          "#text": "Reason"
                                        }
                                      ]
                                    }
                                  ]
                                }
                              ],
                              "tbody": [
                                {
                                  "tr": [
                                    {
                                      "td": [
                                        {
                                          "#text": "None recorded"
                                        },
                                        {
                                          "#text": "None recorded"
                                        },
                                        {
                                          "#text": "None recorded"
                                        },
                                        {
                                          "#text": "None recorded"
                                        }
                                      ]
                                    }
                                  ]
                                }
                              ]
                            }
                          ],
                          "br": [
                            {
                              "#text": ""
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ],
          "entry": [
            {
              "act": [
                {
                  "text": [
                    {
                      "#text": "DISCHARGE SUMMARY NOTE"
                    }
                  ],
                  "statusCode": [
                    {
                      "@_code": "completed"
                    }
                  ],
                  "@_classCode": "ACT",
                  "@_moodCode": "EVN"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

expected data

Expected should properly keep all children of the stopped node as raw text:

{
  "ClinicalDocument": [
    {
      "component": [
        {
          "structuredBody": [
            {
              "component": [
                {
                  "section": [
                    {
                      "title": [
                        {
                          "#text": "DISCHARGE SUMMARY NOTE"
                        }
                      ],
                      "text": [
                        {
                          "#text": "\n            <list>\n              <item>\n                <paragraph>\n                  <text ID=\"noKnownAllergy1\">\n                                            No known allergies\n                  </text>\n                  <statusCode nullFlavor=\"NI\"/>\n                  <effectiveTime xsi:type=\"IVL_TS\">\n                    <low nullFlavor=\"NI\"/>\n                  </effectiveTime>\n                  <br/>\n                </paragraph>\n              </item>\n              <item>\n                <item>\n                  <paragraph>\n                    <b>\n                                            Referrals:\n                    </b>\n                    <table>\n                      <thead>\n                        <tr>\n                          <th>\n                                                        First Name\n                          </th>\n                          <th>\n                                                        Last Name\n                          </th>\n                          <th>\n                                                        NPI#\n                          </th>\n                          <th>\n                                                        Reason\n                          </th>\n                        </tr>\n                      </thead>\n                      <tbody>\n                        <tr>\n                          <td>\n                                                        None recorded\n                          </td>\n                          <td>\n                                                        None recorded\n                          </td>\n                          <td>\n                                                        None recorded\n                          </td>\n                          <td>\n                                                        None recorded\n                          </td>\n                        </tr>\n                      </tbody>\n                    </table>\n                    <br/>\n\n                  </paragraph>\n                </item>\n              </list>\n            "
                        }
                      ],
                      "entry": [
                        {
                          "act": [
                            {
                              "text": [
                                {
                                  "#text": "DISCHARGE SUMMARY NOTE"
                                }
                              ],
                              "statusCode": [
                                {
                                  "@_code": "completed"
                                }
                              ],
                              "@_classCode": "ACT",
                              "@_moodCode": "EVN"
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Would you like to work on this issue?

  • Yes
  • No

Bookmark this repository for further updates.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions