Skip to content

stax-xml

stax-xml


Declarative XML Converter Module

This module provides a zod-style declarative API for parsing XML documents. It allows you to define XML schemas using a fluent API and parse XML with XPath support.

Basic usage:

import { x } from 'stax-xml/converter';
const schema = x.object({
title: x.string().xpath('/book/title'),
author: x.string().xpath('/book/author'),
price: x.number().xpath('/book/price')
});
const xml = '<book><title>TypeScript</title><author>John</author><price>29.99</price></book>';
const result = await schema.parse(xml);
// { title: 'TypeScript', author: 'John', price: 29.99 }

Defined in: Writer.ts:131

High-performance asynchronous XML writer implementing the StAX (Streaming API for XML) pattern.

This writer provides efficient streaming XML generation using WritableStream for handling large XML documents with automatic buffering, backpressure management, and namespace support.

This is an optimized implementation with:

  • Optimization 1: Regex caching for entity escaping
  • Optimization 2: Attribute string batching
  • Optimization 3: Early entity check before regex execution
  • Optimization 4: Qualified closing-tag stack (avoid rebuilding end tags)
  • Optimization 5: Copy-on-write namespace frames
  • Optimization 6: Indentation cache for pretty-print output
  • Optimization 7: TextEncoder.encodeInto() buffering to reduce intermediate byte arrays
  • Optimization 8: Flush by buffer view to avoid per-flush copy slices

The writer supports streaming output with configurable buffering, automatic entity encoding, pretty printing with customizable indentation, and comprehensive namespace handling.

Basic usage:

const writableStream = new WritableStream({
write(chunk) {
console.log(new TextDecoder().decode(chunk));
}
});
const writer = new Writer(writableStream);
await writer.writeStartElement('root');
await writer.writeElement('item', { id: '1' }, 'Hello World');
await writer.writeEndElement();
await writer.close();

With pretty printing:

const options = {
prettyPrint: true,
indentString: ' ',
autoEncodeEntities: true
};
const writer = new Writer(writableStream, options);

new Writer(stream, options?): Writer

Defined in: Writer.ts:170

WritableStream<Uint8Array<ArrayBufferLike>>

WriterOptions = {}

Writer

writeStartDocument(version?, encoding?): Promise<Writer>

Defined in: Writer.ts:292

Write XML declaration

string = '1.0'

string

Promise<Writer>

writeEndDocument(): Promise<void>

Defined in: Writer.ts:317

End document (automatically close all elements)

Promise<void>

writeStartElement(localName, options?): Promise<Writer>

Defined in: Writer.ts:338

Write start element

string

WriteElementOptions

Promise<Writer>

writeEndElement(): Promise<Writer>

Defined in: Writer.ts:430

Write end element

Promise<Writer>

writeCharacters(text): Promise<Writer>

Defined in: Writer.ts:463

Write text

string

Promise<Writer>

writeCData(cdata): Promise<Writer>

Defined in: Writer.ts:485

Write CDATA section

string

Promise<Writer>

writeComment(comment): Promise<Writer>

Defined in: Writer.ts:505

Write comment

string

Promise<Writer>

writeRaw(xml): Promise<Writer>

Defined in: Writer.ts:528

Write raw XML content without escaping

string

Raw XML string to write

Promise<Writer>

this (chainable)

flush(): Promise<void>

Defined in: Writer.ts:537

Manual flush

Promise<void>

getMetrics(): object

Defined in: Writer.ts:544

Return metrics

object

totalBytesWritten: number = 0

flushCount: number = 0

lastFlushTime: number = 0

bufferUtilization: number

averageFlushSize: number


Defined in: WriterSync.ts:493

String-based sync writer.

  • AbstractWriterSync

new WriterSync(options?): WriterSync

Defined in: WriterSync.ts:496

WriterSyncOptions = {}

WriterSync

AbstractWriterSync.constructor

protected state: number = WriterState.INITIAL

Defined in: WriterSync.ts:78

AbstractWriterSync.state

protected elementStack: string[] = []

Defined in: WriterSync.ts:79

AbstractWriterSync.elementStack

protected hasTextContentStack: boolean[] = []

Defined in: WriterSync.ts:80

AbstractWriterSync.hasTextContentStack

protected namespaceStack: Map<string, string>[] = []

Defined in: WriterSync.ts:81

AbstractWriterSync.namespaceStack

protected namespaceOwnedStack: boolean[] = []

Defined in: WriterSync.ts:82

AbstractWriterSync.namespaceOwnedStack

protected readonly options: Required<WriterSyncOptions>

Defined in: WriterSync.ts:83

AbstractWriterSync.options

protected currentIndentLevel: number = 0

Defined in: WriterSync.ts:84

AbstractWriterSync.currentIndentLevel

protected needsIndent: boolean = false

Defined in: WriterSync.ts:85

AbstractWriterSync.needsIndent

protected indentCache: string[]

Defined in: WriterSync.ts:86

AbstractWriterSync.indentCache

writeStartDocument(version?, encoding?): this

Defined in: WriterSync.ts:133

Writes the XML declaration (e.g., ).

string = '1.0'

string

this

AbstractWriterSync.writeStartDocument

writeEndDocument(): void

Defined in: WriterSync.ts:158

Indicates the end of the document and automatically closes all open elements.

void

AbstractWriterSync.writeEndDocument

writeStartElement(localName, options?): this

Defined in: WriterSync.ts:170

string

WriteElementOptions

this

AbstractWriterSync.writeStartElement

writeAttribute(localName, value, prefix?): this

Defined in: WriterSync.ts:249

string

string

string

this

AbstractWriterSync.writeAttribute

writeNamespace(prefix, uri): this

Defined in: WriterSync.ts:259

string

string

this

AbstractWriterSync.writeNamespace

writeCharacters(text): this

Defined in: WriterSync.ts:276

string

this

AbstractWriterSync.writeCharacters

writeCData(cdata): this

Defined in: WriterSync.ts:290

string

this

AbstractWriterSync.writeCData

writeComment(comment): this

Defined in: WriterSync.ts:307

string

this

AbstractWriterSync.writeComment

writeProcessingInstruction(target, data?): this

Defined in: WriterSync.ts:322

string

string

this

AbstractWriterSync.writeProcessingInstruction

writeRaw(xml): this

Defined in: WriterSync.ts:344

string

this

AbstractWriterSync.writeRaw

writeEndElement(): this

Defined in: WriterSync.ts:350

this

AbstractWriterSync.writeEndElement

setPrettyPrint(enabled): this

Defined in: WriterSync.ts:382

boolean

this

AbstractWriterSync.setPrettyPrint

setIndentString(indentString): this

Defined in: WriterSync.ts:387

string

this

AbstractWriterSync.setIndentString

isPrettyPrintEnabled(): boolean

Defined in: WriterSync.ts:393

boolean

AbstractWriterSync.isPrettyPrintEnabled

getIndentString(): string

Defined in: WriterSync.ts:397

string

AbstractWriterSync.getIndentString

protected _closeStartElementTag(): void

Defined in: WriterSync.ts:419

void

AbstractWriterSync._closeStartElementTag

protected _writeNewline(): void

Defined in: WriterSync.ts:437

void

AbstractWriterSync._writeNewline

getXmlString(): string

Defined in: WriterSync.ts:500

string

protected _emit(chunk): void

Defined in: WriterSync.ts:504

string

void

AbstractWriterSync._emit


Defined in: WriterSync.ts:512

Sink-based sync writer. Use this for file/buffer incremental writes.

  • AbstractWriterSync

new WriterSyncSink(sink, options?): WriterSyncSink

Defined in: WriterSync.ts:520

SyncTextSink

WriterSyncSinkOptions = {}

WriterSyncSink

AbstractWriterSync.constructor

protected state: number = WriterState.INITIAL

Defined in: WriterSync.ts:78

AbstractWriterSync.state

protected elementStack: string[] = []

Defined in: WriterSync.ts:79

AbstractWriterSync.elementStack

protected hasTextContentStack: boolean[] = []

Defined in: WriterSync.ts:80

AbstractWriterSync.hasTextContentStack

protected namespaceStack: Map<string, string>[] = []

Defined in: WriterSync.ts:81

AbstractWriterSync.namespaceStack

protected namespaceOwnedStack: boolean[] = []

Defined in: WriterSync.ts:82

AbstractWriterSync.namespaceOwnedStack

protected readonly options: Required<WriterSyncOptions>

