weft-xml 0.2.1

XPath 3.1, XQuery 3.1 and XSLT 3.0 in Rust: a command and the libraries under it, tested on the W3C suites
# weft _(rust-xml-vocabularies)_

XPath 3.1, XQuery 3.1 and XSLT 3.0 in Rust: a command and the libraries under it, tested on the W3C suites

weft parses XML into an owned document, evaluates XPath and XQuery over it,
transforms it with XSLT stylesheets read at run time, and serializes the
result. It is a port of a Common Lisp engine,
[cl-xml-vocabularies](https://github.com/afluentlife-loom/cl-xml-vocabularies),
and is measured against that engine's answers. Use it to run XPath, XQuery or
your own XSLT stylesheets (1.0, 2.0 or 3.0) from a shell or a Rust program
without a JVM or a C library; to read XML into your own types, a plain parser
is simpler. Building it needs a Rust toolchain, and the library crates also
build for WebAssembly. It is version 0.2.1, had from this repository only.

<!-- headline -->
Measured on the W3C test cases that engine passes, weft passes 22,511 of 22,511 for
XPath 3.1 and 7,786 of 7,890 for XQuery 3.1, and agrees with it on 9,038 of
11,271 (80%) for XSLT 3.0: the XSLT is the least complete part
([Conformance, measured](#conformance-measured)).
<!-- /headline -->

MIT licence ([`LICENSE`](LICENSE)); third-party data in the tree keeps its
own licence ([`NOTICE`](NOTICE)). To install the command and run it:

```sh
git clone https://github.com/afluentlife-loom/rust-xml-vocabularies
cd rust-xml-vocabularies
cargo install --locked --path crates/xml
weft xpath '(1 to 3) ! (. * 2)'     # => 2 4 6
```

## Table of Contents

- [Background]#background
- [Install]#install
- [Usage]#usage
- [Conformance, measured]#conformance-measured
- [Performance, measured]#performance-measured
- [How it is made]#how-it-is-made
- [Not supported]#not-supported
- [Stability]#stability
- [Documentation]#documentation
- [Maintainers]#maintainers
- [Contributing]#contributing
- [License]#license

## Background

The port is checked against the Common Lisp engine because the engine can
answer any case; the W3C's specifications and tests have the last word when
the two disagree. Some of
the port's sources are generated from the engine's declared tables (function
signatures, the casting matrix, error codes, the XQuery grammar); those files
are committed, so building the port never needs the engine. There is a
sibling port in Swift,
[swift-xml-vocabularies](https://github.com/afluentlife-loom/swift-xml-vocabularies).

The port does not carry everything the engine does.
[`ADMISSION.md`](ADMISSION.md) lists what it carries, for whom, and what it
refuses by name.

Code comments and some documents cite the project's process records
(`plan-0645`, or `cl plan-0645` for one kept with the engine). Those records
are not published: a citation names the record behind a change, not a page
you can open.

## Install

### Dependencies

- **A stable Rust toolchain**, 2021 edition. No minimum version is declared;
  the port has been built and tested with rustc 1.97.1 (arm64 macOS) and
  1.99.0 (x86-64 Linux).
- **Crates from crates.io**, which cargo fetches. The `weft` command needs
  eight: `quick-xml`, `fancy-regex` and what they need.
  ([`docs/crates.md`]docs/crates.md#what-cargo-fetches has the rest.)

### Steps

The first three lines at the top are the whole installation; the fourth tries
it. `cargo install` puts one binary, `weft`, in `~/.cargo/bin`, in about 45
seconds on a 24-core machine.

To use the libraries from another project, name the repository in its
`Cargo.toml`, and add a `rev` to pin it. `weft-xpath-ir` evaluates all three
languages; `xslt` is a feature because some hosts leave it out, and the rename
lets the code below be the repository's own:

```toml
[dependencies]
xpath-ir = { package = "weft-xpath-ir", git = "https://github.com/afluentlife-loom/rust-xml-vocabularies", features = ["xslt"] }
```

## Usage

### Command

From the repository's root, over the catalogue in `corpus/`:

```sh
weft xpath 'count(//book)' corpus/catalog.xml     # => 3
weft xquery -e 'for $b in //book return string($b/@id)' corpus/catalog.xml     # => b1 b2 b3
weft xslt corpus/catalog.xsl corpus/catalog.xml > report.xml
cat corpus/catalog.xml | weft xpath 'string((//title)[1])' -     # => Requisite Variety
```

The file named last is the context item, or the stylesheet's source; `-`
reads standard input. `weft xquery FILE.xq` runs a query from a file. An
error is written to standard error as its W3C code and message
(`XPST0003 unexpected end of the expression`), and the exit status is 1, or 2
for a usage error. `weft --help` has the rest.

### Library

```rust
use xpath_ir::xslt::transform::transform_text;

fn main() {
    let stylesheet = r#"<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
      <xsl:template match="/"><out><xsl:value-of select="count(//i)"/></out></xsl:template>
    </xsl:stylesheet>"#;
    // the last argument names an initial template, for a run with no source
    match transform_text(stylesheet, Some("<d><i/><i/></d>"), None) {
        Ok(result) => println!("{result}"),
        Err(code) => eprintln!("{code}"), // a W3C error code, such as XTDE0640
    }
}
```

The same file is in the repository:

```sh
cargo run --locked -p weft-xml --example transform     # => <?xml version="1.0" encoding="UTF-8"?><out>2</out>
```

An XPath or XQuery over a document, compiling a stylesheet once for many
documents, parameters, what a stylesheet may read, limits, WebAssembly and the browser polyfill are in
[`docs/usage.md`](docs/usage.md); the crates are listed in
[`docs/crates.md`](docs/crates.md).

## Conformance, measured

The cases below are the W3C suites' cases that the Common Lisp engine passes,
so the port is measured against what its reference can do. **There is no
whole-suite figure for the port itself.** The engine passes 31,505 of the
31,821 cases of QT3, the W3C's XPath and XQuery 3.1 suite; the port is asked
those that need no schema. The engine's
[README](https://github.com/afluentlife-loom/cl-xml-vocabularies#conformance-measured)
has every suite.

For XPath and XQuery a case *passes* when the result meets the W3C's own
expectation. For XSLT the port *agrees* when it gives the engine's output, or
an error where the engine gives one.

Each number is a **floor**: the minimum a named check holds the port to,
raised when the port passes more. `python3 -I tools/readme-numbers.py`
writes this table from the checks, and `tools/green.sh`, the port's own
check, fails when the table and the checks disagree.

<!-- numbers -->
| what is measured | floor | held by |
|---|---:|---|
| XPath 3.1: the QT3 cases the reference engine passes on the XPath route (no schema) | 22,511 of 22,511 | `crates/xpath-ir/tests/universe.rs:22` |
| XPath 3.1: a hand-picked list of those, every case checked against the reference engine's answer | 3,725 of 3,725 | `crates/xpath-ir/tests/differential.rs:26` |
| XQuery 3.1: the QT3 XQuery cases the reference engine passes (their export is not in the repository) | 7,786 of 7,890 | `crates/xpath-ir/tests/xquery_cases.rs:30` |
| XSLT 3.0: the W3C suite's cases the reference engine passes where this port's answer agrees | 9,038 of 11,271 | `docs/xslt-ratchet.tsv:113` |
| XSLT 3.0: W3C suite cases whose output is byte-identical to the reference engine's | 8,024 | `docs/xslt-ratchet.tsv:110` |
| XSLT 3.0: W3C suite cases byte-identical to the Swift port's output | 3,789 | `docs/xslt-ratchet.tsv:153` |
<!-- /numbers -->

Only the first two rows run in a fresh clone (`cargo test`); the others read
the engine's answers, which are too large to be in the repository.

For XSLT, about one case in five that the engine passes does not agree. Of
those, four in ten are not asked, mostly for XSLT packages and for sources
validated by a schema, and two in ten are refused because their stylesheet
needs a schema; the port has neither. In two in ten one side raised an error
and the other did not. But in about 410 **both gave a result and the results
differed**, so a wrong answer is possible without an error. Try your own
stylesheets. [`docs/testing.md`](docs/testing.md) has the counts.

## Performance, measured

The benchmark is Saxonica's XT-Speedo, 130 XSLT cases. The port fails 25 of
them and gets 3 wrong. Over the 89 that this port, the Common Lisp engine,
Saxon-HE 13.0 (on OpenJDK 27.0.1) and SaxonC-HE 12.9.0 all ran correctly, the
port took **about a third less time than Saxon-HE 13.0**: 0.675 of it, as the
geometric mean of the 89 ratios, end to end (compile, parse, transform,
serialize). It was faster on 75 cases and slower on 14.

That is the best of 5 runs of each case, on one Apple silicon machine, with
mostly small sources; five runs may not warm a JVM fully. The harness that
runs all the engines is not published, so you cannot repeat the comparison
from this repository, only time this port's side.
[`docs/performance.md`](docs/performance.md) has the stages, the other engines
and the commits.

## How it is made

One maintainer develops the port with AI coding agents. Four rules decide
what lands:

- **The engine is the oracle.** Its answers to the W3C suites' cases are
  exported to files and the port is compared with them, result by result and
  error code by error code. A case where the port is right and the engine is
  wrong is recorded as that, and the engine is repaired.
- **Numbers only go up.** The floors above are ratchets: a change that lowers
  one fails the checks. That stops a loss; it does not prove the rest right.
- **Three targets run the library crates' tests**: the host, `wasm32-wasip1`,
  and `wasm32-unknown-unknown` in node, as a web page runs it.
- **A repair is tested by removing it.** A repair is planned with a prediction
  before it is written, gets tests that failed without it, and is then taken
  out again, alone, to see exactly those tests fail. The records of this are
  not published; the tests are.

**Saxon.** Saxon-HE is open source under the MPL-2.0, and none of it is copied
here. It is run as a black box, its outputs and times compared with the
port's. Its source was read for this project once: in November 2026 one agent
session, which wrote no engine or port code, read Saxon-HE 13's source and
described in ordinary terms, with no code and no names from it, which general
techniques it uses. A script checked the description against the source's
names and comments, and it was reviewed and approved before any implementer
saw it. Parts of it were an input to the template rule index, positional
access and the joins ([`docs/techniques/`](docs/techniques/README.md) says
where); the description itself is not published. Whoever writes code here,
person or agent, never opens, searches for or copies Saxon's source. Whether
an AI model met it in training is not something this project can know.

The engine's
[How this is built](https://github.com/afluentlife-loom/cl-xml-vocabularies/blob/main/docs/how-this-is-built.md)
describes this way of working in full, what it has cost and where it has
failed.

## Not supported

- **XML Schema.** Validation and schema-typed input are not implemented.
  XQuery refuses `import schema` (`XQST0059`) and `validate`. XSLT refuses
  `xsl:import-schema` (`XTSE1650`), a `type` attribute and
  `validation="strict"` (`XTSE1660`), and
  `system-property('xsl:is-schema-aware')` answers `no`, so a stylesheet can
  leave them out with `use-when`.
- **XSLT packages.** `xsl:use-package` is refused (`XTSE3000`).
- **Streaming, in part.** What a stylesheet declares streamable is checked
  against the specification's rules, and refused where they do not guarantee
  it (`XTSE3430`). A document it declares streamable is then read as a stream
  and `weft xslt` writes the result as it is made, but some constructs still
  hold the whole document (accumulators, a set operator between two streamed
  selections, `xsl:merge`, an ancestor several items share), so
  `system-property('xsl:supports-streaming')` answers `no`. An indented
  result is written at the end.
- **XPath 4.0 and XQuery 4.0**, which are drafts. Parts of them work when a
  query asks for version 4.0; no number above measures them.
- **Fetching over the network.** `weft` reads files; `doc('http://…')` is
  refused (`FODC0002`).
- **Stylesheet parameters in the command.** `weft xslt` takes none. The
  library does.
- **Untrusted input.** A stylesheet or query run by `weft` can read any file
  its user can (`doc`, `unparsed-text`), and nothing bounds its time or
  memory. It writes only with `-o`, and only under the directory named there
  unless `--allow-outside` is given. It sees no environment variable, external
  entities are refused (`FODC0016`) and `xsl:evaluate` is off (`XTDE3175`). A
  library host decides what may be read, and can set limits
  ([`docs/usage.md`]docs/usage.md#limits).
- **Windows.** The checks run on macOS and Linux only.
- **A stable API.** The version is 0.2.1 and any of it may change
  ([Stability]#stability).

## Stability

None of the Rust API is stable yet. While the version is 0.x, cargo's rule
holds: releases 0.2.x are compatible with each other, and a minor version
(0.3) may change or remove anything public, in any crate. From 1.0 the crates
follow semantic versioning. What must settle before 1.0:

- **What `weft-xpath-ir` exposes.** Its intermediate representation (`ir`,
  `plan::Expr`, `types`) and the evaluator's own modules (`eval`, `funcs`,
  `xqeval`, `pul` and others) are public so the port's tests and tools can
  reach them, and the test helpers (`judge`, `parse::parity`, `bench_doc`)
  with them. 1.0 narrows the crate to compiling, evaluating and transforming
  with their options, and hides the representation.
- **The host traits.** `weft_xdm::host::Host` and `Writer`,
  `weft_xdm::reader::Events`, `weft_xpath_ir::ctx::DocSource` and
  `Transformer`, `weft_xpath_ir::plan::Ext`, `weft_xpath_ir::xslt::Resources`
  (with `xslt`) and `xpath_ffi::HostFunction` may gain methods as hosts ask
  for more (resources read as streams, limits).
- **The errors.** `XdmError` is a struct with public fields (a W3C code, a
  message, a name), and the parsers, the compiler, the FFI and XPointer each
  have their own type (`ParseError`, `ReadError`, `CompileError`,
  `XPathError`, `XPointerError`). 1.0 settles one error shape, extensible
  without a breaking change.
- **The generated tables.** `weft-xdm-generated` and `weft-xquery-parser`'s
  grammar tables are generated from the reference engine and change with each
  regeneration; their constants are not for direct use.

The features `parked` and `xquf` build tests of parked work and are not part
of the API. The `weft` command's subcommands, its error lines (a W3C code,
then the message) and its exit statuses (1 for an error, 2 for a usage
error) are meant to stay. `python3 -I tools/api-diff.py OLD NEW` lists the
public items added, removed or changed between two commits, so a release can
say what it breaks.

## Documentation

- [`docs/usage.md`]docs/usage.md — how to use the command, the libraries,
  WebAssembly and the browser polyfill.
- [`docs/crates.md`]docs/crates.md — every crate and the source layout.
- [`docs/testing.md`]docs/testing.md — the checks, how to run them, and
  what each needs.
- [`docs/performance.md`]docs/performance.md — the comparison above, stage
  by stage.
- [`docs/techniques/`]docs/techniques/README.md — the techniques the port
  rests on: the prior art, the code and the measurement for each.
- [`docs/README.md`]docs/README.md — every page in `docs/`, the generated
  tables and the parked work among them.

## Maintainers

David Hart.

## Contributing

Whether this repository takes issues and pull requests from outside, and on
what terms, is not decided yet; until it is, please do not open pull
requests ([`CONTRIBUTING.md`](CONTRIBUTING.md)). The clean-room rule above
would apply to any contribution.

## License

MIT © 2026 David Hart — see [`LICENSE`](LICENSE). The crates also carry
third-party data under its own terms: Unicode CLDR and Unicode Character
Database tables (Unicode License V3, `licenses/Unicode-3.0.txt`), the HTML
Standard's character references (WHATWG, BSD 3-Clause), text derived from W3C
specifications, and test data from the W3C's suites. [`NOTICE`](NOTICE) lists
each with its licence.