Xmlserializer
xmlserializer serializes a DOM subtree or DOM document into XML/XHTML
Install / Use
/learn @cburgmer/XmlserializerREADME
xmlserializer serializes a DOM subtree or DOM document into XML/XHTML. <a href="https://www.npmjs.org/package/xmlserializer"> <img src="https://badge.fury.io/js/xmlserializer.svg" align="right" alt="NPM version" /> </a>
It understands documents generated by parse5 and regular browser DOMs (and thus can act as a drop-in replacement for XMLSerializer which for some browsers only serializes true XML documents).
Limitations
See the wiki for limitations in HTML to XML conversion.
Currently some cases are treated differently to the XMLSerializer implementation of the browsers:
- Invalid characters (ASCII control characters) that are invalid in XML 1.0 are removed on serialization. The browsers silently include those characters and on reparsing those documents throw a parser exception.
- Dashes in comments are escaped to provide valid comments in XHTML. Firefox does not do this.
- The
xmlnsattribute has higher precedence than the type of the DOM object passed to the serializer. - Small differences in style: no space in self-closing tag, empty value for boolean attributes, quoting of single apostrophes in attribute values.
This behaviour might become optional in the future.
Demo
See https://runkit.com/embed/siinwcyjdcjw
Run tests and build for the browser
$ npm install && npm test
Example
For a browser based example run npm test and see example.html.
The same code for Node.js:
var xmlserializer = require('xmlserializer');
var html2xhtml = function (htmlString) {
var parser = require('parse5'),
dom = parser.parse(htmlString);
return xmlserializer.serializeToString(dom);
};
console.log(html2xhtml('<br>'));
// => <html xmlns="http://www.w3.org/1999/xhtml"><head/><body><br/></body></html>
Related Skills
node-connect
352.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
352.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
352.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
