1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
An XML serializer that follows the [W3C specification](https://w3c.github.io/DOM-Parsing/).
This package can be used in Node.js, as long as you feed it a DOM node, e.g. one produced by [jsdom](https://github.com/jsdom/jsdom).
Assume you have a DOM tree rooted at a node `node`. In Node.js, you could create this using [jsdom](https://github.com/jsdom/jsdom) as follows:
```js
const { JSDOM } = require("jsdom");
const { document } = new JSDOM().window;
const node = document.createElement("akomaNtoso");
```
Then, you use this package as follows:
```js
const serialize = require("w3c-xmlserializer");
console.log(serialize(node));
// => '<akomantoso xmlns="http://www.w3.org/1999/xhtml"></akomantoso>'
```
By default the input DOM tree is not required to be "well-formed"; any given input will serialize to some output string. You can instead require well-formedness via
```js
serialize(node, { requireWellFormed: true });
```
which will cause `Error`s to be thrown when non-well-formed constructs are encountered. [Per the spec](https://w3c.github.io/DOM-Parsing/#dfn-require-well-formed), this largely is about imposing constraints on the names of elements, attributes, etc.
As a point of reference, on the web platform:
* *