Defined in: WriterSync.ts:83

AbstractWriterSync.options

protected currentIndentLevel: number = 0

Defined in: WriterSync.ts:84

AbstractWriterSync.currentIndentLevel

protected needsIndent: boolean = false

Defined in: WriterSync.ts:85

AbstractWriterSync.needsIndent

protected indentCache: string[]

Defined in: WriterSync.ts:86

AbstractWriterSync.indentCache

writeStartDocument(version?, encoding?): this

Defined in: WriterSync.ts:133

Writes the XML declaration (e.g., ).

string = '1.0'

string

this

AbstractWriterSync.writeStartDocument

writeStartElement(localName, options?): this

Defined in: WriterSync.ts:170

string

WriteElementOptions

this

AbstractWriterSync.writeStartElement

writeAttribute(localName, value, prefix?): this

Defined in: WriterSync.ts:249

string

string

string

this

AbstractWriterSync.writeAttribute

writeNamespace(prefix, uri): this

Defined in: WriterSync.ts:259

string

string

this

AbstractWriterSync.writeNamespace

writeCharacters(text): this

Defined in: WriterSync.ts:276

string

this

AbstractWriterSync.writeCharacters

writeCData(cdata): this

Defined in: WriterSync.ts:290

string

this

AbstractWriterSync.writeCData

writeComment(comment): this

Defined in: WriterSync.ts:307

string

this

AbstractWriterSync.writeComment

writeProcessingInstruction(target, data?): this

Defined in: WriterSync.ts:322

string

string

this

AbstractWriterSync.writeProcessingInstruction

writeRaw(xml): this

Defined in: WriterSync.ts:344

string

this

AbstractWriterSync.writeRaw

writeEndElement(): this

Defined in: WriterSync.ts:350

this

AbstractWriterSync.writeEndElement

setPrettyPrint(enabled): this

Defined in: WriterSync.ts:382

boolean

this

AbstractWriterSync.setPrettyPrint

setIndentString(indentString): this

Defined in: WriterSync.ts:387

string

this

AbstractWriterSync.setIndentString

isPrettyPrintEnabled(): boolean

Defined in: WriterSync.ts:393

boolean

AbstractWriterSync.isPrettyPrintEnabled

getIndentString(): string

Defined in: WriterSync.ts:397

string

AbstractWriterSync.getIndentString

protected _closeStartElementTag(): void

Defined in: WriterSync.ts:419

void

AbstractWriterSync._closeStartElementTag

protected _writeNewline(): void

Defined in: WriterSync.ts:437

void

AbstractWriterSync._writeNewline

protected _emit(chunk): void

Defined in: WriterSync.ts:536

string

void

AbstractWriterSync._emit

writeEndDocument(): void

Defined in: WriterSync.ts:577

Indicates the end of the document and automatically closes all open elements.

void

AbstractWriterSync.writeEndDocument

flush(): void

Defined in: WriterSync.ts:585

void

close(): void

Defined in: WriterSync.ts:592

void


Defined in: converter/XmlArraySchema.ts:16

Schema for parsing XML array values

T extends XmlSchemaBase<unknown, unknown>

new XmlArraySchema<T>(element, xpath?): XmlArraySchema<T>

Defined in: converter/XmlArraySchema.ts:19

T

string

XmlArraySchema<T>

XmlSchemaBase.constructor

readonly element: T

Defined in: converter/XmlArraySchema.ts:20

readonly optional xpath?: string

Defined in: converter/XmlArraySchema.ts:21

readonly _output: T["_output"][]

Defined in: converter/base.ts:31

XmlSchemaBase._output

readonly _input: T["_input"][]

Defined in: converter/base.ts:32

XmlSchemaBase._input

_parse(input, parseOptions?): T["_output"][]

Defined in: converter/XmlArraySchema.ts:26

Parse XML input synchronously

ParseInput

XML string, byte view, or sync iterator

ParseOptions

T["_output"][]

Parsed output

If parsing fails

XmlSchemaBase._parse

_parseAsync(input, parseOptions?): Promise<T["_output"][]>

Defined in: converter/XmlArraySchema.ts:31

Parse XML input asynchronously

ParseInput

XML string, stream, or async iterator

ParseOptions

Promise<T["_output"][]>

Parsed output

If parsing fails

XmlSchemaBase._parseAsync

parse(input, options?): Promise<T["_output"][]>

Defined in: converter/base.ts:117

Parse XML asynchronously (public API)

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<T["_output"][]>

Parsed output

If parsing fails

XmlSchemaBase.parse

parseSync(input, options?): T["_output"][]

Defined in: converter/base.ts:135

Parse XML synchronously (public API)

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string or sync iterator

ParseOptions

Parse options

T["_output"][]

Parsed output

If parsing fails

XmlSchemaBase.parseSync

safeParse(input, options?): Promise<ParseResult<T["_output"][]>>

Defined in: converter/base.ts:152

Parse XML asynchronously with error handling

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<ParseResult<T["_output"][]>>

Parse result with success flag

XmlSchemaBase.safeParse

safeParseSync(input, options?): ParseResult<T["_output"][]>

Defined in: converter/base.ts:173

Parse XML synchronously with error handling

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string, byte view, or sync iterator

ParseOptions

Parse options

ParseResult<T["_output"][]>

Parse result with success flag

XmlSchemaBase.safeParseSync

transform<NewOutput>(fn): XmlSchemaBase<NewOutput, T["_input"][]>

Defined in: converter/base.ts:193

Transform the parsed output

NewOutput

(value) => NewOutput

Transform function

XmlSchemaBase<NewOutput, T["_input"][]>

New schema with transform applied

XmlSchemaBase.transform

optional(): XmlSchemaBase<T["_output"][] | undefined, T["_input"][] | undefined>

Defined in: converter/base.ts:201

Make this schema optional

XmlSchemaBase<T["_output"][] | undefined, T["_input"][] | undefined>

New optional schema

XmlSchemaBase.optional

array(xpath?): XmlSchemaBase<T["_output"][][], T["_input"][][]>

Defined in: converter/base.ts:210

Convert this schema to an array schema

string

XPath expression for array elements

XmlSchemaBase<T["_output"][][], T["_input"][][]>

New array schema

XmlSchemaBase.array

compile(): XmlSchemaBase<T["_output"][], T["_input"][]>

Defined in: converter/base.ts:238

Compile this schema for repeated parsing.

XmlSchemaBase<T["_output"][], T["_input"][]>

New compiled schema

compile() preserves the public parsing API and can speed up schemas that can be lowered to fixed XML event dispatch. The optimized path works best when the root schema is an object, array, string, or number with static XPath selectors.

Fast-path friendly selectors use absolute paths such as /catalog/book, descendant paths such as //book, and relative selectors inside object or array items such as ./title, ./@id, ./name/text(), or ./name/@code. Object fields, arrays of scalar values, arrays of objects, nested objects, optional fields, and transforms are supported.

Selectors with wildcards or predicates, ambiguous relative paths such as title, nested arrays, and arrays that combine an array XPath with an element XPath are parsed with the normal runtime converter path instead. This keeps behavior compatible, but does not get the dispatch fast path.

Call compile() once on the root schema and reuse the returned schema. Non-object root schemas need an XPath.

XmlSchemaBase.compile

write(data, options?): Promise<string>

Defined in: converter/base.ts:249

Write data to XML string asynchronously (public API)

T["_output"][]

Data to write

XmlWriteOptions

Write options

Promise<string>

XML string

XmlSchemaBase.write

writeToStream(data, stream, options?): Promise<void>

Defined in: converter/base.ts:272

Write data to WritableStream asynchronously (public API)

T["_output"][]

Data to write

WritableStream<Uint8Array<ArrayBufferLike>>

Writable stream to write to

XmlWriteOptions

Write options

Promise<void>

XmlSchemaBase.writeToStream

writeSync(data, options?): string

Defined in: converter/base.ts:286

Write data to XML string synchronously (public API)

T["_output"][]

Data to write

XmlWriteOptions

Write options

string

XML string

XmlSchemaBase.writeSync

writer(config): this

Defined in: converter/base.ts:295

Configure writer settings for this schema

XmlElementWriteConfig

Writer configuration

this

This schema with writer config

XmlSchemaBase.writer


Defined in: converter/XmlBuilder.ts:13

Builder API for creating XML schemas

new XmlBuilder(): XmlBuilder

XmlBuilder

string(xpath?): XmlStringSchema

