/* libxmq - Copyright (C) 2026 Fredrik Öhrström (spdx: MIT) */

// Declare a default namespace on the root and put the children in it too.

import { DOMImplementation } from "@xmldom/xmldom";
import { print } from "libxmq";

const XMLNS = "http://www.w3.org/2000/xmlns/";
const CARGO = "urn:cargo";

const doc = new DOMImplementation().createDocument(CARGO, "car", null);
const car = doc.documentElement!;
// The W3C DOM keeps the namespace of each element, the declaration is an attribute.
car.setAttributeNS(XMLNS, "xmlns", CARGO);

// The children are created in the same namespace, the declaration of the root covers them.
for (const [key, value] of [["model", "escargo"], ["color", "green"]])
{
    const e = doc.createElementNS(CARGO, key);
    e.appendChild(doc.createTextNode(value));
    car.appendChild(e);
}

console.log(print(doc, { compact: true }));