w3c-xmlserializer

A per-spec XML serializer implementation

MIT 13 个版本
安装
npm install w3c-xmlserializer
yarn add w3c-xmlserializer
pnpm add w3c-xmlserializer
bun add w3c-xmlserializer
README

w3c-xmlserializer

An XML serializer that follows the W3C specification.

This package can be used in Node.js, as long as you feed it a DOM node, e.g. one produced by jsdom.

Basic usage

Assume you have a DOM tree rooted at a node node. In Node.js, you could create this using jsdom as follows:

const { JSDOM } = require("jsdom");

const { document } = new JSDOM().window;
const node = document.createElement("akomaNtoso");

Then, you use this package as follows:

const serialize = require("w3c-xmlserializer");

console.log(serialize(node));
// => '<akomantoso xmlns="http://www.w3.org/1999/xhtml"></akomantoso>'

requireWellFormed option

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

serialize(node, { requireWellFormed: true });

which will cause Errors to be thrown when non-well-formed constructs are encountered. Per the spec, this largely is about imposing constraints on the names of elements, attributes, etc.

As a point of reference, on the web platform:

  • The innerHTML getter uses the require-well-formed mode, i.e. trying to get the innerHTML of non-well-formed subtrees will throw.
  • The xhr.send() method does not require well-formedness, i.e. sending non-well-formed Documents will serialize and send them anyway.
版本列表
5.0.0 2023-11-12
4.0.0 2022-11-20
3.0.0 2021-09-18
2.0.0 2020-01-02
1.1.2 2019-03-25
1.1.1 2019-03-25
1.1.0 2019-03-25
1.0.1 2018-12-11
1.0.0 2018-10-08
0.0.4-0 2018-08-18
0.0.3 2018-08-07
0.0.2 2018-08-07
0.0.1 2018-07-01