I have an XHTML document. I use XHR to import another XML document that is supposed to be XHTML, except the author forgot to put an xmlns
on the root. As such, all nodes in the XML document have no namespace.
When I import a node from this other document and append it to my document I get a node that looks right (e.g. <h1>Hi Mom!</h1>
) but that has no namespace. As such, the web browser applies display:inline
.
I could fix this by using code like the following (untested)…but I'd rather not, if a better route exists:
function changeNodeNamespace(node,namespace){
var dup = document.createElementNS(namespace,node.nodeName);
if (node.hasAttributes()){
for (var a=node.attributes,i=a.length;i--;){
dup.setAttributeNS(a[i].namespaceURI,a[i].nodeName,a[i].value);
}
}
if (node.hasChildNodes()){
for (var a=node.childNodes,i=0,len=a.length;i<len;++i){
if (a[i].nodeType==1){ // ELEMENT_NODE
dup.appendChild(changeNodeNamespace(a[i],namespace));
}else{
dup.appendChild(a[i].cloneNode(false));
}
}
}
return dup;
}
Other than re-authoring the documents I import to have the correct namespace, or-recursively re-creating every element/attribute/textnode in a new namespace based on the originals, can I import the elements themselves and somehow change their namespace to http://www.w3.org/1999/xhtml
?