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

// Declare an extra namespace with a prefix on the root, and use it in a child.

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

const XMLNS = "http://www.w3.org/2000/xmlns/";
const POWER = "urn:power";
const SOFT = "urn:soft";

const doc = new DOMImplementation().createDocument(POWER, "flower", null);
const flower = doc.documentElement!;
flower.setAttributeNS(XMLNS, "xmlns", POWER);

// The extra declaration, s for urn:soft, is an attribute of the root.
flower.setAttributeNS(XMLNS, "xmlns:s", SOFT);

// The child uses the prefix declared on its ancestor.
const petals = doc.createElementNS(SOFT, "s:petals");
petals.appendChild(doc.createTextNode("many"));
flower.appendChild(petals);

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