xmldom
xmldom copied to clipboard
Namespace propagation is not correct on serialization
I use this app to generate XML document. fourth element in generated XML doesn't have xmlns:p2="http//second.namespace.com"
const root = new DOMParser().parseFromString(`<root></root>`, "application/xml");
function createElement(tagName, namespace, parent) {
const el = root.createElementNS(namespace, tagName);
parent.appendChild(el);
return el;
}
const namespace1 = "http//first.namespace.com";
const namespace2 = "http//second.namespace.com";
const el1 = createElement("p1:first", namespace1, root.documentElement);
const el2 = createElement("p2:second", namespace2, el1);
const el3 = createElement("p1:third", namespace1, el1);
const el4 = createElement("p2:fourth", namespace2, el3);
const xml = new XMLSerializer().serializeToString(root);
console.log(xml)
XMLDOM output:
<root>
<p1:first xmlns:p1="http//first.namespace.com">
<p2:second xmlns:p2="http//second.namespace.com"/>
<p1:third>
<p2:fourth/>
</p1:third>
</p1:first>
</root>
Chrome output:
<root>
<p1:first xmlns:p1="http//first.namespace.com">
<p2:second xmlns:p2="http//second.namespace.com"/>
<p1:third>
<p2:fourth xmlns:p2="http//second.namespace.com"/>
</p1:third>
</p1:first>
</root>
Wouldn't a second xmlns:p2 (on the p2:fourth element) be redundant, here?
Wouldn't a second
xmlns:p2(on the p2:fourth element) be redundant, here?
No, because that namespace declaration applies only to the second element and any descendants.
https://www.w3.org/TR/xml-names/#scoping
Oh for some reason I was reading that completely backwards; yeah, the specified XMLDOM output isn't correct.
I found that commented code
https://github.com/jindw/xmldom/blob/master/dom.js#L1042-L1043
If uncomment that code it works fine
Example 1
const doc = new DOMImplementation().createDocument(null, "root");
doc.documentElement.appendChild(doc.createElement("child"));
const signature1 = doc.createElementNS("https://signature.com", "Signature");
doc.documentElement.appendChild(signature1);
const signature2 = doc.createElementNS("https://signature.com", "Signature");
doc.documentElement.appendChild(signature2);
const xml = new XMLSerializer().serializeToString(doc);
console.log(xml);
// Output
// <root>
// <child/>
// <Signature xmlns="https://signature.com"/>
// <Signature xmlns="https://signature.com"/>
// </root>
Example 2
const doc = new DOMImplementation().createDocument(null, "root");
const p1First = doc.createElementNS("http//first.namespace.com", "p1:first");
const p2Second = doc.createElementNS("http//second.namespace.com", "p2:second");
const p1Third = doc.createElementNS("http//first.namespace.com", "p1:third");
const p2Fourth = doc.createElementNS("http//second.namespace.com", "p2:fourth");
doc.documentElement.appendChild(p1First);
p1First.appendChild(p2Second)
p1First.appendChild(p1Third)
p1Third.appendChild(p2Fourth)
const xml = new XMLSerializer().serializeToString(doc);
console.log(xml);
// Output
// <root>
// <p1:first xmlns:p1="http//first.namespace.com">
// <p2:second xmlns:p2="http//second.namespace.com"/>
// <p1:third>
// <p2:fourth xmlns:p2="http//second.namespace.com"/>
// </p1:third>
// </p1:first>
// </root>