[英]How to add additional xmlns namespace attributes to XML in IE via javascript
我在尝试通过跨浏览器的 javascript 将多个命名空间附加到 XML 元素时有点卡住了; 我尝试了十几种不同的方法都无济于事。
我通常使用普通的旧 javascript,但为了使这个示例简短,这就是我正在做的事情将通过 jQuery 完成的方式:
var soapEnvelope = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"></soapenv:Envelope>';
var jXML = jQuery.parseXML(soapEnvelope);
$(jXML.documentElement).attr("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
在 Chrome 和 FF 中,这都按预期工作,结果如下:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
但是在 IE9 中,我得到这样的结果:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:NS1="" NS1:xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
如果没有 IE9 将此 NS1 前缀添加到我的命名空间,我无法找到添加此命名空间属性的方法。 此外,如果我尝试将此结果传递回 $.parseXML(result) ,则会出现格式错误的 XML 异常。
我是否误解了与在 IE 中声明命名空间的方式有关,或者有人可以建议我可以在浏览器中获得一致结果的方法吗?
提前致谢
如果其他人遇到与此类似的问题,我最终发现可以通过以与 jQuery 不同的方式初始化 IE XML DOM 对象来修复它。 我使用了类似于以下内容的内容,现在 xml 名称空间似乎在所有主要浏览器中都可以正常工作,并且 jQuery attr 方法现在也可以再次工作。
var getIEXMLDOM = function() {
var progIDs = [ 'Msxml2.DOMDocument.6.0', 'Msxml2.DOMDocument.3.0' ];
for (var i = 0; i < progIDs.length; i++) {
try {
var xmlDOM = new ActiveXObject(progIDs[i]);
return xmlDOM;
} catch (ex) { }
}
return null;
}
var xmlDOM;
if ( $.browser.msie ) {
xmlDOM = getIEXMLDOM();
xmlDOM.loadXML(soapEnvelope);
} else {
xmlDOM = jQuery.parseXML(soapEnvelope);
}
$(xmlDOM.documentElement).attr("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.