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

Namespace propagation is not correct on serialization #247

Open
microshine opened this issue Sep 25, 2018 · 4 comments
Open

Namespace propagation is not correct on serialization #247

microshine opened this issue Sep 25, 2018 · 4 comments

Comments

@microshine
Copy link
Contributor

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>
@awwright
Copy link

Wouldn't a second xmlns:p2 (on the p2:fourth element) be redundant, here?

@Phrogz
Copy link

Phrogz commented Nov 11, 2018

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

@awwright
Copy link

Oh for some reason I was reading that completely backwards; yeah, the specified XMLDOM output isn't correct.

@microshine
Copy link
Contributor Author

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>

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

3 participants