3

Namespaces are not being properly preserved by IE 10 and IE 11 when parsing XML with jQuery and converting back to string. Is there another accepted means of doing this in IE 10/11, aside from writing my own stringify code?

Here is the code I am using, which I've also made a fiddle: http://jsfiddle.net/kd2tvb4v/2/

var origXml = 
    '<styleSheet' 
        + ' xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"'
        + ' xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"'
        + ' xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"'
        + ' mc:Ignorable="x14ac">'
            + '<fonts count="0" x14ac:knownFonts="1"></fonts>'
        + '</styleSheet>';
var xml = $($.parseXML(origXml).documentElement);
var reprocessedXml = (new XMLSerializer()).serializeToString(xml[0]);

$('#origXml').text(origXml);
$('#reprocessedXml').text(reprocessedXml);
4
  • Why not use jQuery parseXml method to convert XML to JavaScript object? Commented Mar 1, 2015 at 21:46
  • 1
    Sigismundus: not sure I understand your suggestion. The code above is already using jQuery.parseXML(). This seems to be the accepted method of converting a string to XML; however, in IE 10/11, the above method to convert XML back to string (XMLSerializer.serializeToString()) does not work. Commented Mar 1, 2015 at 22:05
  • jQuery is supposed to be browser independent, so as long as you use it you should not have such issues. To serialize JavaScript object use the JSON.stringify Commented Mar 3, 2015 at 23:04
  • The issue is not with jQuery -- jQuery.parseXML(origXml) works correctly. The issue, from what I can tell, is the browser's Javascript implementation of XMLSerializer(). jQuery does not offer a writeXML() or similar method of which I am aware. I do not understand how JSON.stringify can help -- the request is to requirement, update, and write XML, not JSON, in IE 10/11. Commented Mar 5, 2015 at 23:48

1 Answer 1

2

So, I thought xml[0].outerHTML would do the job. Oddly enough, this works as expected in FF, but xml[0].outerHTML and xml[0].innerHTML are both undefined in IE. Weird!

The classic trick for getting outerHTML when it is not available still seems to work in this case: Append the node to a dummy element and use .html(). This seems to rearrange the ordering of attributes (it alphabetizes them), but everything is preserved:

Tested in IE11, don't have IE10 handy:

//...your original code...
var xml = $($.parseXML(origXml).documentElement);
var rootChildXml=$('<root />').append(xml).html();
console.log(origXML,rootChildXml);

Original XML:

<styleSheet xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" 
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
 mc:Ignorable="x14ac">
<fonts count="0" x14ac:knownFonts="1"></fonts></styleSheet>

rootChildXml:

<stylesheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" 
 mc:Ignorable="x14ac" 
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
<fonts x14ac:knownFonts="1" count="0"></fonts></stylesheet>

fiddle: http://jsfiddle.net/kd2tvb4v/4/

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

2 Comments

So that's almost a solution, except that XML element names are case-sensitive and this solution does not preserve that. At least not in IE 11 -- I did not test in IE 10.
interesting, not sure what to do about that short of writing your own stringifyer

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.