A pure Go XML Schema 1.0 validator. Compile a schema once, then validate XML
from an io.Reader. Validation streams the document: it does not build a
document tree or store the full XML.
Try it in the browser at https://www.jacoelho.com/xsd/.
The library requires Go 1.27 or newer. It accepts XSD 1.0 and XML 1.0 in UTF-8. The supported language is XSD 1.0 with approved errata and selected corrections. XSD 1.1 syntax is not supported.
Supported platforms are Linux, macOS and WebAssembly (GOOS=js GOARCH=wasm).
Windows is not a target and is not tested.
The library does not support DTDs, external entities, XML versions other than
1.0, or xs:redefine.
Schema sources are explicit. The library does not fetch schemas over HTTP or
any other network transport. Instance xsi:schemaLocation and
xsi:noNamespaceSchemaLocation hints never supply components. Validation uses
only the compiled component set and reports missing required declarations or
types as validation errors.
In a Go module, run:
go get github.com/jacoelho/xsdFor a new program, run go mod init example.com/validate in an empty directory
first.
Save this as main.go. The schema declares a root element containing an
integer, and the XML supplies the value 7.
package main
import (
"fmt"
"log"
"strings"
"github.com/jacoelho/xsd"
)
func main() {
schema := []byte(`<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="xs:int"/>
</xs:schema>`)
engine, err := xsd.Compile(xsd.Bytes("schema.xsd", schema))
if err != nil {
log.Fatal(err)
}
if err := engine.Validate(strings.NewReader(`<root>7</root>`)); err != nil {
log.Fatal(err)
}
fmt.Println("valid")
}Run go run . and it prints valid. Replace 7 with hello and validation
returns an error, because the text is not an integer.
Compile checks the schema. Validate checks one XML document against it. A
nil validation error means the document is valid.
The remaining Go examples are helper functions. Add them to your package with the imports shown, merging any existing imports.
xsd.File returns a schema source for a local file. It resolves local includes and
imports relative to that file, respecting xml:base and local file: URIs. The
caller opens and closes the XML document:
import (
"os"
"github.com/jacoelho/xsd"
)
func validateFile(schemaPath, documentPath string) error {
engine, err := xsd.Compile(xsd.File(schemaPath))
if err != nil {
return err
}
f, err := os.Open(documentPath)
if err != nil {
return err
}
defer f.Close()
return engine.Validate(f)
}Compilation and validation return errors for schema violations. They also return
errors for unsupported features, malformed input and exceeded resource limits.
xsderrors.Flatten lists every diagnostic in an error:
import (
"fmt"
"github.com/jacoelho/xsd/xsderrors"
)
func printDiagnostics(err error) {
for _, diagnostic := range xsderrors.Flatten(err) {
fmt.Println(diagnostic)
}
}A structured diagnostic is a *xsderrors.Error, with methods for category,
code, message, path, line and column. Message() omits the cause and Error()
includes it. Several recoverable errors arrive as one *xsderrors.Errors, with
Len() and At() accessors; errors.AsType[*xsderrors.Errors](err) finds
it.
Two predicates classify an error:
xsderrors.IsUnsupported(err)reports an unsupported feature.xsderrors.IsConclusiveValidation(err)returns true only when every independent diagnostic branch establishes a schema violation. Malformed XML and resource failures return false. False can also mean there is not enough evidence to classify the error. It does not mean the XML is valid.
Validation messages include the constraint and the observed value:
value "é" has length 1 character; expected exactly 2
value "1" does not satisfy minInclusive "2.0"
unexpected child element "c"; expected next child ["b"]
invalid attribute "{http://www.w3.org/1999/xlink}actuate": value "bogus" is not one of ["onLoad", "onRequest"]
Names include their namespace when needed. Enumeration choices use canonical
representations, and long values and lists are truncated with .... Identity
errors name the constraint and the field or referenced key. Detailed formatting
runs only after validation fails, including when a union has tried several
member types.
When the schemas are already in memory, pass them all to Compile.
xsd.Bytes(name, data) copies the schema bytes:
import "github.com/jacoelho/xsd"
func compileSchemas(root, types []byte) (*xsd.Engine, error) {
return xsd.Compile(
xsd.Bytes("schema.xsd", root),
xsd.Bytes("types.xsd", types),
)
}Here root can include types.xsd with
<xs:include schemaLocation="types.xsd"/>. Without a resolver, Bytes and
Open sources resolve references only from the sources passed to Compile.
Source names identify documents. Byte-identical copies under different names
are distinct documents, so two copies that declare the same components fail with
schema.duplicate. A resolver that returns one canonical name for equivalent
locations avoids this, for example for a bundled xml.xsd.
xsd.Open(name, opener) reads a schema from a stream. The opener must return a
new independent reader on every call. Compilation applies byte limits from the
first read.
When the opener returns an error, compilation does not close the reader, so the
opener must release anything it acquired first. Otherwise compilation closes the
returned reader and reports close errors, and the caller must not reuse it. The
Open doc comment covers the remaining error cases.
To supply include and import targets on demand, attach a resolver. This example
loads types.xsd from a map:
import (
"github.com/jacoelho/xsd"
"github.com/jacoelho/xsd/xsderrors"
)
func compileIncludes() (*xsd.Engine, error) {
schema := []byte(`<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="types.xsd"/>
<xs:element name="root" type="Root"/>
</xs:schema>`)
sources := map[string]xsd.SchemaSource{
"types.xsd": xsd.Bytes("types.xsd", []byte(`<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Root"><xs:sequence/></xs:complexType>
</xs:schema>`)),
}
resolver := xsd.ResolverFunc(func(base, location string) (xsd.SchemaSource, error) {
source, ok := sources[location]
if !ok {
return xsd.SchemaSource{}, xsderrors.ErrSchemaNotFound
}
return source, nil
})
return xsd.Compile(xsd.Bytes("schema.xsd", schema).WithResolver(resolver))
}The resulting engine accepts <root/>. The map holds sources built once, and
the resolver returns the same value for every reference to a document.
xsd.Bytes copies and hashes its data on every call, and a source built again
for a document that is already loaded is charged against MaxSchemaTotalBytes
again, so a resolver that calls xsd.Bytes per reference pays for each copy
and can exceed that limit.
The map uses only location. A resolver
for nested paths should also use base: the referring schema's source name with
any xml:base in scope applied. An xml:base that is only a query keeps the
path it is applied to: xml:base="?v=2" in a schema named
http://h/dir/main.xsd gives http://h/dir/main.xsd?v=2
(I-058).
base is empty when an xml:base cannot be composed with that name, for
example a relative xml:base in a schema named urn:root.
Return xsderrors.ErrSchemaNotFound for a missing location to allow fallback
resolution. Wrapping a miss, or joining only misses, also allows fallback. Any
error that is not a miss stops compilation, including a miss joined with another
error. A successful result must have a non-empty source name. The resolver
attached to a root source handles every include and import reached from it. The Resolver and
SchemaSource.WithResolver doc comments define wrapped and joined misses,
source identity and repeated loads.
The library has no built-in xml.xsd or xlink.xsd, and their namespace URIs
never trigger HTTP retrieval. Supply them through the same sources or resolver
as any other schema. An import without schemaLocation can use an explicitly
supplied second root, as in compileSchemas above.
The referring schema must import the namespace. The predefined xml prefix
does not replace that import, including for xml:lang and xml:space. The
supplied declarations control attribute types, facets and fixed or default
values. xml:id has ID semantics only when a supplied schema declares it with
an ID-derived type.
The XSD and XSI namespaces are built in and need no import. Supplied schemas cannot replace their declarations. The four XSI attributes have fixed types:
| Attribute | Type |
|---|---|
xsi:type |
QName |
xsi:nil |
boolean |
xsi:noNamespaceSchemaLocation |
anyURI |
xsi:schemaLocation |
list of anyURI |
On assessed elements, each location hint item must be a valid anyURI, and
MaxInstanceTokenBytes bounds each hint attribute. XSI default or fixed values
do not supply missing attributes, so a required attribute must be present.
An engine is safe to share across goroutines. Each validation call has its own document state. The engine keeps a fixed number of idle sessions for reuse, each with the bounded buffers and caches of a session; Resource limits gives the number and the memory they can hold. A call that finds one allocates no session state; further concurrent calls allocate their own without waiting. Copies of an engine share that cache.
For sequential validation with fixed options, create a session. A session clears document state after each call but keeps bounded buffers and caches for the next one:
import (
"strings"
"github.com/jacoelho/xsd"
)
func validateDocuments(engine *xsd.Engine, docs []string) error {
session, err := engine.NewSession(xsd.ValidateOptions{})
if err != nil {
return err
}
for _, doc := range docs {
if err := session.Validate(strings.NewReader(doc)); err != nil {
return err
}
}
return nil
}A session is not safe for concurrent use. Copies of a session share its state,
and overlapping calls fail with xsderrors.CodeValidationSession before the
second call reads its input. A zero or nil Engine or Session, one that
Compile or NewSession did not create, fails with the same code. Use Engine.Validate or separate sessions for
concurrent work. Discard a session to release its buffers.
Default limits bound compilation and validation. Set compilation limits with
CompileWithOptions. Set validation limits for one call with
ValidateWithOptions, or for a session with NewSession.
Compilation and validation are synchronous. Limits cannot interrupt a blocked opener, resolver, file operation or reader. If you need interruption, provide I/O that you can close or otherwise unblock.
Resource limits lists every limit, its default and its zero-value rule.
A document requires one root element. Outside it, only literal XML whitespace, comments and processing instructions are allowed, with an optional XML declaration at the start.
Empty complex content rejects all character data, including whitespace. Element-only content allows XML whitespace around children, even when all children are optional. Comments, processing instructions and empty CDATA sections add no characters, so they do not violate empty content.
An empty element with a default or fixed value takes the schema's value. If
xsi:type changes the type, that value must satisfy the actual type's facets.
The value uses canonical spelling, except that QName and NOTATION values keep
their schema spelling and namespace bindings. Instance prefix declarations
cannot change a schema-supplied QName. Explicit element text uses the
instance's namespaces and must equal any fixed value.
Relative namespace names are accepted as an extension and compared exactly. They do not change whether an attribute is a namespace declaration.
XSD regular expressions support class subtraction, \i and \c, and Unicode
category and block escapes. Category escapes use Unicode 17.0.0 whichever Go
toolchain builds the library. Compiling and matching patterns have work limits.
From a repository checkout, validate a local file with:
go run ./cmd/xmllint --schema schema.xsd document.xml--schema is required. The optional flags --max-errors,
--max-identity-entries and --max-instance-bytes set validation limits. Zero
selects the library default from resource limits. Run make xmllint to build bin/xmllint.
The exit status classifies the run:
| Status | Meaning |
|---|---|
| 0 | The document is valid. |
| 1 | The document was assessed and violates the schema. |
| 2 | The arguments are wrong. |
| 3 | The document was not assessed: the schema did not compile, the document could not be opened, read or closed, a limit was reached, the document is not well-formed, or the result could not be written. |
A hosted build of the validator page is at https://www.jacoelho.com/xsd/. GitHub Pages deploys it from this repository.
To build the Go WebAssembly module and serve the validator page, run:
make webThen open http://127.0.0.1:8765/xsd/, the same path as the hosted page. Validation runs in a Web Worker, and clearing the input cancels active work.
A result is valid, invalid or error:
invalidmeans the document violates the compiled schema.errormeans assessment could not complete, for example because the schema failed to compile, the XML is malformed, a feature is unsupported or a resource limit was reached.
Both unsuccessful results can include structured diagnostics. The page formats the XML only after a valid result. When a valid document cannot be reformatted, for example because the formatted text would exceed the size limit, the page keeps the text as entered and shows the reason under the valid result.
Each worker reuses the compiled schema while the XSD text is unchanged. Changing the XSD replaces it, and a compilation failure leaves no cached schema. Instance errors keep the schema, and terminating the worker releases it.
Run the JavaScript tests with make web-test. The browser integration tests
need the pinned dependencies and Chromium, installed once:
npm ci --prefix web/js
npm exec --prefix web/js -- playwright install chromium
make browser-testmake lint also checks the GitHub Actions workflows with zizmor, which it runs
through uvx, so it needs uv installed and, the
first time, access to PyPI.
These results compare this library with libxml2's xmllint --stream, measured
with make bench-libxml2. This library's figures are from 2026-12-06 and
libxml2's from 2026-11-15, and each CPU ratio divides one by the other. In a
2026-11-29 run, libxml2's CPU times were within 1.3% of the ones in the table.
The machine was an Apple M2 Max with 32 GiB RAM, running Go 1.27.4 and libxml2
2.9.13. In each run both validators used the same generated files and ran
alternately.
CPU time is user plus system time. All measurements include process startup and
schema compilation. Tables before 2026-11-15 timed libxml2 with
/usr/bin/time and bin/xmllint around that wrapper, so their elapsed times are
not directly comparable.
| Workload | Go CPU | libxml2 --stream CPU |
CPU ratio | Go elapsed | libxml2 elapsed | Go peak RSS | libxml2 peak RSS |
|---|---|---|---|---|---|---|---|
| Streaming, 20 MiB | 250.965 ms | 304.398 ms | 0.824 | 252.255 ms | 305.499 ms | 6.70 MiB | 1.91 MiB |
| Streaming, 100 MiB | 1.230 s | 1.487 s | 0.827 | 1.232 s | 1.488 s | 6.80 MiB | 1.91 MiB |
| Streaming, 500 MiB | 6.086 s | 7.367 s | 0.826 | 6.087 s | 7.369 s | 6.83 MiB | 1.91 MiB |
| Streaming, 1 GiB | 12.429 s | 15.075 s | 0.824 | 12.430 s | 15.078 s | 7.02 MiB | 1.91 MiB |
| Streaming, 2 GiB | 24.849 s | 30.106 s | 0.825 | 24.849 s | 30.110 s | 7.02 MiB | 1.91 MiB |
| Identity constraints, 100,000 rows | 249.488 ms | 300.383 ms | 0.831 | 207.832 ms | 301.699 ms | 74.91 MiB | 74.66 MiB |
Benchmark methodology covers measurement rules, reproduction commands, the package each in-process benchmark lives in and session-reuse guidance.
- Architecture covers design and ownership.
- Test workflows covers the conformance corpus and harness.
- The XSD 1.0 reference holds the specification material.