Defined in: converter/XmlBuilder.ts:19

Create a string schema

string

Optional XPath expression

XmlStringSchema

String schema

number(xpath?): XmlNumberSchema

Defined in: converter/XmlBuilder.ts:28

Create a number schema

string

Optional XPath expression

XmlNumberSchema

Number schema

object<T>(shape, options?): XmlObjectSchema<T>

Defined in: converter/XmlBuilder.ts:38

Create an object schema

T extends XmlObjectShape

T

Object shape definition

XmlObjectOptions

Optional object options

XmlObjectSchema<T>

Object schema

array<T>(element, xpath?): XmlArraySchema<T>

Defined in: converter/XmlBuilder.ts:48

Create an array schema

T extends XmlSchema<unknown, unknown>

T

Element schema

string

XPath expression for array elements

XmlArraySchema<T>

Array schema


Defined in: converter/XmlNumberSchema.ts:16

Schema for parsing XML number values

new XmlNumberSchema(options?): XmlNumberSchema

Defined in: converter/XmlNumberSchema.ts:19

XmlNumberOptions = {}

XmlNumberSchema

XmlSchema.constructor

options: XmlNumberOptions = {}

Defined in: converter/XmlNumberSchema.ts:19

readonly _output: number

Defined in: converter/base.ts:31

XmlSchema._output

readonly _input: number

Defined in: converter/base.ts:32

XmlSchema._input

_parse(input, parseOptions?): number

Defined in: converter/XmlNumberSchema.ts:23

Parse XML input synchronously

ParseInput

XML string, byte view, or sync iterator

ParseOptions

number

Parsed output

If parsing fails

XmlSchema._parse

_parseAsync(input, parseOptions?): Promise<number>

Defined in: converter/XmlNumberSchema.ts:29

Parse XML input asynchronously

ParseInput

XML string, stream, or async iterator

ParseOptions

Promise<number>

Parsed output

If parsing fails

XmlSchema._parseAsync

xpath(path): XmlNumberSchema

Defined in: converter/XmlNumberSchema.ts:158

Set XPath expression for locating the element

string

XPath expression

XmlNumberSchema

New schema with XPath

min(value): XmlNumberSchema

Defined in: converter/XmlNumberSchema.ts:171

Set minimum value

number

Minimum value

XmlNumberSchema

New schema with minimum

max(value): XmlNumberSchema

Defined in: converter/XmlNumberSchema.ts:180

Set maximum value

number

Maximum value

XmlNumberSchema

New schema with maximum

int(): XmlNumberSchema

Defined in: converter/XmlNumberSchema.ts:188

Require integer value

XmlNumberSchema

New schema that only accepts integers

parse(input, options?): Promise<number>

Defined in: converter/base.ts:117

Parse XML asynchronously (public API)

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<number>

Parsed output

If parsing fails

XmlSchema.parse

parseSync(input, options?): number

Defined in: converter/base.ts:135

Parse XML synchronously (public API)

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string or sync iterator

ParseOptions

Parse options

number

Parsed output

If parsing fails

XmlSchema.parseSync

safeParse(input, options?): Promise<ParseResult<number>>

Defined in: converter/base.ts:152

Parse XML asynchronously with error handling

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<ParseResult<number>>

Parse result with success flag

XmlSchema.safeParse

safeParseSync(input, options?): ParseResult<number>

Defined in: converter/base.ts:173

Parse XML synchronously with error handling

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string, byte view, or sync iterator

ParseOptions

Parse options

ParseResult<number>

Parse result with success flag

XmlSchema.safeParseSync

transform<NewOutput>(fn): XmlSchemaBase<NewOutput, number>

Defined in: converter/base.ts:193

Transform the parsed output

NewOutput

(value) => NewOutput

Transform function

XmlSchemaBase<NewOutput, number>

New schema with transform applied

XmlSchema.transform

optional(): XmlSchemaBase<number | undefined, number | undefined>

Defined in: converter/base.ts:201

Make this schema optional

XmlSchemaBase<number | undefined, number | undefined>

New optional schema

XmlSchema.optional

array(xpath?): XmlSchemaBase<number[], number[]>

Defined in: converter/base.ts:210

Convert this schema to an array schema

string

XPath expression for array elements

XmlSchemaBase<number[], number[]>

New array schema

XmlSchema.array

compile(): XmlSchemaBase<number, number>

Defined in: converter/base.ts:238

Compile this schema for repeated parsing.

XmlSchemaBase<number, number>

New compiled schema

compile() preserves the public parsing API and can speed up schemas that can be lowered to fixed XML event dispatch. The optimized path works best when the root schema is an object, array, string, or number with static XPath selectors.

Fast-path friendly selectors use absolute paths such as /catalog/book, descendant paths such as //book, and relative selectors inside object or array items such as ./title, ./@id, ./name/text(), or ./name/@code. Object fields, arrays of scalar values, arrays of objects, nested objects, optional fields, and transforms are supported.

Selectors with wildcards or predicates, ambiguous relative paths such as title, nested arrays, and arrays that combine an array XPath with an element XPath are parsed with the normal runtime converter path instead. This keeps behavior compatible, but does not get the dispatch fast path.

Call compile() once on the root schema and reuse the returned schema. Non-object root schemas need an XPath.

XmlSchema.compile

write(data, options?): Promise<string>

Defined in: converter/base.ts:249

Write data to XML string asynchronously (public API)

number

Data to write

XmlWriteOptions

Write options

Promise<string>

XML string

XmlSchema.write

writeToStream(data, stream, options?): Promise<void>

Defined in: converter/base.ts:272

Write data to WritableStream asynchronously (public API)

number

Data to write

WritableStream<Uint8Array<ArrayBufferLike>>

Writable stream to write to

XmlWriteOptions

Write options

Promise<void>

XmlSchema.writeToStream

writeSync(data, options?): string

Defined in: converter/base.ts:286

Write data to XML string synchronously (public API)

number

Data to write

XmlWriteOptions

Write options

string

XML string

XmlSchema.writeSync

writer(config): this

Defined in: converter/base.ts:295

Configure writer settings for this schema

XmlElementWriteConfig

Writer configuration

this

This schema with writer config

XmlSchema.writer


Defined in: converter/XmlObjectSchema.ts:58

Schema for parsing XML object values

T extends XmlObjectShape

new XmlObjectSchema<T>(shape, options?): XmlObjectSchema<T>

Defined in: converter/XmlObjectSchema.ts:61

T

XmlObjectOptions = {}

XmlObjectSchema<T>

XmlSchema.constructor

readonly shape: T

Defined in: converter/XmlObjectSchema.ts:62

options: XmlObjectOptions = {}

Defined in: converter/XmlObjectSchema.ts:63

readonly _output: Output

Defined in: converter/base.ts:31

XmlSchema._output

readonly _input: unknown

Defined in: converter/base.ts:32

XmlSchema._input

_parse(input, parseOptions?): InferObjectOutput<T>

Defined in: converter/XmlObjectSchema.ts:68

Parse XML input synchronously

ParseInput

XML string, byte view, or sync iterator

ParseOptions

InferObjectOutput<T>

Parsed output

If parsing fails

XmlSchema._parse

_parseAsync(input, parseOptions?): Promise<InferObjectOutput<T>>

Defined in: converter/XmlObjectSchema.ts:73

Parse XML input asynchronously

ParseInput

XML string, stream, or async iterator

ParseOptions

Promise<InferObjectOutput<T>>

Parsed output

If parsing fails

XmlSchema._parseAsync

xpath(path): XmlObjectSchema<T>

Defined in: converter/XmlObjectSchema.ts:126

Set XPath expression for locating the object

string

XPath expression

XmlObjectSchema<T>

New schema with XPath

parse(input, options?): Promise<InferObjectOutput<T>>

Defined in: converter/base.ts:117

Parse XML asynchronously (public API)

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<InferObjectOutput<T>>

Parsed output

If parsing fails

XmlSchema.parse

parseSync(input, options?): Output

Defined in: converter/base.ts:135

Parse XML synchronously (public API)

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string or sync iterator

ParseOptions

Parse options

Output

Parsed output

If parsing fails

XmlSchema.parseSync

safeParse(input, options?): Promise<ParseResult<InferObjectOutput<T>>>

Defined in: converter/base.ts:152

Parse XML asynchronously with error handling

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<ParseResult<InferObjectOutput<T>>>

Parse result with success flag

XmlSchema.safeParse

