Search code examples
javascripte4x

Is there a E4X method I can use to add an attribute to a XML element?


In E4X I can add an element to another element either via an E4X operator or method:

// operator
var errorXml = <error></error>;
reportXml.errors.error += errorXml;

// method
var errorXml = new XML('<error></error>');
reportXml.errors.appendChild(errorXml);

I know I can add an attribute to my errorXml element using the @ operator:

errorXml.@description = 'uh oh!';

But how can I achieve the equivalent to that using an E4X method? Is it even possible? I'm after this because I'm attempting to write JavaScript that will successfully pass JSHint linting.


Solution

  • Looking through http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-357.pdf I couldn't find a method that would suit.

    In your example you are just setting the description attribute. A way that would pass linting for that specific example would be:

    var desc = 'uh-oh';   
    var errorXml = new XML('<error description=\"' + desc + '\"></error>');