import { DOMImplementation } from "@xmldom/xmldom";
import type { Document, Element } from "@xmldom/xmldom";
import { print } from "libxmq";
const XMLNS = "http://www.w3.org/2000/xmlns/";
const KRAFTWERK = "urn:kraftwerk";
const CARGO = "urn:cargo";
/** Add a key value in a namespace, eg krf:who = 'we are'. */
function addKeyValue(doc: Document, parent: Element, ns: string | null, name: string, value: string): void
{
const e = doc.createElementNS(ns, name);
e.appendChild(doc.createTextNode(value));
parent.appendChild(e);
}
const doc = new DOMImplementation().createDocument(KRAFTWERK, "krf:robot", null);
const robot = doc.documentElement!;
robot.setAttributeNS(XMLNS, "xmlns:krf", KRAFTWERK);
addKeyValue(doc, robot, KRAFTWERK, "krf:who", "we are");
addKeyValue(doc, robot, KRAFTWERK, "krf:the", "robots");
const car = doc.createElementNS(CARGO, "c:car");
car.setAttributeNS(XMLNS, "xmlns:c", CARGO);
addKeyValue(doc, car, CARGO, "c:model", "escargo");
robot.appendChild(car);
const box = doc.createElementNS(null, "box");
addKeyValue(doc, box, null, "color", "blue");
robot.appendChild(box);
console.log(print(doc, { compact: true, escapeNewlines: true }));