safeParseSync(input, options?): ParseResult<InferObjectOutput<T>>

Defined in: converter/base.ts:173

Parse XML synchronously with error handling

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string, byte view, or sync iterator

ParseOptions

Parse options

ParseResult<InferObjectOutput<T>>

Parse result with success flag

XmlSchema.safeParseSync

transform<NewOutput>(fn): XmlSchemaBase<NewOutput, unknown>

Defined in: converter/base.ts:193

Transform the parsed output

NewOutput

(value) => NewOutput

Transform function

XmlSchemaBase<NewOutput, unknown>

New schema with transform applied

XmlSchema.transform

optional(): XmlSchemaBase<InferObjectOutput<T> | undefined, unknown>

Defined in: converter/base.ts:201

Make this schema optional

XmlSchemaBase<InferObjectOutput<T> | undefined, unknown>

New optional schema

XmlSchema.optional

array(xpath?): XmlSchemaBase<InferObjectOutput<T>[], unknown[]>

Defined in: converter/base.ts:210

Convert this schema to an array schema

string

XPath expression for array elements

XmlSchemaBase<InferObjectOutput<T>[], unknown[]>

New array schema

XmlSchema.array

compile(): XmlSchemaBase<InferObjectOutput<T>, unknown>

Defined in: converter/base.ts:238

Compile this schema for repeated parsing.

XmlSchemaBase<InferObjectOutput<T>, unknown>

New compiled schema

compile() preserves the public parsing API and can speed up schemas that can be lowered to fixed XML event dispatch. The optimized path works best when the root schema is an object, array, string, or number with static XPath selectors.

Fast-path friendly selectors use absolute paths such as /catalog/book, descendant paths such as //book, and relative selectors inside object or array items such as ./title, ./@id, ./name/text(), or ./name/@code. Object fields, arrays of scalar values, arrays of objects, nested objects, optional fields, and transforms are supported.

Selectors with wildcards or predicates, ambiguous relative paths such as title, nested arrays, and arrays that combine an array XPath with an element XPath are parsed with the normal runtime converter path instead. This keeps behavior compatible, but does not get the dispatch fast path.

Call compile() once on the root schema and reuse the returned schema. Non-object root schemas need an XPath.

XmlSchema.compile

write(data, options?): Promise<string>

Defined in: converter/base.ts:249

Write data to XML string asynchronously (public API)

Output

Data to write

XmlWriteOptions

Write options

Promise<string>

XML string

XmlSchema.write

writeToStream(data, stream, options?): Promise<void>

Defined in: converter/base.ts:272

Write data to WritableStream asynchronously (public API)

Output

Data to write

WritableStream<Uint8Array<ArrayBufferLike>>

Writable stream to write to

XmlWriteOptions

Write options

Promise<void>

XmlSchema.writeToStream

writeSync(data, options?): string

Defined in: converter/base.ts:286

Write data to XML string synchronously (public API)

Output

Data to write

XmlWriteOptions

Write options

string

XML string

XmlSchema.writeSync

writer(config): this

Defined in: converter/base.ts:295

Configure writer settings for this schema

XmlElementWriteConfig

Writer configuration

this

This schema with writer config

XmlSchema.writer


Defined in: converter/XmlOptionalSchema.ts:10

Schema for optional values

  • XmlSchemaBase<T["_output"] | undefined, T["_input"] | undefined>

T extends XmlSchemaBase<unknown, unknown>

new XmlOptionalSchema<T>(schema): XmlOptionalSchema<T>

Defined in: converter/XmlOptionalSchema.ts:13

T

XmlOptionalSchema<T>

XmlSchemaBase.constructor

readonly schema: T

Defined in: converter/XmlOptionalSchema.ts:13

readonly _output: T["_output"] | undefined

Defined in: converter/base.ts:31

XmlSchemaBase._output

readonly _input: T["_input"] | undefined

Defined in: converter/base.ts:32

XmlSchemaBase._input

_parse(input, options?): T["_output"] | undefined

Defined in: converter/XmlOptionalSchema.ts:17

Parse XML input synchronously

ParseInput

XML string, byte view, or sync iterator

ParseOptions

Parse options

T["_output"] | undefined

Parsed output

If parsing fails

XmlSchemaBase._parse

_parseAsync(input, options?): Promise<T["_output"] | undefined>

Defined in: converter/XmlOptionalSchema.ts:30

Parse XML input asynchronously

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<T["_output"] | undefined>

Parsed output

If parsing fails

XmlSchemaBase._parseAsync

parse(input, options?): Promise<T["_output"] | undefined>

Defined in: converter/base.ts:117

Parse XML asynchronously (public API)

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<T["_output"] | undefined>

Parsed output

If parsing fails

XmlSchemaBase.parse

parseSync(input, options?): T["_output"] | undefined

Defined in: converter/base.ts:135

Parse XML synchronously (public API)

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string or sync iterator

ParseOptions

Parse options

T["_output"] | undefined

Parsed output

If parsing fails

XmlSchemaBase.parseSync

safeParse(input, options?): Promise<ParseResult<T["_output"] | undefined>>

Defined in: converter/base.ts:152

Parse XML asynchronously with error handling

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<ParseResult<T["_output"] | undefined>>

Parse result with success flag

XmlSchemaBase.safeParse

safeParseSync(input, options?): ParseResult<T["_output"] | undefined>

Defined in: converter/base.ts:173

Parse XML synchronously with error handling

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string, byte view, or sync iterator

ParseOptions

Parse options

ParseResult<T["_output"] | undefined>

Parse result with success flag

XmlSchemaBase.safeParseSync

transform<NewOutput>(fn): XmlSchemaBase<NewOutput, T["_input"] | undefined>

Defined in: converter/base.ts:193

Transform the parsed output

NewOutput

(value) => NewOutput

Transform function

XmlSchemaBase<NewOutput, T["_input"] | undefined>

New schema with transform applied

XmlSchemaBase.transform

optional(): XmlSchemaBase<T["_output"] | undefined, T["_input"] | undefined>

Defined in: converter/base.ts:201

Make this schema optional

XmlSchemaBase<T["_output"] | undefined, T["_input"] | undefined>

New optional schema

XmlSchemaBase.optional

array(xpath?): XmlSchemaBase<(T["_output"] | undefined)[], (T["_input"] | undefined)[]>

Defined in: converter/base.ts:210

Convert this schema to an array schema

string

XPath expression for array elements

XmlSchemaBase<(T["_output"] | undefined)[], (T["_input"] | undefined)[]>

New array schema

XmlSchemaBase.array

compile(): XmlSchemaBase<T["_output"] | undefined, T["_input"] | undefined>

Defined in: converter/base.ts:238

Compile this schema for repeated parsing.

XmlSchemaBase<T["_output"] | undefined, T["_input"] | undefined>

New compiled schema

compile() preserves the public parsing API and can speed up schemas that can be lowered to fixed XML event dispatch. The optimized path works best when the root schema is an object, array, string, or number with static XPath selectors.

Fast-path friendly selectors use absolute paths such as /catalog/book, descendant paths such as //book, and relative selectors inside object or array items such as ./title, ./@id, ./name/text(), or ./name/@code. Object fields, arrays of scalar values, arrays of objects, nested objects, optional fields, and transforms are supported.

Selectors with wildcards or predicates, ambiguous relative paths such as title, nested arrays, and arrays that combine an array XPath with an element XPath are parsed with the normal runtime converter path instead. This keeps behavior compatible, but does not get the dispatch fast path.

Call compile() once on the root schema and reuse the returned schema. Non-object root schemas need an XPath.

XmlSchemaBase.compile

write(data, options?): Promise<string>

Defined in: converter/base.ts:249

Write data to XML string asynchronously (public API)

T["_output"] | undefined

Data to write

XmlWriteOptions

Write options

Promise<string>

XML string

XmlSchemaBase.write

writeToStream(data, stream, options?): Promise<void>

Defined in: converter/base.ts:272

Write data to WritableStream asynchronously (public API)

T["_output"] | undefined

Data to write

WritableStream<Uint8Array<ArrayBufferLike>>

Writable stream to write to

XmlWriteOptions

Write options

Promise<void>

XmlSchemaBase.writeToStream

writeSync(data, options?): string

Defined in: converter/base.ts:286

Write data to XML string synchronously (public API)

T["_output"] | undefined

Data to write

XmlWriteOptions

Write options

string

XML string

XmlSchemaBase.writeSync

writer(config): this

