boolean and non-existent attributes #26

Closed
rlidwka opened this Issue Nov 14, 2012 · 1 comment

Projects

None yet

2 participants

@rlidwka
rlidwka commented Nov 14, 2012

Right now if I pass undefined object as a value, it would output "undefined" string. And to exclude such attributes I would need to filter out the entire attributes object before passing it to xmlbuilder.

Consider this example:

/*
 *  Configuration
 */
var param1 = 123;
// I want to turn off param2, so it doesn't appear in xml. How do I do it here?
var param2;

/*
 *  XML building
 */
var xml = builder.create('root')
  .ele('test', {
    param1: param1,
    param2: param2
  })

Since null and undefined aren't useful anyway, I suggest to exclude them from attributes in the library itself like this:

// expected: <root><test param1="123" /></root>
var xml = builder.create('root').ele('test', { param1: 123, param2: null })

// expected: <root><test param1="123" /></root>
var xml = builder.create('root').ele('test', { param1: 123, param2: undefined })

The same thing is about boolean attributes. If someone pass false, it's unlikely that he wants to get "false" string instead of it. So, I'd like to see this:

// expected: <root><test param1="123" param2="param2" /></root>
var xml = builder.create('root').ele('test', { param1: 123, param2: true })

// expected: <root><test param1="123" /></root>
var xml = builder.create('root').ele('test', { param1: 123, param2: false })
@oozcitak
Owner

Hello and sorry for the late response. I am thinking of adding a flag to control the behavior of null and undefined attributes. But the default behavior will be to throw an exception on such values. This would be consistent with the rest of the lib.

The true behavior you described above seems like XHTML. Since this is a generic XML generator, I don't think we should support specialized XML dialects out of the box.

Cheers,
Ozgur

@oozcitak oozcitak closed this in 509f6d1 Dec 29, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment