oozcitak / xmlbuilder2

An XML builder for node.js
366 stars 36 forks source link

Bring back inherit namespaces #18

Closed benrei closed 4 years ago

benrei commented 4 years ago

In version <=1.8.1 I was able to dynamically build a xml by creating fragment() and using .import(). Example

const { create, fragment } = require('xmlbuilder2');

const doc = create({ version: '1.0', encoding: 'UTF-8' })
  .ele('Invoice', { xmlns: 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2', 'xmlns:cac': 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2', 'xmlns:cbc': "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" });

let frag = fragment();
frag.ele('cbc:StreetName').txt('Karl Johans gate 45')
frag.ele('cbc:CityName').txt('Oslo')
frag.ele('cbc:PostalZone').txt('0162')
frag.ele('cac:Country').ele('cbc:IdentificationCode', { listID: "ISO3166-1:Alpha2" }).txt('NO')

doc.ele('cac:Address').import(frag)
console.log(doc.end({ prettyPrint: true }));

v 1.8.1 output: (As I want)

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
  <cac:Address>
    <cbc:StreetName>Karl Johans gate 45</cbc:StreetName>
    <cbc:CityName>Oslo</cbc:CityName>
    <cbc:PostalZone>0162</cbc:PostalZone>
    <cac:Country>
      <cbc:IdentificationCode listID="ISO3166-1:Alpha2">NO</cbc:IdentificationCode>
    </cac:Country>
  </cac:Address>
</Invoice>

In versions >1.8.1 I get the empty xmlns=""

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
  <cac:Address>
    <cbc:StreetName xmlns="">Karl Johans gate 45</cbc:StreetName>
    <cbc:CityName xmlns="">Oslo</cbc:CityName>
    <cbc:PostalZone xmlns="">0162</cbc:PostalZone>
    <cac:Country xmlns="">
      <cbc:IdentificationCode listID="ISO3166-1:Alpha2">NO</cbc:IdentificationCode>
    </cac:Country>
  </cac:Address>
</Invoice>

How can this be done in versions over 1.8.1?

oozcitak commented 4 years ago

Sorry I was late adressing this.

privateOmega commented 1 year ago

@oozcitak I think this introduced a bug which discards the namespace reference of top element of the fragment, and force it to use parent's namespace, I hope that wasnt the intention.