{"id":13717010,"url":"https://github.com/syntax-tree/xast-util-to-xml","last_synced_at":"2025-06-29T22:03:43.082Z","repository":{"id":43396599,"uuid":"231243946","full_name":"syntax-tree/xast-util-to-xml","owner":"syntax-tree","description":"utility to serialize to XML","archived":false,"fork":false,"pushed_at":"2023-07-18T14:30:35.000Z","size":126,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-06-14T21:03:58.226Z","etag":null,"topics":["serialize","stringify","syntax-tree","unist","util","xast","xast-util","xml"],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syntax-tree.png","metadata":{"funding":{"github":"unifiedjs","open_collective":"unified"},"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-01T17:23:51.000Z","updated_at":"2024-10-12T14:19:49.000Z","dependencies_parsed_at":"2024-06-19T00:02:37.561Z","dependency_job_id":"d3fb6b47-5e61-4f52-b8b2-2b957a5b063a","html_url":"https://github.com/syntax-tree/xast-util-to-xml","commit_stats":{"total_commits":43,"total_committers":3,"mean_commits":"14.333333333333334","dds":"0.046511627906976716","last_synced_commit":"3b9d676d8fb789d7ba74e71c387b20f9d323091f"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/xast-util-to-xml","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fxast-util-to-xml","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fxast-util-to-xml/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fxast-util-to-xml/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fxast-util-to-xml/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/xast-util-to-xml/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fxast-util-to-xml/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261960483,"owners_count":23236567,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["serialize","stringify","syntax-tree","unist","util","xast","xast-util","xml"],"created_at":"2024-08-03T00:01:16.725Z","updated_at":"2025-06-29T22:03:42.997Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# xast-util-to-xml\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[xast][] utility to serialize as XML.\n\n## Contents\n\n*   [What is this?](#what-is-this)\n*   [When should I use this?](#when-should-i-use-this)\n*   [Install](#install)\n*   [Use](#use)\n*   [API](#api)\n    *   [`toXml(tree[, options])`](#toxmltree-options)\n    *   [`Options`](#options)\n    *   [`Quote`](#quote-1)\n    *   [`Raw`](#raw)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Security](#security)\n*   [Related](#related)\n*   [Contribute](#contribute)\n*   [License](#license)\n\n## What is this?\n\nThis package is a utility that turns a xast tree into a string of XML.\n\n## When should I use this?\n\nYou can use this utility when you want to get the serialized XML that is\nrepresented by the syntax tree, either because you’re done with the syntax tree,\nor because you’re integrating with another tool that does not support syntax\ntrees.\n\nThis utility has options to configure how the XML is serialized.\nThese options help when building tools that make output pretty (such as\nformatters) or ugly (such as minifiers).\n\nThe utility [`xast-util-from-xml`][xast-util-from-xml] does the inverse of this\nutility.\nIt turns XML into xast.\n\nThe utility [`hast-util-to-html`][hast-util-to-html] does the same as this\nutility but for HTML: it turns [hast][] into HTML.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install xast-util-to-xml\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {toXml} from 'https://esm.sh/xast-util-to-xml@4'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {toXml} from 'https://esm.sh/xast-util-to-xml@4?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport {u} from 'unist-builder'\nimport {x} from 'xastscript'\nimport {toXml} from 'xast-util-to-xml'\n\nconst tree = u('root', [\n  u('instruction', {name: 'xml'}, 'version=\"1.0\" encoding=\"utf-8\"'),\n  u('text', '\\n'),\n  x('ncx', {xmlns: 'http://www.daisy.org/z3986/2005/ncx/', version: '2005-1'}, [\n    u('text', '\\n  '),\n    x('head', [\n      u('text', '\\n    '),\n      x('meta', {name: 'dtb:uid', content: 'urn:isbn:9781234567891'}),\n      u('text', '\\n  ')\n    ]),\n    u('text', '\\n  '),\n    x('docTitle', [x('text', 'A Christmas Carol')]),\n    u('text', '\\n  '),\n    x('docAuthor', [x('text', 'Charles Dickens')]),\n    u('text', '\\n')\n  ])\n])\n\nconsole.log(toXml(tree))\n```\n\nYields:\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\"\u003e\n  \u003chead\u003e\n    \u003cmeta name=\"dtb:uid\" content=\"urn:isbn:9781234567891\"\u003e\u003c/meta\u003e\n  \u003c/head\u003e\n  \u003cdocTitle\u003e\u003ctext\u003eA Christmas Carol\u003c/text\u003e\u003c/docTitle\u003e\n  \u003cdocAuthor\u003e\u003ctext\u003eCharles Dickens\u003c/text\u003e\u003c/docAuthor\u003e\n\u003c/ncx\u003e\n```\n\n## API\n\nThis package exports the identifier [`toXml`][api-to-xml].\nThere is no default export.\n\n### `toXml(tree[, options])`\n\nSerialize a xast tree to XML.\n\n###### Parameters\n\n*   `tree` ([`Array\u003cNode\u003e`][node] or `Node`)\n    — xast node(s) to serialize\n*   `options` ([`Options`][api-options], optional)\n    — configuration\n\n###### Returns\n\nSerialized XML (`string`).\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n##### Fields\n\n###### `allowDangerousXml`\n\nAllow `raw` nodes and insert them as raw XML (`boolean`, default: `false`).\n\nWhen `false`, `Raw` nodes are encoded.\n\n\u003e ⚠️ **Danger**: only set this if you completely trust the content.\n\n###### `closeEmptyElements`\n\nClose elements without any content with slash (`/`) on the opening tag instead\nof an end tag: `\u003ccircle /\u003e` instead of `\u003ccircle\u003e\u003c/circle\u003e` (`boolean`, default:\n`false`).\n\nSee `tightClose` to control whether a space is used before the slash.\n\n###### `quote`\n\nPreferred quote to use ([`Quote`][api-quote], default: `'\"'`).\n\n###### `quoteSmart`\n\nUse the other quote if that results in less bytes (`boolean`, default:\n`false`).\n\n###### `tightClose`\n\nDo not use an extra space when closing self-closing elements: `\u003ccircle/\u003e`\ninstead of `\u003ccircle /\u003e` (`boolean`, default: `false`).\n\n\u003e 👉 **Note**: only used if `closeEmptyElements: true`.\n\n### `Quote`\n\nXML quotes for attribute values (TypeScript type).\n\n###### Type\n\n```ts\ntype Quote = '\"' | \"'\"\n```\n\n### `Raw`\n\nRaw (TypeScript type).\n\n###### Type\n\n```ts\nimport type {Data, Literal} from 'xast'\n\ninterface Raw extends Literal {\n  type: 'raw'\n  data?: RawData | undefined\n}\n\nexport interface RawData extends Data {}\n```\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`Options`][api-options], [`Quote`][api-quote],\nand [`Raw`][api-raw].\n\nIt also registers the node type with `@types/xast`.\nIf you’re working with the syntax tree, make sure to import this utility\nsomewhere in your types, as that registers the new node types in the tree.\n\n```js\n/**\n * @typedef {import('xast-util-to-xml')}\n */\n\nimport {visit} from 'unist-util-visit'\n\n/** @type {import('xast').Root} */\nconst tree = getXastNodeSomeHow()\n\nvisit(tree, function (node) {\n  // `node` can now be a raw node.\n})\n```\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `xast-util-to-xml@^4`,\ncompatible with Node.js 16.\n\n## Security\n\nXML can be a dangerous language: don’t trust user-provided data.\n\n## Related\n\n*   [`xast-util-from-xml`](https://github.com/syntax-tree/xast-util-from-xml)\n    — parse from xml\n*   [`hast-util-to-xast`](https://github.com/syntax-tree/hast-util-to-xast)\n    — transform hast (html, svg) to xast (xml)\n*   [`xastscript`](https://github.com/syntax-tree/xastscript)\n    — create xast trees\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for\nways to get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/syntax-tree/xast-util-to-xml/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/xast-util-to-xml/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/xast-util-to-xml.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/xast-util-to-xml\n\n[downloads-badge]: https://img.shields.io/npm/dm/xast-util-to-xml.svg\n\n[downloads]: https://www.npmjs.com/package/xast-util-to-xml\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=xast-util-to-xml\n\n[size]: https://bundlejs.com/?q=xast-util-to-xml\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[health]: https://github.com/syntax-tree/.github\n\n[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[xast]: https://github.com/syntax-tree/xast\n\n[node]: https://github.com/syntax-tree/xast#nodes\n\n[hast]: https://github.com/syntax-tree/hast\n\n[xast-util-from-xml]: https://github.com/syntax-tree/xast-util-from-xml\n\n[hast-util-to-html]: https://github.com/syntax-tree/hast-util-to-html\n\n[api-to-xml]: #toxmltree-options\n\n[api-options]: #options\n\n[api-quote]: #quote-1\n\n[api-raw]: #raw\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["xast utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fxast-util-to-xml","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Fxast-util-to-xml","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fxast-util-to-xml/lists"}