@lanexio/parser-grammar-xml
This page documents @lanexio/parser-grammar-xml, the XML grammar package implementing XML 1.0 5th Edition with optional DTD validation, external entity resolution, and comprehensive attribute-level DTD information.
- Version: Stable
- Module name:
parser-grammar-xml - Package:
@lanexio/parser-grammar-xml - Import path:
@lanexio/parser-grammar-xml - Layer: 2 (Grammar)
- Runtime: Universal
- Module format: ESM
- Stability: Stable
- Primary use case: Parse XML documents into a flat AST with optional DTD validation.
Layer contract
Section titled “Layer contract”When to use this module
Section titled “When to use this module”- You need to parse XML 1.0 documents into a traversable AST.
- You need optional DTD validation (content models, required attributes, ID/IDREF consistency).
- You need to resolve external DTD subsets via a callback.
- You need access to parsed DTD declarations (element, attribute-list, entity).
Module boundary
Section titled “Module boundary”| Boundary | Description |
|---|---|
| Inputs | Uint8Array (source bytes) + optional ParseXmlOptions |
| Outputs | LexTree (flat AST) |
| Side effects | None (the parser does no I/O; external DTD resolved via callback) |
| Determinism | Yes |
| External dependencies | @lanexio/parser-core |
| Never-throw guarantee | Yes |
| Security surface | None (parse only, no output generation; no entity expansion) |
Installation
Section titled “Installation”-
Install the package.
Terminal window pnpm add @lanexio/parser-grammar-xmlTerminal window npm install @lanexio/parser-grammar-xmlTerminal window yarn add @lanexio/parser-grammar-xml -
Import the named export.
import { parseXml } from '@lanexio/parser-grammar-xml';
Peer dependencies
Section titled “Peer dependencies”| Requirement | Required | Layer | Notes |
|---|---|---|---|
@lanexio/parser-core | Yes | 1 | ^1.0.0 |
Basic Usage
Section titled “Basic Usage”import { parseXml } from '@lanexio/parser-grammar-xml';
const encoder = new TextEncoder();const bytes = encoder.encode('<root><item id="1">value</item></root>');
const tree = parseXml(bytes);
console.log(tree.nodeCount);DTD validation
Section titled “DTD validation”import { parseXml } from '@lanexio/parser-grammar-xml';
const encoder = new TextEncoder();const tree = parseXml( encoder.encode('<!DOCTYPE root [ <!ELEMENT root (#PCDATA)> ]><root>text</root>'), { validate: true });Exports
Section titled “Exports”| Export | Type | Description |
|---|---|---|
parseXml | (bytes: Uint8Array, options?: ParseXmlOptions) => LexTree | Parse XML. Never throws. |
XmlKind | const object | Numeric kind IDs for all XML node types. |
XmlField | const object | Numeric field IDs for XML element slots. |
XML_FIELD_NAMES_BY_ID | readonly string[] | Field name lookup by numeric field ID. |
XML_KIND_NAMES_BY_ID | readonly Record<number, string> | Kind-name lookup by numeric ID. |
xmlGrammar | LanexioParserPureGrammar | Grammar descriptor for use with parser-pure. |
xmlRegistration | GrammarRegistration | Registration for the unified grammar registry. |
xmlReuseOracle | ReuseOracle | Incremental reuse oracle. |
Options
Section titled “Options”ParseXmlOptions
Section titled “ParseXmlOptions”| Field | Type | Required | Default | Description |
|---|---|---|---|---|
validate | boolean | No | false | When true, validates against the DTD after parsing. |
resolveExternal | ExternalResolver | No | undefined | Callback for resolving external DTD subset references. The parser does no I/O. |
baseUri | string | No | "" | Base URI for resolving relative SYSTEM identifiers. |
Return shape
Section titled “Return shape”| Property | Type | Description |
|---|---|---|
root | LexNode | Root node of the XML document. |
nodeCount | number | Total nodes in the tree. |
source | Uint8Array | Original parsed bytes. |
Exported types
Section titled “Exported types”| Type | Purpose | Notes |
|---|---|---|
ParseXmlOptions | Options for parseXml | See options table above. |
ExternalResolver | Callback for resolving external DTD subsets | (publicId, systemId, baseUri) => Uint8Array | null |
DtdInfo | Parsed DTD declarations | Elements, attlists, entities |
DtdElementDecl | DTD element declaration | Name and content model |
DtdAttlistDecl | DTD attribute-list declaration | Element name and attr defs |
DtdAttrDef | Single attribute definition in ATTLIST | Name, type, default |
DtdEntityDecl | DTD entity declaration | Name and value |
XmlKindType | Union of all XmlKind values | Type-safe kind reference |
XmlFieldType | Union of all XmlField values | Type-safe field reference |
Configuration and Extension
Section titled “Configuration and Extension”DTD resolution
Section titled “DTD resolution”The resolveExternal callback allows callers to provide external DTD content without the parser performing network I/O. The parser’s no-expand security model prevents billion-laughs attacks.
Accessibility
Section titled “Accessibility”Accessibility requirements
Section titled “Accessibility requirements”- No direct accessibility surface. The XML parser produces flat AST data structures.
Accessibility checklist
Section titled “Accessibility checklist”| Concern | Status |
|---|---|
| Generated output semantics | Not applicable (data format) |
| ARIA attributes in serialized output | Not applicable |
| Semantic element round-trip | Not applicable |
Security
Section titled “Security”Security considerations
Section titled “Security considerations”parseXmlnever throws on any byte sequence.- The parser uses a no-expand security model for entities (billion-laughs guarantee).
| Threat | Mitigation | Status |
|---|---|---|
| Malformed input byte sequence | Panic-free guarantee: all inputs accepted, errors produce LexError AST nodes | Implemented |
| Billion-laughs (entity expansion) | No entity expansion: entities are tracked but not substituted in parsed output | Implemented |
Companion packages
Section titled “Companion packages”| Package | Relationship | Layer | Notes |
|---|---|---|---|
@lanexio/parser-core | Requires | 1 | Provides LexTree, LexNode, LexCursor |
@lanexio/parser | Consumes | 6 | Unified entry point |
Changelog
Section titled “Changelog”| Version | Date | Status | Notable changes |
|---|---|---|---|
1.0.0 | 2026-05-29 | Current | Initial stable release. Apache-2.0. |
Migration notes
Section titled “Migration notes”- None.
Related Content
Section titled “Related Content” Flat AST How the 16-byte node layout works.
Stability Guarantees Never-throw, panic-free, and 3-token recovery.