Problem
When defining namespaces, xmlns:xmlns is explicitly defined. This isn't a valid definition, and if it isn't stripped out, other xml validators or libraries will choke on it.
Workaround
Stringify the XML and rip out the xmlns:xmlns attribute.
Reproduction
(function (document, Ti) {
document = document || Ti && Ti.XML.parseString('<a/>');
function xmlAddNamespaceAttribute(domNode, name, attributeNamespace) {
var doc = domNode.ownerDocument || document;
var attribute = doc.createAttributeNS('http://www.w3.org/2000/xmlns/', name);
attribute.nodeValue = attributeNamespace;
(domNode.documentElement || domNode).setAttributeNode(attribute);
}
var feed = document.implementation.createDocument('http://www.w3.org/2005/Atom', 'atom:feed', null);
xmlAddNamespaceAttribute(feed, "xmlns:m", 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata');
xmlAddNamespaceAttribute(feed, "xmlns:d", 'http://schemas.microsoft.com/ado/2007/08/dataservices');
var expected = '<?xml version="1.0" encoding="UTF-8"?>\
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom"\
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"\
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"/>'.replace(/\s{1,}/ig, ' ').replace(/> </ig, '><');
var actual = '<?xml version="1.0" encoding="UTF-8"?>' + ((Ti && Ti.XML) || new XMLSerializer()).serializeToString(feed);
if (Ti) {
Ti.API.info('Expected:\n' + expected);
Ti.API.info('Actual:\n' + actual);
}
else {
console.log('Expected:\n' + expected);
console.log('Actual:\n' + actual);
}
if (expected != actual) {
alert('FAIL! Check logs for more information.');
}
else {
alert('PASS!');
}
})(this['document'], this['Ti']);
Tested With
Passes in Chrome 18.0
Fails in iOS Simulator with Titanium Mobile 2.1.0 (05/03/12 17:34 c366287)
Expected XML results:
<?xml version="1.0" encoding="UTF-8"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"/>
Actual XML results:
<?xml version="1.0" encoding="UTF-8"?>
<atom:feed xmlns:xmlns="http://www.w3.org/2000/xmlns/"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"/>
Discrepancies:
- xmlns:xmlns
- absence of xmlns:atom
Creating a document without a prefix for the root item should output a default namespace definition when serialized. For example,
should result in the following when serialized:
instead I'm getting this:
PR merged https://github.com/appcelerator/titanium_mobile/pull/2302
Verified fix with: Titanium Studio, build: 2.1.1.201207271312 Titanium SDK: 2.2.0.v20120816212512 Devices: iPhone 4s 5.0.1 Simulator 5.1 Mac osx 10.8 Mountain Lion