简体   繁体   English

Node.js & xml2js - 将多个相同类型的属性渲染到元素中不起作用

[英]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.我正在使用 node.js 和 xml2js 创建 xml 文件。 I'm not able to add to similar attributes to a tag thoug.我无法将类似的属性添加到标签 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: 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):尝试 1(仅呈现最后一个):

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

Attempt 2:尝试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.根据规范,它是无效的 XML。 The attribute name must be unique.属性名称必须是唯一的。

Unless the library you're using doesn't implement exactly what the spec requires.除非您使用的库没有完全实现规范的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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