xmldom icon indicating copy to clipboard operation
xmldom copied to clipboard

XMLSerializer and namespaces?

Open mbd-dbc-dk opened this issue 11 years ago • 4 comments
trafficstars

Hi there.

Not entirely sure this is an issue, really, but does anyone know how to get the XMLSerializer to output namespace information?

Say, code like this:

var doc = new domi.createDocument( "http://www.example.org/", "hest:foo");

Gives this doc:

#2={doctype:(void 0), implementation:{}, childNodes:{0:#1={_nsMap:{}, attributes:{_ownerElement:#1#}, childNodes:{}, ownerDocument:#2#, nodeName:"hest:foo", tagName:"hest:foo", namespaceURI:"http://www.example.org/", prefix:"hest", localName:"foo", previousSibling:null, nextSibling:null, parentNode:#2#}, length:1}, documentElement:#1#, firstChild:#1#, lastChild:#1#, _inc:2}

But, toString() outputs:

"<hest:foo/>"

not what I would expect:

"<hest:foo xmlns:hest="http://www.example.org/" />"

Does anyone know is this is a bug, me using xmldom wrong, or?

mbd-dbc-dk avatar Aug 27 '14 12:08 mbd-dbc-dk

I have also ran into this issue. Below are my tests:

        "Without prefix" : function(test)
        {
            var xmldom = require('xmldom');
            var di = new (xmldom.DOMImplementation)();
            var doc = di.createDocument();
            var element = doc.createElementNS('urn:test', 'test');
            // namespace must be declared explicitly
            element.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', 'urn:test');
            doc.appendChild(element);
            var xs = new (xmldom.XMLSerializer)();
            var docAsString = xs.serializeToString(doc);
            test.equal('<test xmlns="urn:test"/>', docAsString);
            test.done();
        },
        "With Prefix" : function(test)
        {
            var xmldom = require('xmldom');
            var di = new (xmldom.DOMImplementation)();
            var doc = di.createDocument();
            var element = doc.createElementNS('urn:test', 't:test');
            // namespace must be declared explicitly
            element.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:t', 'urn:test');
            doc.appendChild(element);
            var xs = new (xmldom.XMLSerializer)();
            var docAsString = xs.serializeToString(doc);
            test.equal('<t:test xmlns:t="urn:test"/>', docAsString);
            test.done();
        }

If element.setAttributeNS is removed, tests fails since namespaces are not declared/serialized in the resulting strings.

Similar tests work in browser environments, xmlns attributes are generated there.

highsource avatar Sep 10 '14 11:09 highsource

duplicate of #45

daurnimator avatar May 08 '15 05:05 daurnimator

same here.. xmldom don't output the namespace when a child node is serialized.

bjrmatos avatar May 20 '15 16:05 bjrmatos

Work around:

var el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
el.setAttribute('xmlns', 'http://www.w3.org/2000/svg');

arve0 avatar Jul 13 '16 22:07 arve0