TypesXML - XML Processing Library for TypeScript
TypesXML is a TypeScript / Node.js XML library for parsing, validating, and processing XML
documents. It provides both DOM and streaming (SAX) APIs, along with support for DTD and XML
Schema 1.0 validation and XML Catalog resolution.
The library is implemented entirely in TypeScript (no native bindings) and is validated
against the official W3C XML test suites for DTD and XML Schema.
The source code is available on GitHub under the Eclipse
Public License v1.0. Developers can clone, adapt, and ship the library under the
terms of that license, or contact Maxprograms for commercial arrangements.
Quick example
Parse an XML file and build a DOM document:
import { DOMBuilder, SAXParser } from "typesxml";
const handler = new DOMBuilder();
const parser = new SAXParser();
parser.setContentHandler(handler);
parser.parseFile("example.xml");
const document = handler.getDocument();
console.log(document.toString());
Why TypesXML
Most XML tools in JavaScript ecosystems fall into one of two categories: DOM-based models that are
easy to use but memory-heavy and unpredictable for large documents, or lightweight parsers that
expose raw streaming events without a structured model on top.
TypesXML sits between these extremes. It is built on a SAX-style foundation but exposes a strongly
structured, TypeScript-friendly model that stays predictable even for large and complex XML formats
such as TMX and XLIFF.
- Streaming-oriented processing without requiring full in-memory document trees
- Structured access to XML data while preserving parsing determinism
- Native support for DTD and XML Schema validation workflows
- Aligned with translation/localization formats rather than generic XML editing
- Designed for backend and tooling scenarios, not browser DOM manipulation
TypesXML is not intended to replace general-purpose XML libraries. It is focused specifically on
cases where XML is a data interchange format with strict structural requirements and large-scale
processing needs.
Features
Core capabilities for parsing, validation, and integration of XML documents:
Parsing
- DOM builder (
DOMBuilder) for in-memory document trees with lexical preservation
- Streaming SAX parser with file, string, and Node.js stream support
Validation
- Complete DTD parser/validator (including conditional sections and parameter entities)
- XML Schema 1.0 validation with support for complex types
- Strict XML 1.0 / 1.1 character validation
Integration
- OASIS XML Catalog resolver for public/system identifiers
- XML ↔ JSON conversion (lightweight and lossless modes)
Compliance
- 100% W3C XML DTD test suite
- 96.5% W3C XML Schema test suite
SAX Parser
SAXParser drives any ContentHandler implementation. A handler receives
structured callbacks during parsing:
interface ContentHandler {
initialize(): void;
setCatalog(catalog: Catalog): void;
startDocument(): void;
endDocument(): void;
xmlDeclaration(version: string, encoding: string, standalone: string | undefined): void;
startElement(name: string, atts: XMLAttribute[]): void;
endElement(name: string): void;
internalSubset(declaration: string): void;
characters(text: string): void;
ignorableWhitespace(text: string): void;
comment(text: string): void;
processingInstruction(target: string, data: string): void;
startCDATA(): void;
endCDATA(): void;
startDTD(name: string, publicId: string, systemId: string): void;
endDTD(): void;
skippedEntity(name: string): void;
getGrammar(): Grammar | undefined;
setGrammar(grammar: Grammar | undefined): void;
getCurrentText(): string;
}
The built-in DOMBuilder implements this interface to provide DOM support out of the box.
Usage
import { DOMBuilder, SAXParser } from "typesxml";
const handler = new DOMBuilder();
const parser = new SAXParser();
parser.setContentHandler(handler);
// Parse from a file
parser.parseFile("example.xml");
const document = handler.getDocument();
console.log(document.toString());
// Parse from a string
parser.parseString("");
// Parse from a stream
// await parser.parseStream(fs.createReadStream("example.xml"));
To enable XML Catalog resolution or validation, configure the parser before invoking
parse* methods:
parser.setCatalog(myCatalog);
parser.setValidating(true); // Turns on DTD and XML Schema validation
Documentation & Samples
- Read the step-by-stepTypesXML tutorial for guided workflows.
- Use the JSON and XML Conversion Guide to translate
between XML documents and JSON objects, with guidance on when to enable the metadata-preserving
round-trip mode.
- Explore the runnable examples under samples/ to see the code in action.
W3C XML Test Suite
The repository includes code that runs the official W3C XML Conformance Test Suite for DTD and XML
Schema grammars.
DTD
- Download the latest archive from the W3C XML Test Suite (e.g., xmlts20080827.zip).
- Extract the archive into
./tests/xmltest so the valid,
invalid, and not-wf folders sit under that path.
- Install dependencies if needed:
npm install.
- Run the test suite:
npm run testDtd
The script compiles the TypeScript sources and executes ts/tests/DTDTestSuite.ts,
reporting any conformance failures.
XML Schema
TypesXML currently passes 96.5% of the W3C XML Schema Test Suite (2006 edition, ~40,000 tests), the
only native TypeScript implementation of XML Schema 1.0 validated against the official W3C test
suite.
- Download the latest archive from the XML Schema Version 1.0, 2nd Edition.
- Extract the archive into
./tests/ so the test cases are available under
./tests/xmlschema2006-11-06.
- Install dependencies if needed:
npm install.
- Run the test suite:
npm run testXmlSchema
The script compiles the TypeScript sources and executes ts/tests/XmlSchemaTestSuite.ts,
reporting any conformance failures.