Comparison
XSD vs RELAX NG
XSD has the type system and RELAX NG has the grammar. Which matters more depends on whether your documents are records or prose.
XSD's advantage is datatypes: it can say a value is a decimal with two fraction digits, a date, or one of an enumerated list, and reject the ones that are not. RELAX NG deliberately has no datatype library of its own and borrows XSD's when one is wanted.
RELAX NG's advantage is expressing shape. Repetition, alternation and — its real trick — interleave, which says "these children in any order" in one keyword and has no XSD equivalent worth the name. It is also far easier to read, which is why DocBook, TEI and JATS all ship it as their normative grammar.
Both are supported here, and the demonstrations below run the same document and the same defect through each so you can compare not just the verdicts but the wording, which is what you will actually be reading at three in the morning.
What actually differs
| Aspect | XSD | RELAX NG |
|---|---|---|
| Standard | W3C Recommendation | ISO/IEC 19757-2 |
| Written in | XML | XML, or a compact non-XML syntax |
| Datatypes | A large built-in library with facets | None of its own; borrows XSD's |
| Unordered children | xs:all, with real restrictions | interleave, without them |
| Readability | Verbose; a grammar is a document | Compact; the grammar looks like the document |
| Namespace awareness | Central — targetNamespace decides everything | Supported, and less load-bearing |
| Tooling | Everywhere, including code generation | Good, narrower, and no code generation to speak of |
Shown, not asserted
The differences above are claims, so here they are being made. Every one of these runs on each test run, against the same engines the tools use — the outputs are what came back, not what we expected.
The same document against an XSD grammar: valid.
document.xml <?xml version="1.0"?> <note><to>Ada</to></note>schema.xsd <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="note"> <xs:complexType><xs:sequence><xs:element name="to" type="xs:string"/></xs:sequence></xs:complexType> </xs:element> </xs:schema>What came back
Valid against the XSD grammar above.And against a RELAX NG grammar that says the same thing, in a third of the markup.
document.xml <?xml version="1.0"?> <note><to>Ada</to></note>grammar.rng <?xml version="1.0"?> <element name="note" xmlns="http://relaxng.org/ns/structure/1.0"> <element name="to"><text/></element> </element>What came back
Valid against the RELAX NG grammar above.Break the document, and XSD tells you what it expected instead.
document.xml <?xml version="1.0"?> <note><from>Ada</from></note>schema.xsd <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="note"> <xs:complexType><xs:sequence><xs:element name="to" type="xs:string"/></xs:sequence></xs:complexType> </xs:element> </xs:schema>What came back
Invalid against the XSD grammar above.Element 'from': This element is not expected. Expected is ( to ). (line 2)RELAX NG rejects it too, and words it its own way. Both are the same defect.
document.xml <?xml version="1.0"?> <note><from>Ada</from></note>grammar.rng <?xml version="1.0"?> <element name="note" xmlns="http://relaxng.org/ns/structure/1.0"> <element name="to"><text/></element> </element>What came back
Invalid against the RELAX NG grammar above.element from: Relax-NG validity error : Did not expect element from there (line 2)
Which to pick
XSD, when
- Values need checking, not just structure — dates, decimals, ranges, patterns and enumerations.
- You want code generated from the grammar, which is XSD's ecosystem and not RELAX NG's.
- The other end of the wire specified XSD, which for anything B2B they almost certainly did.
RELAX NG, when
- The documents are prose — mixed content, optional children in any order — where XSD's model fights you.
- The grammar has to be read and edited by people, regularly.
- You need interleave. Nothing in XSD does the same job without a combinatorial mess.
Where it reaches
- ToolsXSD validatorValidate against a schema you supply.
- ToolsRELAX NG validatorThe same, for a RELAX NG grammar in its XML syntax.
- FormatsXML Schema (XSD)The format page for schema documents.
- Schema errorsThis element is not expectedThe XSD message above, in full.
Related comparisons
- XSD vs DTDDTDs came with XML and are still the only way to declare an entity. For anything else, XSD has replaced them — and this is the one comparison this site cannot run.
- RSS vs AtomAtom is the better-specified format and RSS is the one everything reads. The real difference is strictness: Atom requires what RSS merely permits.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.