buglabs/node-xml2json

xml:space="preserve"

mjbogusz opened this issue · 5 comments

Following code should work but it doesn't.

const xmlParser = require('xml2json');
const assert = require('assert');

const xml = '<test xml:space="preserve">   </test>'; // Three spaces
const jsObject = xmlParser.toJson(xml, { object: true });
assert.equal(jsObject.test, '   '); // Should be three spaces too

Instead, the returned object (jsObject) looks like this:

{ test: { 'xml:space': 'preserve', '$t': '' } }

xml2json: v0.10.0,
NodeJS: v6.4.0

Try passing trim: false in the options.

But I do want other elements to be trimmed - as far as I understand it, the point of this property is more or less to disable trimming in one element (and it's children).

And as I know that adding support for this might require quite complex code changes, for a temporary fix I'd suggest ignoring this property (treating <test xml:space="preserve"> </test> just like <test> </test>).

Moreover, passing trim: false yields following, also unsatisfying, result:

{ test: { 'xml:space': 'preserve', '$t': '   ' } }

@akkenoth that's just not within the scope of this module. It translates to JSON every element, attribute and element value it finds in your xml.

Even if it's part of the specification? If so, then this issue is indeed invalid.

Though I reported this issue (or possibly more like a feature request?) after already doing a simple workaround in my code more to simply notify about existence of such a quirk.

@akkenoth I appreciate for letting us know about this detail. I spent a little bit more of time and you are right. I'm happy to review and merge any patches if you want to contribute them.