Skip to main content

@libs/xml@7.0.4
Built and signed on GitHub Actions

Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score100%
License
MIT
Downloads879/wk
Published3 weeks ago (7.0.4)

📃 XML parser/stringifier with no dependencies.

Functions

f
cdata(text: string): Omit<xml_text, "~parent">

Helper to create a CDATA node.

f
comment(text: string): Omit<xml_text, "~parent">

Helper to create a comment node.

f
parse(
content: string | ReaderSync,
options?: parse_options
): xml_document

Parse a XML string into an object.

f
stringify(
document: stringifyable,
options?: stringify_options
): string

Stringify an xml_document object into a XML string.

Type Aliases

T
clean_options = { attributes?: boolean; comments?: boolean; doctype?: boolean; instructions?: boolean; }

XML parser parse_options.clean

  • attributes: boolean

    Remove attributes from result.

  • comments: boolean

    Remove comments from result.

  • doctype: boolean

    Remove XML doctype from result.

  • instructions: boolean

    Remove XML processing instructions from result.

T
flatten_options = { attributes?: boolean; text?: boolean; empty?: boolean; }

XML parser parse_options.flatten

  • attributes: boolean

    If node only contains attributes values (i.e. with key starting with @), it'll be flattened as a regular object without @ prefixes.

  • empty: boolean

    If node does not contains any attribute or text, it'll be flattened to null (defaults to true).

  • text: boolean

    If node only contains a #text value, it'll be flattened as a string (defaults to true).

T
format_options = { indent?: string; breakline?: number; }

XML stringifier stringify_options.format

  • breakline: number

    Break text node if its length is greater than this value (defaults to 128).

  • indent: string

    Indent string (defaults to " "). Set to empty string to disable indentation and enable minification.

T
Nullable<T> = T | null

Nullable type.

T
parse_options = { clean?: clean_options; flatten?: flatten_options; revive?: revive_options; mode?: "xml" | "html"; }

XML parser options.

  • clean: clean_options

    Remove elements from result.

  • flatten: flatten_options

    Flatten result depending on node content.

  • mode: "xml" | "html"

    Parsing mode. Using html is more permissive and will not throw on some invalid XML syntax. Mainly unquoted attributes will be supported and not properly closed tags will be accepted.

  • revive: revive_options

    Revive result.

T
ReaderSync = { readSync(buffer: Uint8Array): Nullable<number>; }

Synchronous reader.

  • readSync(buffer: Uint8Array): Nullable<number>

    Read synchronously some data into a buffer and returns the number of bytes read.

T
replace_options = { entities?: boolean; custom?: (args: { name: string; key: Nullable<string>; value: Nullable<string>; node: Readonly<xml_node>; }) => unknown; }

XML stringifier stringify_options.replace

  • custom: (args: { name: string; key: Nullable<string>; value: Nullable<string>; node: Readonly<xml_node>; }) => unknown

    Custom replacer (this is applied after other revivals). When it is applied on an attribute, key and value will be given. When it is applied on a node, both key and value will be null. Return undefined to delete either the attribute or the tag.

  • entities: boolean

    Force escape all XML entities. By default, only the ones that would break the XML structure are escaped.

T
revive_options = { trim?: boolean; entities?: boolean; booleans?: boolean; numbers?: boolean; custom?: reviver; }

XML parser parse_options.revive

  • booleans: boolean

    Revive booleans (matching /^(?:[Tt]rue|[Ff]alse)$/).

  • custom: reviver

    Custom reviver (this is applied after other revivals). When it is applied on an attribute, key and value will be given. When it is applied on a node, both key and value will be null. Return undefined to delete either the attribute or the tag.

  • entities: boolean

    Revive XML entities (defaults to true). Automatically unescape XML entities and replace common entities with their respective characters.

  • numbers: boolean

    Revive finite numbers. Note that the version of the XML prolog is always treated as a string to avoid breaking documents.

  • trim: boolean

    Trim texts (this is applied before other revivals, defaults to true). It honors xml:space="preserve" attribute.

T
reviver = (args: { name: string; key: Nullable<string>; value: Nullable<string>; node: Readonly<xml_node>; }) => unknown

Custom XML parser reviver. It can be used to change the way some nodes are parsed.

T
stringify_options = { format?: format_options; replace?: replace_options; }

XML stringifier options.

  • format: format_options

    Format options.

  • replace: replace_options

    Replace options.

T
stringifyable = Partial<
Omit<xml_document, "@version" | "@standalone">
& { @version: string; @standalone: string; }
>

A laxer type for what can be stringified. We won’t ever create this, but we’ll accept it.

T
xml_document =
xml_node
& { [@version]?: `1.${number}`; [@encoding]?: string; [@standalone]?: "yes" | "no"; [#doctype]?: xml_node; [#instructions]?: { [key: string]: xml_node | Array<xml_node>; }; }

XML document.

T
xml_node = { [key: string]: unknown; readonly [~name]: string; readonly [~children]: Array<xml_node | xml_text>; readonly [#comments]?: Array<string>; readonly [#text]: string; }

XML node.

  • #comments: Array<string>

    Comments.

  • #text: string

    Text content.

  • ~children: Array<xml_node | xml_text>

    Child nodes.

  • ~name: string

    Tag name (~xml for XML prolog, other nodes will be given their respective tag name).

T
xml_text = { readonly [~parent]: xml_node; readonly [~name]: string; [#text]: string; }

XML text node.

  • #text: string

    Text content.

  • ~name: string

    Tag name (~text for text nodes, ~cdata for CDATA nodes and ~comment for comment nodes).

  • ~parent: xml_node

    Parent node.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@libs/xml

Import symbol

import * as mod from "@libs/xml";
or

Import directly with a jsr specifier

import * as mod from "jsr:@libs/xml";