# @xml-tools/ast

> XML Ast and Utilities

Latest version **5.0.6** (published 2026-10-09) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @xml-tools/ast
pnpm add @xml-tools/ast
yarn add @xml-tools/ast
bun add @xml-tools/ast
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 5.0.6 |
| Published | 2026-10-09 |
| First published | 2019-10-28 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 41.9 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 21 |
| Maintainers | shahars, rima.sirich, sap-ospo-admin |
| Keywords | xml, ast |

## Links

- npm: https://www.npmjs.com/package/@xml-tools/ast
- Repository: https://github.com/SAP/app-studio-toolkit
- Issues: https://github.com/SAP/app-studio-toolkit/issues
- npm.io page: https://npm.io/package/@xml-tools/ast

## Dependencies (4)

- [lodash](https://npm.io/package/lodash.md) 4.17.23
- [chevrotain](https://npm.io/package/chevrotain.md) 7.1.1
- [@xml-tools/common](https://npm.io/package/@xml-tools/common.md) 0.1.7
- [@xml-tools/parser](https://npm.io/package/@xml-tools/parser.md) 1.0.12

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 5.0.6 (latest) — 2026-10-09
- 5.0.5 — 2021-06-03
- 5.0.4 — 2021-02-09
- 5.0.3 — 2021-01-21
- 5.0.2 — 2021-01-20
- 5.0.1 — 2021-01-20
- 5.0.0 — 2020-06-29
- 4.2.1 — 2020-06-18
- 4.2.0 — 2020-05-17
- 4.1.0 — 2020-05-05
- 4.0.3 — 2020-04-02
- 4.0.2 — 2020-02-19
- 4.0.1 — 2020-02-11
- 4.0.0 — 2020-02-10
- 3.0.0 — 2020-02-07
- … 14 more at https://npm.io/package/@xml-tools/ast/versions

## README

[![npm (scoped)](https://img.shields.io/npm/v/@xml-tools/ast.svg)](https://www.npmjs.com/package/@xml-tools/ast)

# @xml-tools/ast

Utilities for building and traversing an XML **A**bstract **S**yntax **T**ree ([AST][ast]).

There are two things which distinguish this AST from most others ASTs:

1. This AST can represent a **partially valid** XML, in practice this means virtually all properties on
   the AST are optional and may have undefined values.
2. This AST contains **additional syntactic information** to enable additional linting & formatting flows, for example:
   - the original position and value of an attribute's value (including the quotes).
   - The original positions and values of an XMLElement's open/close names.

The input for constructing the AST is a CST which is created by the [@xml-tools/parser](../parser) package.

The AST structure is used as part of the input for the [@xml-tools/content-assist](../content-assist) APIs.

## Installation

With npm:

- `npm install @xml-tools/ast`

With Yarn

- `yarn add @xml-tools/ast`

## Usage

Please see the [TypeScript Definitions](./api.d.ts) for full API details.

A simple usage example:

```javascript
const { parse } = require("@xml-tools/parser");
const { buildAst, accept } = require("@xml-tools/ast");

const xmlText = `<note>
                     <to>Bill</to>
                     <from>Tim</from>
                 </note>
`;

const { cst, tokenVector } = parse(xmlText);
const xmlDocAst = buildAst(cst, tokenVector);
console.log(xmlDocAst.rootElement.name); // -> note

// A Visitor allows us to invoke actions on the XML ASTNodes without worrying about
// The XML AST structure / traversal method.
const printVisitor = {
  // Will be invoked once for each Element node in the AST.
  visitXMLElement: function (node) {
    console.log(node.name);
  },

  // An XML AST Visitor may have other methods as well, see the api.d.ts file/
};

// Invoking the Visitor
accept(xmlDocAst, printVisitor); // -> note, Bill, Tim
```

## Support

Please report issues [here](https://github.com/SAP/app-studio-toolkit/issues/new/choose) and label them with `project:xml-tools`.

## Contributing

See [CONTRIBUTING.md](https://github.com/SAP/app-studio-toolkit/blob/main/CONTRIBUTING.md).

---
_Source: https://npm.io/package/@xml-tools/ast · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
