简体   繁体   English

Javascript XML序列化(IE问题)

[英]Javascript XML serialization (IE issue)

I use the following code to generate XML, see codepen 我使用以下代码生成XML,请参阅codepen

var doc = document.implementation.createDocument ('http://www.w3.org/1999/xhtml', 'root', null);

var somenamespace = "http://somenamespace.com/schema";
doc.documentElement.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' + "pref", somenamespace);

var el1 = doc.createElementNS(doc.documentElement.namespaceURI, "el1");
doc.documentElement.appendChild(el1);

var el2 = doc.createElementNS(doc.documentElement.namespaceURI, "el2");
el1.appendChild(el2);

el2.setAttributeNS(somenamespace, "pref" + ":" + "foo", "bar");

var serializer = new XMLSerializer();
var raw = serializer.serializeToString(doc);
document.body.innerText = raw;

It works as expected in Chrome, Firefox, Safari, but IE drops my xmlns:prefix where I set it and writes in other XML nodes. 它在Chrome,Firefox,Safari中按预期工作,但IE删除我的xmlns:prefix ,我设置它并在其他XML节点中写入。

Expected: 预期:

<root
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:pref="http://somenamespace.com/schema">
    <el1>
        <el2 pref:foo="bar"></el2>
    </el1>
</root>

IE: IE:

<root
 xmlns="http://www.w3.org/1999/xhtml">
 <el1>
  <el2
   xmlns:pref="http://somenamespace.com/schema" pref:foo="bar">
  </el2>
 </el1>
</root>

What can I do to workaround and make IE write xmlns in the root element (where I set it)? 我可以做些什么来解决并让IE在根元素(我设置它)中写xmlns

What can I do to workaround and make IE write xmlns in the root element (where I set it)? 我可以做些什么来解决并让IE在根元素(我设置它)中写xmlns

Nothing. 没有。

Your expected and actual XML are fully equivalent. 您的预期和实际XML完全等效。 No compliant XML processor will treat the two forms differently. 没有兼容的XML处理器会以不同方式处理这两种形式 You should not care either. 你也不应该在乎。

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

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