- Zig 95%
- XSL 3%
- Shell 1%
- Nix <1%
zxsl #
An XSLT 1.0 processor for Zig 0.17, built on zxpath, ztree, zxml, zig-datetime, zig-collate and zregex.
It is a library first and a command line tool second. The library reads no
files and opens no sockets: a stylesheet's xsl:include and its document()
calls go through a Loader the caller supplies, so a transformation can be run
against a bundle, a cache, or nothing at all, and an untrusted stylesheet
cannot reach the filesystem because there is nothing there to reach it with.
The zxsl binary is the one place a Loader is wired to a directory.
The API documentation is generated from the doc comments, which is where most of the explanation lives.
Installation #
$ zig fetch --save git+https://git.jcollie.dev/jeff/zxsl.git
That is main, which needs Zig 0.17. The last release for Zig 0.16 is tagged
v0.1.0, and the zig-0.16 branch carries it:
$ zig fetch --save git+https://git.jcollie.dev/jeff/zxsl.git#v0.1.0
Repository #
The repository's home is my Forgejo instance at git.jcollie.dev/jeff/zxsl, which is where CI runs.
$ git clone https://git.jcollie.dev/jeff/zxsl.git
It is mirrored on Tangled at https://tangled.org/jcollie.dev/zxsl, and it is also published on Radicle, a peer-to-peer forge built on git, where the copy needs no account and no server anyone has to keep running. The repository's identifier there is
rad:z2dxNbg5hyMZBET2t6Laok4jH43ba
and this fetches it:
$ rad clone rad:z2dxNbg5hyMZBET2t6Laok4jH43ba
Any of the three is the whole project, on the main branch, with the same
history.
rad clone finds seeds through your local node's routing table rather than
through a known host, so the node has to be running before it can find
anything:
$ rad node start
If you already have the repository and only want to help host it, seeding it tells your node to carry a copy for others:
$ rad seed rad:z2dxNbg5hyMZBET2t6Laok4jH43ba
Standards #
| Standard | Title | Support in zxsl |
|---|---|---|
| XSLT 1.0 | XSL Transformations | Every instruction and top-level declaration; see the table below for what within them is not covered |
| XPath 1.0 | XML Path Language | Through zxpath: all thirteen axes, all four types and the core function library. XSLT's own additions — key, document, current, format-number, generate-id and the rest — are supplied from here |
| XML 1.0 (5th ed.) | Extensible Markup Language | Through ztree and zxml. The internal subset is read — entities, attribute defaults and ID declarations — but nothing external is ever fetched |
| XML Namespaces 1.0 | Namespaces in XML | Resolved into expanded names by ztree when a document is parsed, and here when an expression is compiled |
| XML Schema Part 2 | Datatypes | The eight right-truncated date and time types, which is what EXSLT's dates-and-times module is defined in terms of. Nothing else of XML Schema: there is no validation here |
| UTS #10 | Unicode Collation Algorithm | Through zig-collate, which passes the conformance suite of section 12.2 in full. It is what xsl:sort orders text with, and lang selects a CLDR tailoring on top of it |
| UTS #35 part 5 | LDML: Collation | Through zig-collate: the per-locale tailorings xsl:sort lang= selects are compiled from CLDR's own rule files, and all 134 CLDR 48.2 ships, root aside, are built in by default |
EXSLT common |
node-set, object-type, exsl:document |
A result tree fragment is already a tree here, so node-set hands a node-set back unchanged and wraps anything else in a fragment holding one text node. exsl:document writes a secondary result document to the caller's Sink, which refuses everything until the caller says otherwise — see Secondary result documents |
EXSLT functions |
func:function, func:result |
A stylesheet declares its own functions and calls them from any expression. func:script names code in another language and is not implemented |
EXSLT sets |
difference intersection distinct has-same-node leading trailing |
All six |
EXSLT math |
min max highest lowest abs sqrt power log exp constant |
The trigonometric and random functions are not implemented |
EXSLT strings |
replace tokenize split padding concat align encode-uri decode-uri |
All eight |
crypto |
md5, sha1 |
Not an EXSLT module but a pair of extension functions libxslt defines in that namespace. The digests, and not the ciphers — see below |
EXSLT regexp |
match replace test |
All three, over zregex. libxslt implements no regexp at all, so these are the one part of this processor with no oracle behind them — see Regular expressions |
EXSLT dynamic |
evaluate, map |
An expression that will not compile or will not run is an empty node-set rather than an error: it came from the document, not the stylesheet. closure, min, max and sum are not implemented |
EXSLT dates-and-times |
date-time date time year leap-year month-in-year month-name month-abbreviation week-in-year week-in-month day-in-year day-in-month day-of-week-in-month day-in-week day-name day-abbreviation hour-in-day minute-in-hour second-in-minute add add-duration sum difference duration seconds |
format-date and parse-date are not implemented; they want a locale and a strftime. date:date-time() needs a clock, which Options.io supplies |
What is here and what is not #
XSLT needs a tree. zxml is a pull parser and never builds one, which is the
right shape for reading a feed once and the wrong shape for a language whose
questions a stream cannot answer: chapter/title asks for a node's parent,
xsl:sort reorders a node-set and then wants document order back, and a key
indexes every node in the document before the first template runs.
That tree is ztree, and it is a separate library because none of it is about XSLT — it resolves namespace prefixes, merges adjacent character data, orders every node, and serializes a document back out by the XML, HTML or text method. It lived here first and was lifted out once it was clear the import graph had no edges back.
XPath went the same way, for the same reason, and is zxpath. EXSLT's
dates-and-times module needs a calendar, and a calendar is the kind of thing
that is wrong in the same way in every project that reimplements one, so that
comes from zig-datetime: the ISO 8601 parsing, the durations
and the arithmetic are its, and what is here is only the part that is XSLT's —
XML Schema's eight right-truncated date types, the rule deciding which of them
an answer is written in, and the year numbering that has no zero.
xsl:sort needs a collation, and a collation is the same kind of thing again:
comparing text by code point is not merely unlocalized but wrong in a way
people notice, Mueller sorting before Märtens because u is U+0075 and
ä is U+00E4. So the Unicode Collation Algorithm is
zig-collate — the weighing, the sort keys and CLDR's per-locale
tailorings — and what is here is the part that is XSLT's: the two attributes
xsl:sort spells a collation with, lang and case-order, and computing one
sort key per node rather than collating inside the comparison.
What is left here is XSLT itself: match patterns and their priorities, the
stylesheet reader, xsl:number and format-number, and the engine that
decides which template handles which node. The other layers still matter to
all of it — an XSLT result is a tree and not a string, and nearly every
instruction evaluates an expression — but building trees, running XPath,
counting days and ordering text are somebody else's job now.
Design #
- Template dispatch is indexed, not scanned. Rules are filed by what their
pattern's last step can match -- an element name, an attribute name, a
wildcard,
text(),/-- because a pattern is matched from its last step backwards, so that step alone decides whether a rule could apply at all. Each bucket is sorted by the order conflicts resolve in, so a lookup is a merge across at most four of them that stops at the first rule which really matches. At a thousand templates that is about four times faster than testing every rule against every node. - The library does no I/O. A
Loaderis a pair of function pointers the caller supplies.Loader.nonerefuses everything, which is the default. - Expressions are compiled once and cached by where they were written. A
prefix in
select="x:a"means whateverxwas bound to on the element the attribute sits on, so an expression is compiled against that element and cached against that attribute node. - The stylesheet is interpreted from its tree. There is no instruction set in between. An instruction set would run faster and would be several hundred lines whose only job is to restate a tree that is already in memory; since every expression is compiled once, the work that repeats is not the parsing.
- Values own their storage. An arena would be easier to write and would make the peak memory of a transformation whatever the worst template happened to allocate. A node-set can hold every node in a large document.
- Nesting is bounded. Expressions, evaluation and template instantiation are all capped, so a stylesheet that recurses without a base case is an error rather than a stack overflow in whatever is calling the library.
Sorting text #
xsl:sort orders text by the Unicode Collation Algorithm, through
zig-collate. XSLT 1.0 section 10 leaves the ordering
implementation-defined, so that is a choice rather than a requirement — but it
is a better default than code point order for every stylesheet whether or not
it says which language its data is in: Märtens sorts before Mueller
because the root collation gives ä the same primary weight as a, not because
anybody asked for German.
lang selects a CLDR tailoring on top of that root, which is where a language
gets what is actually its own:
| root collation | with lang |
|
|---|---|---|
lang="sv" |
Ahlgren Ängström Östberg Zetterlund | Ahlgren Zetterlund Ängström Östberg |
lang="cs" |
chata cukr hora irena | cukr hora chata irena |
lang="tr" |
halk iyi ılık zil | halk ılık iyi zil |
Swedish's ä and ö are letters after z rather than variants of a and o; Czech
sorts ch as a letter of its own between h and i; Turkish's dotless ı is a
letter before i rather than a variant beside it. None of the three is
reachable from the root collation.
The attribute carries a language tag and is matched as one — the whole tag,
then with its last subtag dropped, and so on — so sv-FI finds Swedish. A
language with no tailoring is the root collation and never an error, which is
also the answer for 23 of CLDR's locales that need none because the root is
already right for them: German, French, English, Italian, Dutch and Portuguese
among them.
A locale's rules may ask for the parametric settings as well as the letters.
Danish's open with [caseFirst upper], which is what orders the three
spellings of aa — and since case-order is what the stylesheet itself
asked for, the attribute is applied after the locale and wins:
xsl:sort |
result |
|---|---|
lang="da" |
ab zz AA Aa aa |
lang="da" case-order="lower-first" |
ab zz aa Aa AA |
-Dlocales chooses which tailorings are compiled in. The default is all,
which is every locale CLDR ships a collation for:
| binary | over none | |
|---|---|---|
-Dlocales= |
7,516,256 | — |
-Dlocales=sv,tr,pl |
7,719,360 | +198 KiB |
-Dlocales=all, the default: 134 locales |
8,626,776 | +1.06 MiB |
ReleaseSafe on x86_64-linux. Asking for any tailoring at all also brings in
CLDR's root collation and sorts by that rather than by DUCET, because that is
what the tailorings are written against — a delta computed against one root
and laid over the other is being applied to a table it was not written for.
The rules and that root arrive together in a 66 MB archive, fetched once,
which is the reason -Dlocales= is worth having: it compiles no tailorings,
fetches nothing, and sorts every language by DUCET.
Secondary result documents #
exsl:document is EXSLT's multiple-output instruction — XSLT 2.0's
xsl:result-document, a version early — and it is how a chunking stylesheet
writes a document per chapter instead of one enormous one:
<xsl:for-each select="ch">
<exsl:document href="{@id}/index.html" method="html" indent="yes">
<html>...</html>
</exsl:document>
</xsl:for-each>
Its content is a template like any other and what it makes is a result tree
rather than a result tree fragment, so a literal result element in one carries
the stylesheet's in-scope namespace declarations exactly as one in the main
result does. Its output attributes are the xsl:output ones plus href, they
are attribute value templates where xsl:output's are not, and they start
from the defaults rather than from the stylesheet's xsl:output — the module
is explicit that "the attributes on xsl:output elements affect only the main
result document".
It is also the one thing in a stylesheet that writes, and this library
opens nothing. A Loader supplies what a transformation reads; a Sink takes
what it produces:
const result = try zxsl.transform.transformToString(gpa, sheet, source, .{
.sink = my_sink, // .none by default, and .none refuses everything
});
A Sink is given the href as the stylesheet wrote it and the document
already serialized, and it decides what — if anything — that means. The
default refuses, with error.DocumentNotWritten, which is what keeps an
untrusted stylesheet off the filesystem: it has nothing to reach it with. A
stylesheet that writes where the caller gave it nowhere to write is an error
rather than a silent discard.
The zxsl binary is the one place a Sink is wired to a directory, and it
confines what it writes to one:
$ zxsl -o out/index.xml chunk.xsl book.xml
$ find out -type f
out/index.xml
out/one/index.html
out/two/index.html
An href is resolved against the directory the main result goes to — XSLT
2.0's rule, and the only reading that puts ch01.html beside -o out/index.html — or against the working directory when the result goes to
standard output, which has no directory of its own. Directories along the way
are created. An absolute path, a URI with a scheme, or a relative one that
climbs out with .. is refused and the run fails, because a stylesheet is
input and this is the one thing the tool does at input's request that leaves a
mark.
Regular expressions #
EXSLT's regexp module is three functions — regexp:match, regexp:replace
and regexp:test — over zregex, for the same reason the collation
is zig-collate's: Zig's standard library ships no regular expressions, and an
engine is the kind of thing that is wrong in the same way in every project
that writes its own.
<xsl:if test="regexp:test($line, '^\s*#')">…</xsl:if>
<xsl:value-of select="regexp:replace($text, 'no', 'gi', 'yes')"/>
<xsl:for-each select="regexp:match($uri, '(\w+)://([^/:]+)(:\d*)?(.*)')">…</xsl:for-each>
The flags are the module's two: g for global and i for case-insensitive.
Any other character is ignored rather than refused, which is what "if a
character is present then that flag is true" asks for — a character that names
no flag makes no flag true.
This is the one part of the processor with no oracle. libxslt implements
no regexp module, so nothing in the corpus exercises these three and there
is nothing to compare them against. What they are written against is the
module's own page and its worked examples, and each example is a test. Two
places where the page is not explicit, and what was decided:
regexp:matchfollows JavaScript'sString.match, which is what "follows the Javascript regular expression syntax" points at. Withoutg, the first match: onematchelement for the whole of it, then one per capture group, an absent group giving an empty element. Withg, one element per match, each holding the whole of that match. The page's own examples settle it —([a-z])+overThis is a test stringglobally gives four elements and not four captures, and the trailing space on each is the tell, rendered away on the page but there in the text.- The replacement string is literal.
$1in it is a dollar sign and a one. The page calls the fourth argument "the string to replace the matched parts of the string" and scopes its mention of JavaScript to the pattern, so expanding$1would be inventing a feature rather than implementing one; a stylesheet that wants a capture back takes it fromregexp:match.
A pattern the engine cannot compile fails the transformation with a diagnostic naming the function, rather than quietly answering "no match" for the rest of the run — a pattern is nearly always written in the stylesheet, and a stylesheet that cannot say what it meant is worth stopping for. So is a search that runs past zregex's step budget, which is neither a match nor a non-match but a question that was never decided.
Usage #
const std = @import("std");
const zxsl = @import("zxsl");
const sheet = try zxsl.stylesheet.parse(gpa, stylesheet_text, .{});
defer sheet.destroy();
const source = try zxsl.tree.parse(gpa, document_text, .{});
defer source.destroy();
const output = try zxsl.transform.transformToString(gpa, sheet, source, .{});
defer gpa.free(output);
transform returns the result tree instead, for a caller that wants to walk it
rather than serialize it. Top-level xsl:params are supplied through
Options.parameters, and xsl:message goes wherever Options.messages
points.
When a stylesheet will not compile, sheet.diagnostic says what was wrong with
it — the expression, the attribute it was in, and what the parser objected to.
The error itself is only error.InvalidStylesheet, because an unbound prefix
and a stray bracket are the same kind of problem to a caller and neither is
findable without the text that caused it.
Conformance #
| Suite | Result |
|---|---|
| libxslt's own test corpus: its regression suite, the XSLT Recommendation's examples, and the per-subject suites | 376 of 408 |
tests/cases, recorded from xsltproc for this project |
29 of 29 |
The first number is the honest one, and the second is why it needs saying: a suite you wrote yourself passes because you wrote it. Running somebody else's found four bugs within an hour, one of which was silent heap corruption — and pointing it at libxslt's EXSLT suites, after those modules were already written and passing the tests here, found six more in an afternoon.
$ zig build corpus
libxslt corpus: 376/408 pass, 32 known failures
The corpus is fetched, not vendored. corpus/ is a project of its own whose
manifest names libxslt's release tarball as a lazy dependency, so nothing that
merely builds this library pays for it, and somebody else's tests stay under
somebody else's licence and outside this repository.
corpus/expected.txt lists every case that does not pass and why, and it is a
ratchet in both directions: a case that fails without being listed is an
error, and so is a case that passes while still listed. A fix cannot land
without deleting its line, and a regression cannot hide behind a long list of
known failures. Nothing is skipped and nothing is untriaged: every case in
the corpus is run, and each of the 32 that fail says what it is waiting on.
Six of those are cases whose recorded output is not what libxslt produces
any more — running xsltproc over the whole corpus and comparing is how that
was found, and an expectation is only an oracle while the oracle still
produces it.
| Category | Count | What it says |
|---|---|---|
exslt, libxslt-extension |
8 | libxslt's own extensions, and the Saxon and Xalan ones it provides, are not implemented |
spec-divergence |
7 | zxsl follows the Recommendation and libxslt does not; the recorded output is the oracle's own behaviour |
stale-expectation |
6 | The recorded output is not what libxslt produces today, so the case is not an oracle for anything |
generate-id |
3 | Untestable: the test compares generated ids literally, and only uniqueness is specified |
harness |
2 | libxslt's own harness runs the case with parameters, or with a plugin built; plain xsltproc does not reproduce it either |
strict-error |
2 | key() on an undeclared key raises here and recovers in libxslt |
external-entity |
1 | The internal subset is read; nothing external is ever fetched |
output-encoding |
1 | xsl:output names an encoding written as character references rather than bytes — valid in that encoding, but not byte for byte |
sort-collation |
1 | zxsl orders text by UTS #10 and libxslt compares code points; _ and - sort below letters in the root collation and above D by code point |
declined |
1 | crypto:rc4-encrypt; see below |
The one that used to be skipped #
general/bug-166 is the corpus's largest document -- fifteen thousand elements
against six keys, one of which indexes on a call to another -- and it is worth
keeping the history of this entry, because each correction was more
interesting than the last.
It began as too-slow, on the grounds that template dispatch was a linear
scan over every rule and the case took minutes. Dispatch was a linear scan
and is now indexed by name, but the minutes were an artefact of measuring a
debug build, whose allocator captures a stack trace per allocation: the
profile was sixty per cent DWARF unwinding. A benchmark is not a measurement
until you have looked at where the time went.
Measured properly it took seven seconds and produced the wrong answer, so it
became skipped: a correctness failure with a stability problem on top, since
it also exhausted the runner's memory budget and took the other 280 results
down with it. The wrong answer was one bug — xsl:sort evaluated its keys
without setting the current node, so a key using current() gave the same
answer for every node and a two-level sort silently became a one-level one.
The seven seconds and the memory went somewhere in the work on key(), which
had a dangling pointer in it at the time; which of those changes paid for it
was never measured.
It now runs in about a third of a second — 310ms, measured in a ReleaseFast build — and produces the right bytes. It is still by a wide margin the slowest case in the corpus: nothing else reaches twenty milliseconds.
Using libxslt as an oracle #
The test suite does not compare against expectations somebody wrote down. It compares against what libxslt actually printed.
tools/oracle.sh runs xsltproc over every case in tests/cases and captures
its output verbatim into an expected file; tests/conformance.zig then
asserts that this library produces the same bytes. For a specification this
size the difference matters, because what a careful reading of the words
suggests and what every implementation does are not always the same thing. Four
of the first twenty-nine cases disagreed, and all four were bugs here:
- The HTML output method indents by default, and its indentation is nothing like the XML method's — no spaces at all, newlines in exactly three places, and only around elements HTML actually defines.
- An HTML
<head>gets a character encoding declaration inserted into it. format-numberkeeps a leading integer digit: a#before the decimal point means "no padding here", not "no digit here".xsl:namespace-aliasreplaces a name's namespace URI and leaves its prefix alone.
Those rules are taken from libxml2's source rather than guessed at, down to its
name[0] != 'p' test for "p, pre and param" — which also catches picture,
and is reproduced with the quirk intact, because matching the reference
implementation's whitespace means matching it.
Where this library disagrees on purpose #
One case, xpath-number-format, carries a second expected output. XPath 1.0
§4.2 says a number is written in decimal with no exponent form
at all and with as many digits as it takes to identify the double. libxslt
hands the number to C instead, switching to scientific notation past INT_MAX
and rounding to fifteen significant digits:
| Expression | XPath 1.0, and zxsl | libxslt 1.1.45 |
|---|---|---|
1000000 * 1000000 |
1000000000000 |
1e+12 |
1 div 3 |
0.3333333333333333 |
0.333333333333333 |
1 div 100000000 |
0.00000001 |
1e-08 |
The first of those is the one that bites: an identifier computed in a
stylesheet and written out as 1e+12 is not the same document. A case that
diverges must carry a why.md arguing why, and a test fails if one does not —
a disagreement nobody wrote down is indistinguishable from a bug that was
papered over.
The other disagreement worth knowing about before porting a stylesheet is
about parameters and the built-in template rules. XSLT 1.0
§5.8 defines those rules as literal templates —
<xsl:template match="*|/"><xsl:apply-templates/></xsl:template> — and they
have no xsl:param, so §11.6 says a parameter passed to one is
ignored, and its own xsl:apply-templates passes nothing on. libxslt threads
the parameters through instead, so a with-param survives any number of
elements that no template matches:
<xsl:template match="/">
<xsl:apply-templates select="//wrapper">
<xsl:with-param name="p" select="'passed'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="leaf">
<xsl:param name="p" select="'default'"/>
<xsl:value-of select="$p"/>
</xsl:template>
Against <r><wrapper><leaf/></wrapper></r>, libxslt writes passed and this
writes default, because nothing matches wrapper. A stylesheet relying on
it will not work here without an explicit rule for the elements in between —
which it needs anyway to work on any other conforming processor.
One function of one module is missing on purpose rather than for want of
time. EXSLT crypto has crypto:rc4-encrypt and crypto:rc4-decrypt
alongside its digests, and those two are not here. RC4 is broken, and a
function named "encrypt" is an invitation to rely on it — a stylesheet author
who reaches for it is reaching for secret-keeping, which is the one thing it
cannot do. libxslt provides them for the stylesheets that already call them; a
new implementation has no such debt. crypto:md5 and crypto:sha1 are
here, because the things a stylesheet actually wants a digest for — naming a
fragment, keying a cache, telling two documents apart — are not
secret-keeping claims.
Versions, and what a stylesheet's own version means #
version on xsl:stylesheet, and xsl:version anywhere else, is read and
acted on. Every element has an effective version — the nearest one stated at
or above it — and it is compared against the version this processor implements,
which is what system-property('xsl:version') answers. One constant decides
both, so the two cannot drift apart.
| the stylesheet says | ||
|---|---|---|
| less than this processor implements | backwards-compatible behaviour | |
| the same | neither | |
| more | forwards-compatible behaviour | an xsl: element this processor does not know is ignored |
The comparison is against this processor rather than a fixed number, which
is the whole of what forwards compatibility means: a stylesheet is
forwards-compatible when it was written for a later version than the one
reading it. libxslt's general/bug-175 is the case that makes it concrete —
it declares version="2.0" and an xsl:function, and expects a processor
without xsl:function to ignore the declaration and take the xsl:otherwise
branch.
This tightened one thing. An unrecognised xsl: element at the top level
used to be ignored whatever the version said, which meant
<xsl:varaible name="x"/> read as a stylesheet with no such variable rather
than as a stylesheet with a typo. XSLT 1.0 §2.5 makes it an error unless
forwards-compatible behaviour is in force, and it now is one.
Two traps worth naming, because both are silent when got wrong. version on
xsl:output is a serialization parameter naming the XML declaration's
version, not the stylesheet's, so it is not read as one — nor is it on
xsl:result-document. And a version that is not a number is refused rather
than taken as 1.0, because a guess there changes the meaning of every
expression below it.
The same lenience at the instruction level is deliberately not tightened. An
unknown instruction still runs its xsl:fallback or fails when reached, rather
than being a static error: a static error rejects a stylesheet whose dead
branch has a typo, libxslt does not enforce it either, and the corpus is full
of stylesheets nobody has read in twenty years.
What is not covered #
Everything in XSLT 1.0 is implemented except the following, which are accepted and ignored rather than rejected, so that a stylesheet using them still runs:
| Feature | Status |
|---|---|
| External DTD subsets | The internal subset is read — its entity declarations resolve, its attribute defaults arrive as attributes, and its ID declarations are what id() looks for. Nothing external is ever fetched, so an entity declared SYSTEM cannot be resolved and an external subset is named and skipped |
xsl:number lang, letter-value |
Accepted and ignored; format, level, count, from, value, grouping-separator and grouping-size all work |
xsl:output media-type |
Accepted and ignored; it names a MIME type and changes no bytes |
| Extension elements and functions | EXSLT common, functions, sets, math, strings, dates-and-times, dynamic, regexp and the digests of crypto. random is the one module absent entirely, and math:random is missing for the same reason: a transformation here is a pure function of its inputs unless the caller hands it a clock, and a random sequence is the one thing that cannot be; see the table above for what within them is not covered. func:script is not implemented, which is the whole of it: it names code in another language, and there is no other language here. libxslt's own extensions are absent, as are Saxon's and Xalan's. An unknown extension element runs its xsl:fallback |
One place is deliberately more permissive than the specification: a result
tree fragment is a real tree here, so $fragment/foo works where XSLT 1.0 says
a fragment may only be converted to a string. The strict behaviour would be to
raise an error, and raising one would break stylesheets that every other
processor runs.
Command line #
$ zxsl style.xsl document.xml
$ zxsl --stringparam title 'A Report' style.xsl document.xml
$ zxsl -o out.html style.xsl -
The options are xsltproc's, because that is the program every example on the
web is written for and because the test suite compares the two — a case that
has to be invoked differently is a case that is not really being compared. A
document of - is read from standard input.
Testing #
$ zig build test
That runs the unit tests, the oracle suite and the fuzz targets over their
seed corpus, all together. Re-recording the oracle
needs xsltproc, which the devshell provides:
$ nix develop
$ zig build oracle # re-record every case
$ zig build oracle -- keys # or just one
The version of libxslt that produced the current recordings is in
tests/cases/libxslt-version, because "what libxslt printed" is not a fact
without it.
Fuzzing #
Everything here takes bytes from outside — an expression out of a stylesheet, a
stylesheet off disk, a document off the network — so tests/fuzz.zig checks
the property that matters for all of them: return, do not crash. An error
is a fine answer and so is a result; a panic, a leak or a stack overflow is a
failure in whatever embedded the library rather than in the document that
caused it.
$ zig build fuzz-run -- --seconds 600
$ zig build fuzz-run -- --iterations 500000 --target xpath
That is a loop of this project's own, and it is what CI runs: every target in one process, a count that means the same thing on every machine, and the seed printed so a failure can be replayed. What it has in place of coverage feedback is a corpus of inputs that already parse, which for a parser is most of the way there — the interesting inputs are the ones that are nearly right.
Zig's own fuzzer, with coverage feedback, runs the same targets one test binary at a time:
$ zig build fuzz --fuzz # until interrupted, with a web interface
$ zig build fuzz --fuzz=1M -Dfuzz-filter=xpath
The test binary is compiled with LLVM for this, because the self-hosted backend
Debug uses by default emits no coverage instrumentation and the fuzzer then
fails in the build runner instead of finding anything. A finding prints input saved to '.zig-cache/f/crash' above the report; anything else that goes wrong
is the tooling.
Eight targets: three that compile a small language (xpath, pattern,
picture), two that read a document (document, stylesheet), one that runs
the engine (transform), and two that read one of XSLT's other syntaxes
(date, duration). Five of them check only that nothing crashed. The other
three check a round trip, which is where the bugs have actually been:
whatever parsed must be writable, and what was written must parse back to the
same value.
That property has found three bugs so far, each of a different kind. zxml
accepted <:/> as an element whose name was a single colon, nothing
downstream objected, and the result serialized to <>, which is not a
document any more — a parser that accepts what its own writer cannot write
turns a malformed input into malformed output, so zxml validates names now.
zig-datetime checked each component of a duration for overflow as it read
it and not the total they accumulated into, so P9000000000000000000Y was
refused while P900000000000000000Y900000000000000000M panicked. And
format-number handed std.unicode.utf8Decode a clamped slice to find out
whether a picture was valid UTF-8; that function dispatches on the length it
is given and then asserts the lead byte agrees, so a four-byte lead with two
bytes behind it panicked rather than failing. Its own doc comment calls the
API too easy to use incorrectly.
References cited #
What this processor is written against, and what it is checked against. The
Standards table above says which part of each one is covered;
this is the bibliography. The same list is kept as a Zotero collection named
zxsl, and RFC 2396 carries the RFC Editor's text as an attachment.
- Berners-Lee, T., R. Fielding, and L. Masinter. Uniform Resource Identifiers (URI): Generic Syntax. RFC 2396. Draft Standard. RFC Editor, August 1998. https://www.rfc-editor.org/info/rfc2396.
- Biron, Paul V. and Ashok Malhotra. XML Schema Part 2: Datatypes Second Edition. W3C Recommendation REC-xmlschema-2-20041028. World Wide Web Consortium (W3C), 28 October 2004. https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/.
- Bray, Tim, Jean Paoli, C. M. Sperberg-McQueen, Eve Maler, and François Yergeau. Extensible Markup Language (XML) 1.0 (Fifth Edition). W3C Recommendation REC-xml-20081126. World Wide Web Consortium (W3C), 26 November 2008. https://www.w3.org/TR/2008/REC-xml-20081126/.
- Bray, Tim, Dave Hollander, Andrew Layman, Richard Tobin, and Henry S. Thompson. Namespaces in XML 1.0 (Third Edition). W3C Recommendation REC-xml-names-20091208. World Wide Web Consortium (W3C), 8 December 2009. https://www.w3.org/TR/2009/REC-xml-names-20091208/.
- Clark, James. XSL Transformations (XSLT) Version 1.0. W3C Recommendation REC-xslt-19991116. World Wide Web Consortium (W3C), 16 November 1999. https://www.w3.org/TR/1999/REC-xslt-19991116.
- Clark, James and Steve DeRose. XML Path Language (XPath) Version 1.0. W3C Recommendation REC-xpath-19991116. World Wide Web Consortium (W3C), 16 November 1999. https://www.w3.org/TR/1999/REC-xpath-19991116/.
- EXSLT - Common. EXSLT. https://exslt.github.io/exsl/index.html.
- EXSLT - Dates and Times. EXSLT. https://exslt.github.io/date/index.html.
- EXSLT - Dynamic. EXSLT. https://exslt.github.io/dyn/index.html.
- EXSLT - Functions. EXSLT. https://exslt.github.io/func/index.html.
- EXSLT - Math. EXSLT. https://exslt.github.io/math/index.html.
- EXSLT - Regular Expressions. EXSLT. https://exslt.github.io/regexp/index.html.
- EXSLT - Sets. EXSLT. https://exslt.github.io/set/index.html.
- EXSLT - Strings. EXSLT. https://exslt.github.io/str/index.html.
- libxslt 1.1.45. GNOME Project. https://gitlab.gnome.org/GNOME/libxslt.
- Scherer, Markus. Unicode Technical Standard #35: Unicode Locale Data Markup Language (LDML) Part 5: Collation. Version 48.2. Unicode Technical Reports. https://www.unicode.org/reports/tr35/tr35-collation.html.
- Whistler, Ken and Markus Scherer. Unicode Technical Standard #10: Unicode Collation Algorithm. Version 17.0.0, Revision 53. Unicode Technical Reports, 3 September 2025. https://www.unicode.org/reports/tr10/tr10-53.html.
Two are worth a word. EXSLT's own site at exslt.org stopped resolving, so
the URLs above are the project's mirror at exslt.github.io; the namespace
URIs a stylesheet actually writes are unchanged, and are still
http://exslt.org/common and the rest. And crypto is not an EXSLT module at
all — there is no page for it — but a pair of extension functions libxslt
defines in that namespace, which is why libxslt is cited for it as well as for
being the oracle.
Documentation #
$ zig build docs # into zig-out/docs
$ zig build docs-serve # and read it at http://127.0.0.1:8000
It has to be served rather than opened: the viewer fetches its sources and its
WebAssembly at runtime, which a browser refuses to do from a file:// page.