Defined in: converter/base.ts:295

Configure writer settings for this schema

XmlElementWriteConfig

Writer configuration

this

This schema with writer config

XmlSchemaBase.writer


Defined in: converter/XmlSchema.ts:10

Main XML schema class (extends XmlSchemaBase with all methods)

Output

Input = Output

new XmlSchema<Output, Input>(): XmlSchema<Output, Input>

XmlSchema<Output, Input>

XmlSchemaBase.constructor

readonly _output: Output

Defined in: converter/base.ts:31

XmlSchemaBase._output

readonly _input: Input

Defined in: converter/base.ts:32

XmlSchemaBase._input

abstract _parse(input, options?): Output

Defined in: converter/base.ts:53

Parse XML input synchronously

ParseInput

XML string, byte view, or sync iterator

ParseOptions

Parse options

Output

Parsed output

If parsing fails

XmlSchemaBase._parse

abstract _parseAsync(input, options?): Promise<Output>

Defined in: converter/base.ts:62

Parse XML input asynchronously

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<Output>

Parsed output

If parsing fails

XmlSchemaBase._parseAsync

parse(input, options?): Promise<Output>

Defined in: converter/base.ts:117

Parse XML asynchronously (public API)

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<Output>

Parsed output

If parsing fails

XmlSchemaBase.parse

parseSync(input, options?): Output

Defined in: converter/base.ts:135

Parse XML synchronously (public API)

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string or sync iterator

ParseOptions

Parse options

Output

Parsed output

If parsing fails

XmlSchemaBase.parseSync

safeParse(input, options?): Promise<ParseResult<Output>>

Defined in: converter/base.ts:152

Parse XML asynchronously with error handling

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<ParseResult<Output>>

Parse result with success flag

XmlSchemaBase.safeParse

safeParseSync(input, options?): ParseResult<Output>

Defined in: converter/base.ts:173

Parse XML synchronously with error handling

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string, byte view, or sync iterator

ParseOptions

Parse options

ParseResult<Output>

Parse result with success flag

XmlSchemaBase.safeParseSync

transform<NewOutput>(fn): XmlSchemaBase<NewOutput, Input>

Defined in: converter/base.ts:193

Transform the parsed output

NewOutput

(value) => NewOutput

Transform function

XmlSchemaBase<NewOutput, Input>

New schema with transform applied

XmlSchemaBase.transform

optional(): XmlSchemaBase<Output | undefined, Input | undefined>

Defined in: converter/base.ts:201

Make this schema optional

XmlSchemaBase<Output | undefined, Input | undefined>

New optional schema

XmlSchemaBase.optional

array(xpath?): XmlSchemaBase<Output[], Input[]>

Defined in: converter/base.ts:210

Convert this schema to an array schema

string

XPath expression for array elements

XmlSchemaBase<Output[], Input[]>

New array schema

XmlSchemaBase.array

compile(): XmlSchemaBase<Output, Input>

Defined in: converter/base.ts:238

Compile this schema for repeated parsing.

XmlSchemaBase<Output, Input>

New compiled schema

compile() preserves the public parsing API and can speed up schemas that can be lowered to fixed XML event dispatch. The optimized path works best when the root schema is an object, array, string, or number with static XPath selectors.

Fast-path friendly selectors use absolute paths such as /catalog/book, descendant paths such as //book, and relative selectors inside object or array items such as ./title, ./@id, ./name/text(), or ./name/@code. Object fields, arrays of scalar values, arrays of objects, nested objects, optional fields, and transforms are supported.

Selectors with wildcards or predicates, ambiguous relative paths such as title, nested arrays, and arrays that combine an array XPath with an element XPath are parsed with the normal runtime converter path instead. This keeps behavior compatible, but does not get the dispatch fast path.

Call compile() once on the root schema and reuse the returned schema. Non-object root schemas need an XPath.

XmlSchemaBase.compile

write(data, options?): Promise<string>

Defined in: converter/base.ts:249

Write data to XML string asynchronously (public API)

Output

Data to write

XmlWriteOptions

Write options

Promise<string>

XML string

XmlSchemaBase.write

writeToStream(data, stream, options?): Promise<void>

Defined in: converter/base.ts:272

Write data to WritableStream asynchronously (public API)

Output

Data to write

WritableStream<Uint8Array<ArrayBufferLike>>

Writable stream to write to

XmlWriteOptions

Write options

Promise<void>

XmlSchemaBase.writeToStream

writeSync(data, options?): string

Defined in: converter/base.ts:286

Write data to XML string synchronously (public API)

Output

Data to write

XmlWriteOptions

Write options

string

XML string

XmlSchemaBase.writeSync

writer(config): this

Defined in: converter/base.ts:295

Configure writer settings for this schema

XmlElementWriteConfig

Writer configuration

this

This schema with writer config

XmlSchemaBase.writer


Defined in: converter/XmlStringSchema.ts:27

Schema for parsing XML string values

new XmlStringSchema(options?): XmlStringSchema

Defined in: converter/XmlStringSchema.ts:30

XmlStringOptions = {}

XmlStringSchema

XmlSchema.constructor

options: XmlStringOptions = {}

Defined in: converter/XmlStringSchema.ts:30

readonly _output: string

Defined in: converter/base.ts:31

XmlSchema._output

readonly _input: string

Defined in: converter/base.ts:32

XmlSchema._input

_parse(input, parseOptions?): string

Defined in: converter/XmlStringSchema.ts:34

Parse XML input synchronously

ParseInput

XML string, byte view, or sync iterator

ParseOptions

string

Parsed output

If parsing fails

XmlSchema._parse

_parseAsync(input, parseOptions?): Promise<string>

Defined in: converter/XmlStringSchema.ts:39

Parse XML input asynchronously

ParseInput

XML string, stream, or async iterator

ParseOptions

Promise<string>

Parsed output

If parsing fails

XmlSchema._parseAsync

xpath(path): XmlStringSchema

Defined in: converter/XmlStringSchema.ts:123

Set XPath expression for locating the element

string

XPath expression

XmlStringSchema

New schema with XPath

parse(input, options?): Promise<string>

Defined in: converter/base.ts:117

Parse XML asynchronously (public API)

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<string>

Parsed output

If parsing fails

XmlSchema.parse

parseSync(input, options?): string

Defined in: converter/base.ts:135

Parse XML synchronously (public API)

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string or sync iterator

ParseOptions

Parse options

string

Parsed output

If parsing fails

XmlSchema.parseSync

safeParse(input, options?): Promise<ParseResult<string>>

Defined in: converter/base.ts:152

Parse XML asynchronously with error handling

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<ParseResult<string>>

Parse result with success flag

XmlSchema.safeParse

safeParseSync(input, options?): ParseResult<string>

Defined in: converter/base.ts:173

Parse XML synchronously with error handling

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string, byte view, or sync iterator

ParseOptions

Parse options

ParseResult<string>

Parse result with success flag

XmlSchema.safeParseSync

transform<NewOutput>(fn): XmlSchemaBase<NewOutput, string>

Defined in: converter/base.ts:193

Transform the parsed output

NewOutput

(value) => NewOutput

Transform function

XmlSchemaBase<NewOutput, string>

New schema with transform applied

XmlSchema.transform

optional(): XmlSchemaBase<string | undefined, string | undefined>

Defined in: converter/base.ts:201

Make this schema optional

XmlSchemaBase<string | undefined, string | undefined>

New optional schema

XmlSchema.optional

array(xpath?): XmlSchemaBase<string[], string[]>

Defined in: converter/base.ts:210

Convert this schema to an array schema

string

XPath expression for array elements

XmlSchemaBase<string[], string[]>

New array schema

XmlSchema.array

compile(): XmlSchemaBase<string, string>

Defined in: converter/base.ts:238

Compile this schema for repeated parsing.

XmlSchemaBase<string, string>

New compiled schema

compile() preserves the public parsing API and can speed up schemas that can be lowered to fixed XML event dispatch. The optimized path works best when the root schema is an object, array, string, or number with static XPath selectors.

Fast-path friendly selectors use absolute paths such as /catalog/book, descendant paths such as //book, and relative selectors inside object or array items such as ./title, ./@id, ./name/text(), or ./name/@code. Object fields, arrays of scalar values, arrays of objects, nested objects, optional fields, and transforms are supported.

Selectors with wildcards or predicates, ambiguous relative paths such as title, nested arrays, and arrays that combine an array XPath with an element XPath are parsed with the normal runtime converter path instead. This keeps behavior compatible, but does not get the dispatch fast path.

