简体   繁体   中英

Node.js & xml2js - Render multiple attributes of same kind into element not working

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 (only the last one is rendered):

  <?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?

I don't think you can do that. It's invalid XML according to the spec. The attribute name must be unique.

Unless the library you're using doesn't implement exactly what the spec requires.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM