# XSD, DTD, RelaxNG or Schematron: which one checks what

Source: https://xmlvalid.com/blog/xsd-vs-dtd-vs-relaxng-vs-schematron

> One order, one schema in each language and the same mistakes run through all four, with the messages libxml2 and SaxonJS print

XML has four schema languages in common use, and the comparisons usually stop at a feature table. Tables are fine, but they hide the question that matters: given a document and a mistake in it, which of the four catches it? So here is one small document, one schema in each language, and the same mistakes run through all of them with libxml2 2.15 and SaxonJS. The messages are what they print.

## The document

An order with two lines and a total:

```xml
<order id="A-1042" date="2026-10-30">
  <customer>Beispiel AG</customer>
  <line sku="T-100" qty="4" price="25.00"/>
  <line sku="D-200" qty="1" price="80.00"/>
  <total>180.00</total>
</order>

```

## DTD: the structure, and only the structure

```dtd
<!ELEMENT order (customer, line+, total)>
<!ATTLIST order id ID #REQUIRED date CDATA #REQUIRED>
<!ELEMENT customer (#PCDATA)>
<!ELEMENT line EMPTY>
<!ATTLIST line sku CDATA #REQUIRED qty CDATA #REQUIRED price CDATA #REQUIRED>
<!ELEMENT total (#PCDATA)>

```

A DTD says which elements exist, what they contain, in what order and how often, and which attributes they carry. It is part of XML 1.0 itself, which is why it is still everywhere in publishing (JATS, DocBook, DITA) and in older industry formats.

What it cannot say is what a value looks like. Every attribute here is `CDATA`, any text at all, and every element's content is `#PCDATA`, the same. A date of `30.10.2026`, a quantity of `four`, a SKU of `t100`: the DTD accepts all of them. Move `customer` to the end and it does object:

```
Element order content does not follow the DTD, expecting (customer , line+ , total), got (line line total customer )

```

DTDs can also declare entities, named pieces of text or markup the document reuses, which none of the others can. That is a feature of the document format more than of validation, and the reason a DTD can never be fully replaced in documents that rely on it.

## XSD: structure plus types

```xml
<xs:element name="order">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="customer" type="xs:string"/>
      <xs:element name="line" maxOccurs="unbounded">
        <xs:complexType>
          <xs:attribute name="sku" use="required">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:pattern value="[A-Z]-\d{3}"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
          <xs:attribute name="qty" type="xs:positiveInteger" use="required"/>
          <xs:attribute name="price" type="money" use="required"/>
        </xs:complexType>
      </xs:element>
      <xs:element name="total" type="money"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required"/>
    <xs:attribute name="date" type="xs:date" use="required"/>
  </xs:complexType>
</xs:element>

```

(`money` is `xs:decimal` with at most two fraction digits.) XML Schema adds data types, and with them most of what a DTD lets through:

```
Element 'order', attribute 'date': '30.10.2026' is not a valid value of the atomic type 'xs:date'.
Element 'line', attribute 'qty': 'four' is not a valid value of the atomic type 'xs:positiveInteger'.
Element 'line', attribute 'sku': [facet 'pattern'] The value 't100' is not accepted by the pattern '[A-Z]-\d{3}'.

```

It also understands namespaces, which DTDs predate, and it can say that a value is unique or refers to another one. Add an identity constraint to `order`:

```xml
<xs:unique name="one-line-per-sku">
  <xs:selector xpath="line"/>
  <xs:field xpath="@sku"/>
</xs:unique>

```

and two lines for the same SKU are caught:

```
Element 'line': Duplicate key-sequence ['T-100'] in unique identity-constraint 'one-line-per-sku'.

```

The price is verbosity, and a strictness some formats find awkward: an `xs:sequence` is ordered, so `customer` after the lines fails here too, with `Element 'line': This element is not expected. Expected is ( customer ).` XSD 1.0 is what most industry standards publish (UBL, CII, ISO 20022, FpML, government forms), which makes it the one you will meet most. XSD 1.1 added assertions that can compare values, but few tools implement it, libxml2 among those that don't.

## RelaxNG: the same types, fewer rules about order

```xml
<element name="order" xmlns="http://relaxng.org/ns/structure/1.0"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <attribute name="id"><data type="ID"/></attribute>
  <attribute name="date"><data type="date"/></attribute>
  <interleave>
    <element name="customer"><text/></element>
    <oneOrMore>
      <element name="line">...</element>
    </oneOrMore>
    <element name="total"><data type="decimal">...</data></element>
  </interleave>
</element>

```

RelaxNG borrows XSD's data types, so it catches the same bad values:

```
Type date doesn't allow value '30.10.2026'
Type positiveInteger doesn't allow value 'four'

```

Where it differs is in how content is described. `interleave` means "these, in any order", so the order with `customer` at the end is valid here, which neither the DTD nor the XSD could express without listing every order. RelaxNG can also make content depend on an attribute's value or treat attributes and elements alike in choices, things XSD 1.0 cannot say. It has a compact, non-XML syntax that is pleasant to write, and it is the schema language of ODF, DocBook 5, TEI and Atom, written mostly by people who maintain document formats.

One practical difference with libxml2: its RelaxNG messages are vaguer than its XSD ones. The bad SKU pattern above comes back only as `Element line failed to validate attributes`, without saying which attribute or why.

## Schematron: rules about values

Change the total to `190.00`. The DTD, the XSD and the RelaxNG schema all pass the document, because `190.00` is a perfectly good decimal. None of them can say that a total must equal the sum of its lines.

Schematron can, because it is not a grammar at all. It is a list of XPath assertions, each with a message:

```xml
<rule context="order">
  <assert id="ORD-1" test="xs:decimal(total) = sum(line/(xs:decimal(@qty) * xs:decimal(@price)))">
    The total (<value-of select="total"/>) must equal the sum of quantity × price
    over the lines (<value-of select="sum(line/(xs:decimal(@qty) * xs:decimal(@price)))"/>).
  </assert>
</rule>

```

```
ORD-1  The total (190.00) must equal the sum of quantity × price over the lines (180).

```

Anything XPath can compute, Schematron can check: sums, dates in the right order, a field required only when another one has a certain value, a code that must match a country. It reports in your words rather than the validator's, with your rule ids, which is why business rules are published this way. The EN 16931 rules for European e-invoices, Peppol, XRechnung and most national e-invoicing rules are Schematron.

What Schematron is bad at is structure. Saying "an order contains a customer, then one or more lines, then a total" in assertions is possible and tedious, and it has no notion of data types beyond what XPath offers. That is why it runs alongside a grammar, not instead of one: the XSD makes sure the document has the right shape, then the Schematron checks that the values in that shape make sense.

## Side by side

| Mistake | DTD | XSD | RelaxNG | Schematron |
|---|---|---|---|---|
| Element in the wrong order | caught | caught | allowed by `interleave` | only if you write a rule |
| `date="30.10.2026"` | passes | caught | caught | only if you write a rule |
| `qty="four"` | passes | caught | caught | only if you write a rule |
| SKU `t100` against a pattern | passes | caught | caught, vaguely | only if you write a rule |
| Duplicate SKU | passes | caught with `xs:unique` | passes | caught with a rule |
| Total ≠ sum of lines | passes | passes (XSD 1.0) | passes | caught |

## Which one to use

Usually you do not choose: the format you work with comes with its schema, and you validate against that. When you do choose:

- **DTD** if the format already has one, or you need entities. Not for anything new that has data in it.
- **XSD** for data exchange. It is what partners, code generators and most tools expect.
- **RelaxNG** for document formats written by people, where order is loose and the schema should be readable.
- **Schematron** on top of either, for every rule that relates one value to another.

## Trying them

The [validator](https://xmlvalid.com/validate) takes all four: an XSD (with its imports and includes), a DTD (external or from the document's DOCTYPE), RelaxNG in either syntax (`.rnc` is translated in the browser), and your own Schematron under "Business rules", alone or after a schema. The [XSD](https://xmlvalid.com/xsd-validator), [DTD](https://xmlvalid.com/dtd-validator), [RelaxNG](https://xmlvalid.com/relaxng-validator) and [Schematron](https://xmlvalid.com/schematron-validator) guides cover each one in more detail, with the errors you are most likely to see. The document is checked in the browser in every case. The one thing that goes to our server is a Schematron file of your own, because the browser cannot compile Schematron; it is compiled in a sandbox where the rules can read the document they check and nothing else.

### More from the blog

- [Facture électronique refusée : les erreurs BR-FR les plus fréquentes](https://xmlvalid.com/blog/facture-electronique-refusee-erreurs-br-fr)
- [Peppol-factuur geweigerd: de meest voorkomende fouten in België en Nederland](https://xmlvalid.com/blog/peppol-factuur-geweigerd)
- [XRechnung oder ZUGFeRD: der Unterschied, und was Sie wem senden](https://xmlvalid.com/blog/xrechnung-oder-zugferd)
- [When the PDF and the XML of a ZUGFeRD invoice disagree](https://xmlvalid.com/blog/zugferd-pdfs-that-dont-match-their-xml)
- [E-Rechnung ab 1. Januar 2027: die Checkliste vor der ersten Rechnung](https://xmlvalid.com/blog/e-rechnung-2027-checkliste)
