</> XML Language Service v1.0.1
XML tools for JavaScript and TypeScript

XML Language Service

This tool helps you work with XML in JavaScript and TypeScript projects. It can understand XML even when it has errors, and it gives editor-like features without needing Java or native software.

$ npm install xml-language-service
Works anywhere Use it in editors, language servers, CLIs, web apps, and tests.
Handles broken XML Incomplete XML still produces a usable document tree.
No Java setup XSD validation runs through bundled Xerces-C++ WebAssembly.

Install

Install the package in an ESM-compatible Node.js project.

bash
npm install xml-language-service
Requires Node.js 18+. The package includes the WASM files needed for XSD validation.

Quick Start

Create a service, parse XML, then call the feature you need.

typescript
import { getLanguageService } from "xml-language-service";

const service = getLanguageService();

const xml = `<catalog>
  <book id="bk101">
    <title>XML Developer Guide</title>
  </book>
</catalog>`;

const document = service.parseXMLDocument("file:///catalog.xml", xml);

const completions = service.doComplete(document, { line: 1, character: 4 });
const hover = service.doHover(document, { line: 2, character: 6 });
const symbols = service.findDocumentSymbols(document);
const formatting = service.format(document, { tabSize: 2, insertSpaces: true });

console.log(completions.items);
console.log(hover);
console.log(symbols);
console.log(formatting);

service.dispose();

Features

The service returns plain TypeScript objects, so you can connect these features to your own editor, CLI, web app, language server, or test tool.

Fault-tolerant parsing

Reads XML and builds a usable document tree, even when the file is incomplete or has syntax errors.

Auto complete

Suggests element names, attributes, and closing tags while the user is typing.

Hover info

Shows useful information for the XML element, attribute, or comment under the cursor.

Document symbols

Creates an outline of the XML structure so editors can show breadcrumbs, sidebars, and quick navigation.

Folding ranges

Finds XML blocks that can be collapsed, such as large nested elements.

Formatting

Returns text edits that format XML with clean indentation and spacing.

Rename

Renames matching opening and closing tags together so XML stays consistent.

Definition and references

Jumps between matching tags and finds other elements with the same tag name.

XSD validation

Checks XML against XSD schemas using Apache Xerces-C++ compiled to WebAssembly.

XSD Validation

Register an XSD schema once, then validate parsed XML documents against it.

typescript
await service.registerSchema({
  uri: "file:///schema.xsd",
  xsdText: schemaText,
});

const document = service.parseXMLDocument("file:///catalog.xml", xmlText);
const diagnostics = await service.validate("file:///schema.xsd", document);

Schema bundles are also supported for XSD files that use xs:include or xs:import.

Debugging

Use the printer helpers when you want to see what the parser produced.

typescript
console.log(service.printTreeAST(document));
console.log(service.printAST(document));
console.log(service.printCST(document));

Links

Resource URL
npm package npmjs.com/package/xml-language-service
Repository github.com/harshanacz/xml-language-service
Project using this github.com/harshanacz/xml-review-bot-demo
XML language server based on this github.com/harshanacz/xml-language-server