xmldom
xmldom copied to clipboard
XMLSerializer and namespaces?
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?
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.
duplicate of #45
same here.. xmldom don't output the namespace when a child node is serialized.
Work around:
var el = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
el.setAttribute('xmlns', 'http://www.w3.org/2000/svg');