Call compile() once on the root schema and reuse the returned schema. Non-object root schemas need an XPath.

XmlSchema.compile

write(data, options?): Promise<string>

Defined in: converter/base.ts:249

Write data to XML string asynchronously (public API)

string

Data to write

XmlWriteOptions

Write options

Promise<string>

XML string

XmlSchema.write

writeToStream(data, stream, options?): Promise<void>

Defined in: converter/base.ts:272

Write data to WritableStream asynchronously (public API)

string

Data to write

WritableStream<Uint8Array<ArrayBufferLike>>

Writable stream to write to

XmlWriteOptions

Write options

Promise<void>

XmlSchema.writeToStream

writeSync(data, options?): string

Defined in: converter/base.ts:286

Write data to XML string synchronously (public API)

string

Data to write

XmlWriteOptions

Write options

string

XML string

XmlSchema.writeSync

writer(config): this

Defined in: converter/base.ts:295

Configure writer settings for this schema

XmlElementWriteConfig

Writer configuration

this

This schema with writer config

XmlSchema.writer


Defined in: converter/XmlTransformSchema.ts:11

Schema for transforming parsed values

Output

Input

IntermediateOutput = unknown

new XmlTransformSchema<Output, Input, IntermediateOutput>(schema, transformFn): XmlTransformSchema<Output, Input, IntermediateOutput>

Defined in: converter/XmlTransformSchema.ts:19

XmlSchemaBase<IntermediateOutput, Input>

(value) => Output

XmlTransformSchema<Output, Input, IntermediateOutput>

XmlSchemaBase.constructor

readonly _output: Output

Defined in: converter/base.ts:31

XmlSchemaBase._output

readonly _input: Input

Defined in: converter/base.ts:32

XmlSchemaBase._input

_parse(input, options?): Output

Defined in: converter/XmlTransformSchema.ts:28

Parse XML input synchronously

ParseInput

XML string, byte view, or sync iterator

ParseOptions

Parse options

Output

Parsed output

If parsing fails

XmlSchemaBase._parse

_parseAsync(input, options?): Promise<Output>

Defined in: converter/XmlTransformSchema.ts:33

Parse XML input asynchronously

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<Output>

Parsed output

If parsing fails

XmlSchemaBase._parseAsync

parse(input, options?): Promise<Output>

Defined in: converter/base.ts:117

Parse XML asynchronously (public API)

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<Output>

Parsed output

If parsing fails

XmlSchemaBase.parse

parseSync(input, options?): Output

Defined in: converter/base.ts:135

Parse XML synchronously (public API)

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string or sync iterator

ParseOptions

Parse options

Output

Parsed output

If parsing fails

XmlSchemaBase.parseSync

safeParse(input, options?): Promise<ParseResult<Output>>

Defined in: converter/base.ts:152

Parse XML asynchronously with error handling

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<ParseResult<Output>>

Parse result with success flag

XmlSchemaBase.safeParse

safeParseSync(input, options?): ParseResult<Output>

Defined in: converter/base.ts:173

Parse XML synchronously with error handling

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string, byte view, or sync iterator

ParseOptions

Parse options

ParseResult<Output>

Parse result with success flag

XmlSchemaBase.safeParseSync

transform<NewOutput>(fn): XmlSchemaBase<NewOutput, Input>

Defined in: converter/base.ts:193

Transform the parsed output

NewOutput

(value) => NewOutput

Transform function

XmlSchemaBase<NewOutput, Input>

New schema with transform applied

XmlSchemaBase.transform

optional(): XmlSchemaBase<Output | undefined, Input | undefined>

Defined in: converter/base.ts:201

Make this schema optional

XmlSchemaBase<Output | undefined, Input | undefined>

New optional schema

XmlSchemaBase.optional

array(xpath?): XmlSchemaBase<Output[], Input[]>

Defined in: converter/base.ts:210

Convert this schema to an array schema

string

XPath expression for array elements

XmlSchemaBase<Output[], Input[]>

New array schema

XmlSchemaBase.array

compile(): XmlSchemaBase<Output, Input>

Defined in: converter/base.ts:238

Compile this schema for repeated parsing.

XmlSchemaBase<Output, Input>

New compiled schema

compile() preserves the public parsing API and can speed up schemas that can be lowered to fixed XML event dispatch. The optimized path works best when the root schema is an object, array, string, or number with static XPath selectors.

Fast-path friendly selectors use absolute paths such as /catalog/book, descendant paths such as //book, and relative selectors inside object or array items such as ./title, ./@id, ./name/text(), or ./name/@code. Object fields, arrays of scalar values, arrays of objects, nested objects, optional fields, and transforms are supported.

Selectors with wildcards or predicates, ambiguous relative paths such as title, nested arrays, and arrays that combine an array XPath with an element XPath are parsed with the normal runtime converter path instead. This keeps behavior compatible, but does not get the dispatch fast path.

Call compile() once on the root schema and reuse the returned schema. Non-object root schemas need an XPath.

XmlSchemaBase.compile

write(data, options?): Promise<string>

Defined in: converter/base.ts:249

Write data to XML string asynchronously (public API)

Output

Data to write

XmlWriteOptions

Write options

Promise<string>

XML string

XmlSchemaBase.write

writeToStream(data, stream, options?): Promise<void>

Defined in: converter/base.ts:272

Write data to WritableStream asynchronously (public API)

Output

Data to write

WritableStream<Uint8Array<ArrayBufferLike>>

Writable stream to write to

XmlWriteOptions

Write options

Promise<void>

XmlSchemaBase.writeToStream

writeSync(data, options?): string

Defined in: converter/base.ts:286

Write data to XML string synchronously (public API)

Output

Data to write

XmlWriteOptions

Write options

string

XML string

XmlSchemaBase.writeSync

writer(config): this

Defined in: converter/base.ts:295

Configure writer settings for this schema

XmlElementWriteConfig

Writer configuration

this

This schema with writer config

XmlSchemaBase.writer


Defined in: converter/base.ts:30

Base abstract class for all XML schema types

This class provides the foundation for zod-style declarative XML parsing. Each schema type extends this class and implements the parsing logic.

Output

Input = Output

new XmlSchemaBase<Output, Input>(): XmlSchemaBase<Output, Input>

XmlSchemaBase<Output, Input>

readonly _output: Output

Defined in: converter/base.ts:31

readonly _input: Input

Defined in: converter/base.ts:32

abstract _parse(input, options?): Output

Defined in: converter/base.ts:53

Parse XML input synchronously

ParseInput

XML string, byte view, or sync iterator

ParseOptions

Parse options

Output

Parsed output

If parsing fails

abstract _parseAsync(input, options?): Promise<Output>

Defined in: converter/base.ts:62

Parse XML input asynchronously

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<Output>

Parsed output

If parsing fails

parse(input, options?): Promise<Output>

Defined in: converter/base.ts:117

Parse XML asynchronously (public API)

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<Output>

Parsed output

If parsing fails

parseSync(input, options?): Output

Defined in: converter/base.ts:135

Parse XML synchronously (public API)

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string or sync iterator

ParseOptions

Parse options

Output

Parsed output

If parsing fails

safeParse(input, options?): Promise<ParseResult<Output>>

Defined in: converter/base.ts:152

Parse XML asynchronously with error handling

ParseInput

XML string, stream, or async iterator

ParseOptions

Parse options

Promise<ParseResult<Output>>

Parse result with success flag

safeParseSync(input, options?): ParseResult<Output>

Defined in: converter/base.ts:173

Parse XML synchronously with error handling

string | ArrayBufferView<ArrayBufferLike> | Iterator<AnyXmlEvent, any, any>

XML string, byte view, or sync iterator

ParseOptions

Parse options

ParseResult<Output>

Parse result with success flag

transform<NewOutput>(fn): XmlSchemaBase<NewOutput, Input>

Defined in: converter/base.ts:193

Transform the parsed output

NewOutput

(value) => NewOutput

Transform function

XmlSchemaBase<NewOutput, Input>

New schema with transform applied

optional(): XmlSchemaBase<Output | undefined, Input | undefined>

Defined in: converter/base.ts:201

Make this schema optional

XmlSchemaBase<Output | undefined, Input | undefined>

New optional schema

array(xpath?): XmlSchemaBase<Output[], Input[]>

Defined in: converter/base.ts:210

Convert this schema to an array schema

