6

I'm a little bit stuck trying to attach multiple namespaces to an XML element via javascript across browsers; I've tried about a dozen different ways to no avail.

I usually use plain old javascript but for the sake of keeping this example short, this is how what I'm doing would be done via 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");

In both Chrome and FF, this works as expected giving a result like this:

<soapenv:Envelope 
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

But in IE9, I get a result like this:

<soapenv:Envelope 
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
   xmlns:NS1="" NS1:xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>

And I cannot find a way to add this namespace attribute without IE9 adding this NS1 prefix to my namespaces. Also if I try passing this result back into $.parseXML(result) I get a malformed XML exception.

Am I misunderstanding something to do with the way namespaces are declared in IE or can anyone suggest a way I can get a consistent result across browsers?

Thanks in advance

1 Answer 1

4

In case anyone else runs into a similar problem to this, I ended up finding out that it can be fixed by initialising the IE XML DOM object differently to the way jQuery does it. I used something similar to the following and now the xml namespaces seem to be working fine across all major browsers and the jQuery attr method will now work again also.

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");
Sign up to request clarification or add additional context in comments.

4 Comments

+1. Anyway, you should modify your browser test. Currently you prefer the proprietary approach, but IE 9+ supports window.DOMParser, which is what jQuery defaults to internally. Don't do if ( $.browser.msie ), do if ( !window.DOMParser ). Also, here is a nice wrap-up about creating namespace declarations in MSXML: msmvps.com/blogs/martin_honnen/archive/2009/04/15/…
Without jQuery: var objXmlDomParser = new DOMParser(); xmlDOM = objXmlDomParser.parseFromString(soapEnvelope, "text/xml"); xmlDOM.documentElement.setAttribute("xmlns:xsd", "w3.org/2001/XMLSchema");
And old IE: xmlDOM.setProperty("SelectionNamespaces", 'xmlns:xsd="w3.org/2001/XMLSchema"');
@Tomalak Even with IE11, using jQuery default parser produced the issue mentioned by 2potatocakes. The best way I found without using custom browser code was to set all my namespaces before calling parseXML.

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.