Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-9084] iOS: XML xmlns:xmlns definition

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2012-06-01T17:07:15.000+0000
Affected Version/sRelease 2.1.0
Fix Version/sRelease 2.1.0, Sprint 2012-11 API
ComponentsiOS
Labelsapi, parity, qe-port
ReporterDawson Toth
AssigneeVishal Duggal
Created2012-05-09T14:55:36.000+0000
Updated2012-08-17T12:40:43.000+0000

Description

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

Comments

  1. Jeff English 2012-06-01

    Creating a document without a prefix for the root item should output a default namespace definition when serialized. For example,
       var entry = document.implementation.createDocument('http://www.w3.org/2005/Atom', 'entry', null);
       
    should result in the following when serialized:
       <?xml version="1.0" encoding="UTF-8"?>
       <entry xmlns="http://www.w3.org/2005/Atom" />
       
    instead I'm getting this:
       <?xml version="1.0" encoding="UTF-8"?>
       <entry xmlns="http://www.w3.org/2000/xmlns/" xmlns="http://www.w3.org/2005/Atom" />
       
  2. Max Stepanov 2012-06-01

    PR merged https://github.com/appcelerator/titanium_mobile/pull/2302
  3. Olga Romero 2012-08-17

    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

JSON Source