RELAX NG

XML Foundations [./]
Fall 2013 — INFO 242 (CCN 41613)

Erik Wilde, UC Berkeley School of Information
2013-12-04

Creative Commons License [http://creativecommons.org/licenses/by/3.0/]

This work is licensed under a CC
Attribution 3.0 Unported License
[http://creativecommons.org/licenses/by/3.0/]

Contents E. Wilde: RELAX NG

Contents

E. Wilde: RELAX NG

(2) Abstract

Schema languages as a general concept in XML are used to (1) prescribe the allowed document structure, and/or (2) validate a document against a description of what is allowed in a document and what isn't. DTDs and XSDs are particularly important schema languages because DTDs are part of XML itself, and XSD was the first major improvement of the rather limited capabilities of DTDs. Recently, however, XSD is increasingly criticized for its complexity, and the RELAX NG schema language is gaining popularity instead. RELAX NG is a grammar-based schema language (like DTD and XSD), but adds a human-friendly syntax, the ability to use datatypes, and it removes the ability to allow validation to change a document.



E. Wilde: RELAX NG

(3) Design by Committee



E. Wilde: RELAX NG

(4) RELAX NG +/-



E. Wilde: RELAX NG

(5) RELAX NG Syntaxes

xml-technology-syntaxes.png

Principles

Outline (Principles)

  1. Principles [2]
  2. Example [3]
Principles E. Wilde: RELAX NG

(7) Validation



Principles E. Wilde: RELAX NG

(8) Grammars



Example

Outline (Example)

  1. Principles [2]
  2. Example [3]
Example E. Wilde: RELAX NG

(10) DTD and XSD

<!ELEMENT document (heading, chapter) >
<!ELEMENT heading  (#PCDATA) >
<!ELEMENT chapter  (heading, para+) >
<!ATTLIST chapter  id ID #REQUIRED >
<!ELEMENT para     (#PCDATA) >
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="document">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="heading"/>
    <xs:element ref="chapter"/>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
 <xs:element name="heading" type="xs:string"/>
 <xs:element name="chapter">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="heading"/>
    <xs:element name="para" type="xs:string" maxOccurs="unbounded"/>
   </xs:sequence>
   <xs:attribute name="id" type="xs:ID"/>
  </xs:complexType>
 </xs:element>
</xs:schema>


Example E. Wilde: RELAX NG

(11) RELAX NG

<grammar xmlns="http://relaxng.org/ns/structure/1.0">
 <start><ref name="document"/></start>
 <define name="document">
  <element name="document">
   <ref name="heading"/>
   <ref name="chapter"/>
  </element>
 </define>
 <define name="heading">
  <element name="heading"><text/></element>
 </define>
 <define name="chapter">
  <element name="chapter">
   <attribute name="id"><text/></attribute>
   <ref name="heading"/>
   <oneOrMore>
    <element name="para"><text/></element>
   </oneOrMore>
  </element>
 </define>
</grammar>


Example E. Wilde: RELAX NG

(12) RELAX NG Compact Syntax

start    = document

document = element document { heading, chapter }

heading  = element heading { text }

chapter  = element chapter {
    attribute id { text },
    heading,
    element para { text }+
  }


2013-12-04 XML Foundations [./]
Fall 2013 — INFO 242 (CCN 41613)