IE11 Bug: Namespaces serialized incorrectly in XML
Hi there @tyrasd,
Great library, thanks a lot!
Here's an example that works perfectly in Chrome 58 and Firefox 53:
<html>
<head>
<title>XML Serialization</title>
</head>
<body>
<script src="jxon.js"></script>
<script>
var xml = '<?xml version="1.0"?><People xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://MyNS.com"><Person><Id>a012345d-12bc-41fe-a35d-ff713bc876e0</Id><Name>Complete Automatically?</Name><Prompt>Would you like to complete this...</Prompt><Options><Option><Text>Automatically</Text><Value>Automatic Name</Value></Option><Option><Text>Manually</Text><Value>Manual Name</Value></Option></Options><Value>Current Name</Value></Person></People>';
var json = JXON.stringToJs(xml);
console.log(JSON.stringify(json));
var desXml = JXON.jsToString(json);
console.log(desXml);
</script>
</body>
</html>
Output:
{
"People": {
"Person": {
"Id": "a012345d-12bc-41fe-a35d-ff713bc876e0",
"Name": "Complete Automatically?",
"Prompt": "Would you like to complete this...",
"Options": {
"Option": [{
"Text": "Automatically",
"Value": "Automatic Name"
}, {
"Text": "Manually",
"Value": "Manual Name"
}
]
},
"Value": "Current Name"
},
"$xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"$xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"$xmlns": "http://MyNS.com"
}
}
<People xmlns="http://MyNS.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person>
<Id>a012345d-12bc-41fe-a35d-ff713bc876e0</Id>
<Name>Complete Automatically?</Name>
<Prompt>Would you like to complete this...</Prompt>
<Options>
<Option>
<Text>Automatically</Text>
<Value>Automatic Name</Value>
</Option>
<Option>
<Text>Manually</Text>
<Value>Manual Name</Value>
</Option>
</Options>
<Value>Current Name</Value>
</Person>
</People>
In IE11, the same code produces identical JSON, but the XML looks like this:
<People xmlns="http://MyNS.com" xmlns:NS1="" NS1:xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:NS2="" NS2:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Person>
<Id>a012345d-12bc-41fe-a35d-ff713bc876e0</Id>
<Name>Complete Automatically?</Name>
<Prompt>Would you like to complete this...</Prompt>
<Options>
<Option>
<Text>Automatically</Text>
<Value>Automatic Name</Value>
</Option>
<Option>
<Text>Manually</Text>
<Value>Manual Name</Value>
</Option>
</Options>
<Value>Current Name</Value>
</Person>
</People>
So there are these empty xmlns:NSx="" namespaces and the actual namespaces are prefixed incorrectly.
Is there a way to get IE11 to serialize namespaces correctly?
which version of jxon are you using?
The version is 2.0.0-beta.5 which I believe is the latest.
I also ran into this issue when trying to generate GPX data using the togpx module (0.5.4) in IE11 (which seems to be using version 2.0.0-beta.5 of JXON).
PR #53 adds some additional namespace support, can someone with an IE setup please test and see if it also fixes this issue?
@nrenner Hello! Thank you for attempting to fix this problem. I've found a couple of things with your PR:
- It appears that, for JSON, your PR moves the namespaces up so that they are listed first. I don't mind that, but I do think it's worth calling out. The rest of the JSON looks to be serialized correctly.
- For XML, on Chrome 88.0.4324.190, the
xmlnsnamespace is moved to the end of the top-level element. Again, this is fine, but worth mentioning. - For XML, on IE11 (11.789.19041.0), only the
xmlnsvalue is provided; both of thexmlns:xsdandxmlns:xsivalues are missing. On Chrome, those values are provided, and appear to be defaults. Why do they not show up on IE11?
So this PR is a step towards fixing my original issue, but the way it affects serialization for both JSON and XML might be cause for concern for other people.
For XML, on Chrome 88.0.4324.190, the xmlns namespace is moved to the end of the top-level element.
That's because it's ordered like that in the input XML and the PR sets the namespace attributes explicitly. The current master version relies on the XML serializer to add the xmlns attribute automatically, which seems put it first.
For XML, on IE11 (11.789.19041.0), only the xmlns value is provided; both of the xmlns:xsd and xmlns:xsi values are missing.
My guess would be, that those namespaces are not used in the document and are therefore removed by the XML serializer, probably nothing we can do about it. My question would rather be why they are added in the original XML?