oozcitak / xmlbuilder2

An XML builder for node.js
361 stars 35 forks source link

Namespace is removed when importing fragments #178

Open svobik7 opened 4 months ago

svobik7 commented 4 months ago

Describe the bug There is a namespace misalignment when creating child element with .ele and when importing fragment child using .import.

To Reproduce ⚙️ Try to run following functions and compare their outputs.

  1. Function called "workingExample" outputs properly.
function workingExample() {
  const doc = create({ version: '1.0' });
  const root = doc.ele('ns1', 'Root');

  // creates Child element with proper namespace set to "ns2"
  root.ele('ns2', 'Child').txt('text');

  return doc.end({ prettyPrint: true })
}

✅ Output of workingExample function that is OK:

<?xml version="1.0"?>
<Root xmlns="ns1">
  <Child xmlns="ns2">text</Child>
</Root>
  1. Function called "notWorkingExample" outputs child element without namespace.
function notWorkingExample() {
  const doc = create({ version: '1.0' });
  const root = doc.ele('ns1', 'Root');

  // inserts Child element (fragment) but namespace 'ns2' is omitted
  const child = fragment().ele('ns2', 'Child').txt('text');
  root.import(child);

  return doc.end({ prettyPrint: true })
}

⚠️ Output of notWorkingExample function that is NOK:

<?xml version="1.0"?>
<Root xmlns="ns1">
  <Child>text</Child>
</Root>

Expected behavior Output of notWorkingExample function is equal to output of workingExample = both functions output:

<?xml version="1.0"?>
<Root xmlns="ns1">
  <Child xmlns="ns2">text</Child>
</Root>

Version:

afladmark commented 1 month ago

I've also encountered this issue and can confirm that the PR addresses it for me. Haven't noticed any other issues.

nicolasiscoding commented 1 month ago

Seeing the same issues coming from 2 and going to 3