w3c-xmlserializer

中文文档
6.0.0发布于 2026/09/21MITCommonJS构建来源已验证近 30 天下载 4亿

A per-spec XML serializer implementation

原始使用文档

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.

版本与发布记录

w3c-xmlserializer 共发布 14 个版本,首次发布于 2018/07/02;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.0.0latest2026/09/211 个月前
5.0.02023/11/122 年前
4.0.02022/11/203 年前
3.0.02021/09/195 年前
2.0.02020/01/036 年前
1.1.22019/03/257 年前

关于 w3c-xmlserializer 的常见问题

w3c-xmlserializer 是什么?

A per-spec XML serializer implementation

如何安装 w3c-xmlserializer?

在项目目录运行 npm install w3c-xmlserializer,或使用 pnpm add / yarn add / bun add。

w3c-xmlserializer 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/w3c-xmlserializer 是否存在。

w3c-xmlserializer 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

w3c-xmlserializer 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.22.2 || ^24.15.0 || >=26.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

w3c-xmlserializer 可以商用吗?

w3c-xmlserializer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

w3c-xmlserializer 的下载量有多少?

根据 npm 官方下载统计,w3c-xmlserializer 在 2026/10/05 – 2026/11/03 这 30 天内被下载 404,190,633 次,日均约 13,473,021 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

w3c-xmlserializer 还在维护吗?

最新版本 6.0.0 发布于 2026/09/21(1 个月前);至今共发布 14 个版本,其中过去 12 个月发布了 1 个;首次发布于 2018/07/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。