string

XPath expression for array elements

XmlSchemaBase<Output[], Input[]>

New array schema

compile(): XmlSchemaBase<Output, Input>

Defined in: converter/base.ts:238

Compile this schema for repeated parsing.

XmlSchemaBase<Output, Input>

New compiled schema

compile() preserves the public parsing API and can speed up schemas that can be lowered to fixed XML event dispatch. The optimized path works best when the root schema is an object, array, string, or number with static XPath selectors.

Fast-path friendly selectors use absolute paths such as /catalog/book, descendant paths such as //book, and relative selectors inside object or array items such as ./title, ./@id, ./name/text(), or ./name/@code. Object fields, arrays of scalar values, arrays of objects, nested objects, optional fields, and transforms are supported.

Selectors with wildcards or predicates, ambiguous relative paths such as title, nested arrays, and arrays that combine an array XPath with an element XPath are parsed with the normal runtime converter path instead. This keeps behavior compatible, but does not get the dispatch fast path.

Call compile() once on the root schema and reuse the returned schema. Non-object root schemas need an XPath.

write(data, options?): Promise<string>

Defined in: converter/base.ts:249

Write data to XML string asynchronously (public API)

Output

Data to write

XmlWriteOptions

Write options

Promise<string>

XML string

writeToStream(data, stream, options?): Promise<void>

Defined in: converter/base.ts:272

Write data to WritableStream asynchronously (public API)

Output

Data to write

WritableStream<Uint8Array<ArrayBufferLike>>

Writable stream to write to

XmlWriteOptions

Write options

Promise<void>

writeSync(data, options?): string

Defined in: converter/base.ts:286

Write data to XML string synchronously (public API)

Output

Data to write

XmlWriteOptions

Write options

string

XML string

writer(config): this

Defined in: converter/base.ts:295

Configure writer settings for this schema

XmlElementWriteConfig

Writer configuration

this

This schema with writer config


Defined in: converter/errors.ts:6

XML parse error with detailed issue information

  • Error

new XmlParseError(issues): XmlParseError

Defined in: converter/errors.ts:16

object[]

XmlParseError

Error.constructor

issues: object[]

Defined in: converter/errors.ts:10

List of validation issues

path: string[]

message: string

code: string

Defined in: Writer.ts:20

Configuration options for the Writer

optional encoding?: string

Defined in: Writer.ts:25

Text encoding for the output stream

'utf-8'

optional prettyPrint?: boolean

Defined in: Writer.ts:31

Whether to format output with indentation

false

optional indentString?: string

Defined in: Writer.ts:37

String used for indentation when prettyPrint is true

' '

optional addEntities?: object[]

Defined in: Writer.ts:43

Additional custom entities to encode

entity: string

value: string

[]

optional autoEncodeEntities?: boolean

Defined in: Writer.ts:49

Whether to automatically encode XML entities

true

optional namespaces?: NamespaceDeclaration[]

Defined in: Writer.ts:55

Namespace declarations to include

[]

optional bufferSize?: number

Defined in: Writer.ts:61

Internal buffer size in bytes

16384

optional highWaterMark?: number

Defined in: Writer.ts:67

WritableStream backpressure threshold

65536

optional flushThreshold?: number

Defined in: Writer.ts:73

Automatic flush threshold (percentage of bufferSize)

0.8

optional enableAutoFlush?: boolean

Defined in: Writer.ts:79

Whether to enable automatic flushing

true

Defined in: WriterSync.ts:7

Sink interface for custom sync targets.

write(chunk): void

Defined in: WriterSync.ts:8

string

void

optional flush(): void

Defined in: WriterSync.ts:9

void

optional close(): void

Defined in: WriterSync.ts:10

void


Defined in: WriterSync.ts:16

Writer output options shared by string and sink variants.

optional encoding?: string

Defined in: WriterSync.ts:17

optional prettyPrint?: boolean

Defined in: WriterSync.ts:18

optional indentString?: string

Defined in: WriterSync.ts:19

optional addEntities?: object[]

Defined in: WriterSync.ts:20

entity: string

value: string

optional autoEncodeEntities?: boolean

Defined in: WriterSync.ts:21

optional namespaces?: NamespaceDeclaration[]

Defined in: WriterSync.ts:22


Defined in: WriterSync.ts:28

Writer options for sink-based sync mode.

optional encoding?: string

Defined in: WriterSync.ts:17

WriterSyncOptions.encoding

optional prettyPrint?: boolean

Defined in: WriterSync.ts:18

WriterSyncOptions.prettyPrint

optional indentString?: string

Defined in: WriterSync.ts:19

WriterSyncOptions.indentString

optional addEntities?: object[]

Defined in: WriterSync.ts:20

entity: string

value: string

WriterSyncOptions.addEntities

optional autoEncodeEntities?: boolean

Defined in: WriterSync.ts:21

WriterSyncOptions.autoEncodeEntities

optional namespaces?: NamespaceDeclaration[]

Defined in: WriterSync.ts:22

WriterSyncOptions.namespaces

optional bufferSize?: number

Defined in: WriterSync.ts:33

Internal character buffer size.

16384

optional enableAutoFlush?: boolean

Defined in: WriterSync.ts:39

Emit buffered chunks automatically when threshold is reached.

true

optional flushOnClose?: boolean

Defined in: WriterSync.ts:45

Whether to call sink.flush() when the writer is finalized.

false

optional flushThreshold?: number

Defined in: WriterSync.ts:52

Flush threshold (percentage or absolute char count). If <= 1, treated as percentage of bufferSize. Otherwise absolute char count.

0.8

Defined in: converter/types.ts:10

Parse options for XML converter

optional trimText?: boolean

Defined in: converter/types.ts:15

Whether to trim whitespace from text content

false

optional decodeEntities?: boolean

Defined in: converter/types.ts:21

Whether to decode XML entities

true

optional strict?: boolean

Defined in: converter/types.ts:27

Strict mode for parsing

false

optional documentMode?: DocumentMode

Defined in: converter/types.ts:34

XML document conformance mode.

'fragment'

optional maxDepth?: number

Defined in: converter/types.ts:40

Maximum XML depth

1000

optional maxEvents?: number

Defined in: converter/types.ts:46

Maximum number of events to process

1000000

optional xpathNamespaces?: Record<string, string>

Defined in: converter/types.ts:56

Namespace bindings used by XPath 1.0 prefix resolution.

XML default namespaces do not automatically apply to unprefixed XPath element names, matching XPath 1.0 semantics. Bind a prefix here and use it in XPath expressions when selecting namespaced elements.


Defined in: converter/types.ts:65

Options for string schema

optional xpath?: string

Defined in: converter/types.ts:69

XPath expression to locate the element

optional min?: number

Defined in: converter/types.ts:74

Minimum string length

optional max?: number

Defined in: converter/types.ts:79

Maximum string length

optional pattern?: RegExp

Defined in: converter/types.ts:84

Regular expression pattern to validate against


Defined in: converter/types.ts:92

Options for number schema

optional xpath?: string

Defined in: converter/types.ts:96

XPath expression to locate the element

optional min?: number

Defined in: converter/types.ts:101

Minimum value

optional max?: number

Defined in: converter/types.ts:106

Maximum value

optional int?: boolean

Defined in: converter/types.ts:112

Whether the number must be an integer

false

Defined in: converter/types.ts:120

Options for object schema

optional xpath?: string

Defined in: converter/types.ts:124

XPath expression to locate the element

optional strict?: boolean

Defined in: converter/types.ts:130

Strict mode - reject unknown properties

false

Defined in: converter/types.ts:138

Writer configuration for XML element

element: string

Defined in: converter/types.ts:142

Element name (required)

optional asAttribute?: string

Defined in: converter/types.ts:148

Write as attribute instead of element Value is the attribute name

optional namespace?: object

Defined in: converter/types.ts:153

Namespace configuration

optional prefix?: string

Namespace prefix (e.g., ‘dc’, ‘xsi’)

optional uri?: string

