Skip to main content
XMLDir

Search 311 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

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

XSD compared with RELAX NG, one row per aspect
AspectXSDRELAX NG
StandardW3C RecommendationISO/IEC 19757-2
Written inXMLXML, or a compact non-XML syntax
DatatypesA large built-in library with facetsNone of its own; borrows XSD's
Unordered childrenxs:all, with real restrictionsinterleave, without them
ReadabilityVerbose; a grammar is a documentCompact; the grammar looks like the document
Namespace awarenessCentral — targetNamespace decides everythingSupported, and less load-bearing
ToolingEverywhere, including code generationGood, 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.

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.