In HTML5/JavaScript, if I create a namespaced dom node programmatically, like so:
svgElt = document.createElementNS(\"http://www.w3.org/2000/svg\", \'svg\')
You can simply use an XMLSerializer for this, which will stringify your DOM tree to xml and thus preserve the namespaces.
const svgElt = document.createElementNS("http://www.w3.org/2000/svg", 'svg') const serialized = new XMLSerializer().serializeToString(svgElt); console.log(serialized);