Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.88k stars 604 forks source link

Bug - Parsing empty tag with namespace doesn't return empty value #647

Open jonikorhonen opened 2 years ago

jonikorhonen commented 2 years ago

Running

const util = require('util');
const xml2js = require('xml2js');

const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<test xmlns="http://example.com/namespace">
    <value1>value1</value1>
    <value2></value2>
    <value3 xmlns="http://example.com/namespace">value3</value3>
    <value4 xmlns="http://example.com/namespace"></value4>
</test>
`;

(async () => {
    const result = await new xml2js.Parser({
        emptyTag: '--EMPTY--',
        explicitArray: false
    }).parseStringPromise(xml);

    console.log(util.inspect(result, false, null));
})();

prints following

{
  test: {
    '$': { xmlns: 'http://example.com/namespace' },
    value1: 'value1',
    value2: '--EMPTY--',
    value3: { _: 'value3', '$': { xmlns: 'http://example.com/namespace' } },
    value4: { '$': { xmlns: 'http://example.com/namespace' } }
  }
}

expected result for value4

 { _: '--EMPTY--', '$': { xmlns: 'http://example.com/namespace' } }