Namespace URI (e.g., ‘http://purl.org/dc/elements/1.1/‘)

optional cdata?: boolean

Defined in: converter/types.ts:169

Wrap content in CDATA section

false

optional selfClosing?: boolean

Defined in: converter/types.ts:175

Use self-closing tag for empty elements

false

optional comment?: string

Defined in: converter/types.ts:180

Add XML comment before element


Defined in: converter/types.ts:188

Options for XML writer

optional prettyPrint?: boolean

Defined in: converter/types.ts:193

Format output with indentation

false

optional indentString?: string

Defined in: converter/types.ts:199

Indentation string

' '

optional encoding?: string

Defined in: converter/types.ts:205

Text encoding for output

'utf-8'

optional rootElement?: string

Defined in: converter/types.ts:211

Root element name If not provided, no root element wrapper is added

optional namespaces?: object[]

Defined in: converter/types.ts:216

Global namespace declarations

prefix: string

uri: string

optional includeDeclaration?: boolean

Defined in: converter/types.ts:225

Include XML declaration

true

optional xmlVersion?: string

Defined in: converter/types.ts:231

XML version for declaration

'1.0'

optional writer?: Writer | WriterSync | WriterSyncSink

Defined in: converter/types.ts:239

Custom writer instance

  • WriterSync: for writeSync() method
  • WriterSyncSink: for writeSync() with custom sink
  • Writer: for write() async method

Defined in: types.ts:48

Event fired when the document starts parsing

type: "START_DOCUMENT"

Defined in: types.ts:49


Defined in: types.ts:57

Event fired when the document ends parsing

type: "END_DOCUMENT"

Defined in: types.ts:58


Defined in: types.ts:66

Event fired when an XML element starts

type: "START_ELEMENT"

Defined in: types.ts:67

name: string

Defined in: types.ts:68

optional localName?: string

Defined in: types.ts:69

optional prefix?: string

Defined in: types.ts:70

optional uri?: string

Defined in: types.ts:71

attributes: Record<string, string>

Defined in: types.ts:72

optional attributesWithPrefix?: Record<string, AttributeInfo>

Defined in: types.ts:73


Defined in: types.ts:76

type: "END_ELEMENT"

Defined in: types.ts:77

name: string

Defined in: types.ts:78

optional localName?: string

Defined in: types.ts:79

optional prefix?: string

Defined in: types.ts:80

optional uri?: string

Defined in: types.ts:81


Defined in: types.ts:84

type: "CHARACTERS"

Defined in: types.ts:85

value: string

Defined in: types.ts:86


Defined in: types.ts:89

type: "CDATA"

Defined in: types.ts:90

value: string

Defined in: types.ts:91


Defined in: types.ts:94

type: "ERROR"

Defined in: types.ts:95

error: Error

Defined in: types.ts:96


Defined in: types.ts:114

Attribute interface (for Writer)

optional prefix?: string

Defined in: types.ts:115

localName: string

Defined in: types.ts:116

optional uri?: string

Defined in: types.ts:117

value: string

Defined in: types.ts:118


Defined in: types.ts:125

Namespace declaration interface (for Writer) Not used in this simple implementation.

prefix: string

Defined in: types.ts:126

uri: string

Defined in: types.ts:127


Defined in: types.ts:134

Processing instruction (PI) interface (for Writer) Not used in this simple implementation.

target: string

Defined in: types.ts:135

optional data?: string

Defined in: types.ts:136


Defined in: types.ts:142

Attribute information interface

value: string

Defined in: types.ts:143

localName: string

Defined in: types.ts:144

optional prefix?: string

Defined in: types.ts:145

optional uri?: string

Defined in: types.ts:146


Defined in: types.ts:354

Element writing options interface (for Writer)

optional prefix?: string

Defined in: types.ts:355

optional uri?: string

Defined in: types.ts:356

optional attributes?: Record<string, string | AttributeInfo>

Defined in: types.ts:357

optional selfClosing?: boolean

Defined in: types.ts:358

optional comment?: string

Defined in: types.ts:359

XmlObjectShape = Record<string, XmlSchema<unknown, unknown>>

Defined in: converter/XmlObjectSchema.ts:42

Shape type for object schema


InferObjectOutput<T> = { [K in keyof T]: T[K]["_output"] }

Defined in: converter/XmlObjectSchema.ts:49

Infer output type from object shape

T extends XmlObjectShape


ParseInput = string | ArrayBufferView | ReadableStream<Uint8Array> | AsyncIterator<AnyXmlEvent> | Iterator<AnyXmlEvent>

Defined in: converter/base.ts:14

Parse input type - accepts string, sync iterator, async iterator, or ReadableStream


ParseResult<T> = { success: true; data: T; } | { success: false; error: XmlParseError; }

Defined in: converter/errors.ts:28

Parse result type for safe parsing operations

T


Infer<T> = T["_output"]

Defined in: converter/index.ts:108

T extends XmlSchema<unknown, unknown>


SchemaType = typeof SchemaType[keyof typeof SchemaType]

Defined in: converter/types.ts:247

Schema type union


XmlCoreSchema = XmlStringSchema | XmlNumberSchema | XmlArraySchema<XmlSchemaBase<unknown, unknown>> | XmlObjectSchema<XmlObjectShape>

Defined in: converter/types.ts:277

Core schema types (non-wrapper schemas)


XmlWrappedSchema = XmlTransformSchema<unknown, unknown> | XmlOptionalSchema<XmlSchemaBase<unknown, unknown>>

Defined in: converter/types.ts:288

Wrapper schema types (transform and optional)


AnyXmlSchema = XmlCoreSchema | XmlWrappedSchema

Defined in: converter/types.ts:297

Any XML schema type


XmlEventType = typeof XmlEventType[keyof typeof XmlEventType]

Defined in: types.ts:6

Enumeration of XML stream event types used by the StAX parser


AnyXmlEvent = StartDocumentEvent | EndDocumentEvent | StartElementEvent | EndElementEvent | CharactersEvent | CdataEvent | ErrorEvent

Defined in: types.ts:102

Discriminated Union type for developer use


DocumentMode = "fragment" | "document"

Defined in: types.ts:373

XML document conformance mode.

const x: XmlBuilder

Defined in: converter/XmlBuilder.ts:58

Singleton builder instance


const SchemaType: object

Defined in: converter/types.ts:247

Schema type constants for XML schema classification

readonly STRING: "STRING" = 'STRING'

readonly NUMBER: "NUMBER" = 'NUMBER'

readonly ARRAY: "ARRAY" = 'ARRAY'

readonly OBJECT: "OBJECT" = 'OBJECT'

readonly TRANSFORM: "TRANSFORM" = 'TRANSFORM'

readonly OPTIONAL: "OPTIONAL" = 'OPTIONAL'


const XmlEventType: object

Defined in: types.ts:6

Enumeration of XML stream event types used by the StAX parser

readonly START_DOCUMENT: "START_DOCUMENT" = 'START_DOCUMENT'

readonly END_DOCUMENT: "END_DOCUMENT" = 'END_DOCUMENT'

readonly START_ELEMENT: "START_ELEMENT" = 'START_ELEMENT'

readonly END_ELEMENT: "END_ELEMENT" = 'END_ELEMENT'

readonly CHARACTERS: "CHARACTERS" = 'CHARACTERS'

readonly CDATA: "CDATA" = 'CDATA'

readonly ERROR: "ERROR" = 'ERROR'

isStringSchema(schema): schema is XmlStringSchema

Defined in: converter/types.ts:304

Type guard for string schema

XmlSchemaBase<unknown, unknown>

schema is XmlStringSchema


isNumberSchema(schema): schema is XmlNumberSchema

Defined in: converter/types.ts:313

Type guard for number schema

XmlSchemaBase<unknown, unknown>

schema is XmlNumberSchema


isArraySchema(schema): schema is XmlArraySchema<XmlSchemaBase<unknown, unknown>>

Defined in: converter/types.ts:322

Type guard for array schema

XmlSchemaBase<unknown, unknown>

schema is XmlArraySchema<XmlSchemaBase<unknown, unknown>>


isObjectSchema(schema): schema is XmlObjectSchema<XmlObjectShape>

Defined in: converter/types.ts:331

Type guard for object schema

XmlSchemaBase<unknown, unknown>

schema is XmlObjectSchema<XmlObjectShape>


isTransformSchema(schema): schema is XmlTransformSchema<unknown, unknown, unknown>

Defined in: converter/types.ts:340

Type guard for transform schema

XmlSchemaBase<unknown, unknown>

schema is XmlTransformSchema<unknown, unknown, unknown>


isOptionalSchema(schema): schema is XmlOptionalSchema<XmlSchemaBase<unknown, unknown>>

Defined in: converter/types.ts:349

Type guard for optional schema

XmlSchemaBase<unknown, unknown>

schema is XmlOptionalSchema<XmlSchemaBase<unknown, unknown>>