Leonidas-from-XIV / node-xml2js

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

Render multiple attributes of same kind in element #534

Open markusrempfer opened 4 years ago

markusrempfer commented 4 years ago

I'm using node.js and xml2js to create a xml file. I'm not able to add to similar attributes to a tag thoug. So sth like this:

  <?xml version="1.0" encoding="UTF-8"?>
    <data xmlns:xsi="url" xmlns="abc" xmlns="xyz" xsi:schema="123">     

What I tried is:

js :

  var obj = {
    'data': {
      /*'$': {
        'xmlns:xsi': 'url',
        'xmlns': 'abc',
        'xmlns': 'xyz',
        'xsi:schema': '123'
      },*/
      '$': {
        'xmlns:xsi': 'url',
        'xmlns': [
          'abc',
          'xyz'
        ],
        'xsi:schema': '123'
      }
      ...
    }
  };

  var builder = new xml2js.Builder({ xmldec: {'version': '1.0', 'encoding': 'UTF-8'} });
  var xml = builder.buildObject(obj);

 console.log(xml);

... which results in:

Attempt 1:

  <?xml version="1.0" encoding="UTF-8"?>
    <data xmlns:xsi="url" xmlns="xyz" xsi:schema="123"> 

Attempt 2:

  <?xml version="1.0" encoding="UTF-8"?>
    <data xmlns:xsi="url" xmlns="abc,xyz" xsi:schema="123"> 

But I need this:

Goal:

  <?xml version="1.0" encoding="UTF-8"?>
    <data xmlns:xsi="url" xmlns="abc" xmlns="xyz" xsi:schema="123">         

How can I render two similar attributes in the same element?