0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Then maybe the docs I received are incorrect. If the attributes need to be unique then all is good. Rgds

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.