Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XMLSerializer and namespaces? #97

Open
mbd-dbc-dk opened this issue Aug 27, 2014 · 4 comments
Open

XMLSerializer and namespaces? #97

mbd-dbc-dk opened this issue Aug 27, 2014 · 4 comments

Comments

@mbd-dbc-dk
Copy link

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?

@highsource
Copy link

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.

@daurnimator
Copy link
Contributor

duplicate of #45

@bjrmatos
Copy link

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

@arve0
Copy link

arve0 commented Jul 13, 2016

Work around:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants