RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Discover RELAX NG, A Simple Schema Solution : Page 2

With the goals of compact syntax and compatibility, the Regular Language Description for XML (RELAX NG) eases the pain of working with cumbersome DTDs and XSDs.


Data Types

RELAX NG supports all the data types defined by W3C XML schema data types. In the student roll example, the age attribute refers to the integer defined in datatypeLibrary. Specifying the URI is a good practice, but if you omit it, RELAX NG implicitly uses the default URI in the root element (http://www.w3.org/1999/XMLSchema-datatypes).

Data types also can have parameters. For example, a string data type can have a parameter controlling the length of the string. You specify parameters by adding one or more param elements as children of the data element, as in the following example:

<element name="email">
  <data type="string">
    <param name="maxLength">127</param>

Data Type Plug-ins
The editors of RELAX NG (and I) believe that no universal data type system can exist and that, beyond some very basic universal types, each application domain has its own requirements. RELAX NG defines a generic mechanism for plugging in external type systems. The current implementations support W3C XML Schema data types.

Let's walk through an example of pluggable data type use:

<?xml version="1.0" encoding="UTF-8"?>
  <element name="book">
   <attribute name="isbn">
    <data type="nonNegativeInteger"/>
   <element name="title">
    <data type="token"/>
   <element name="author">
    <data type="token"/>

Note that the data types "nonnegativeInteger" and "token" are defined in the XML schema for XML Schemas: Part 2: Data types. They are not part of the initial data type library. With prior foresight, you can define your own application data types and use the definition across all the applications in your domain.

To define a custom data type within an application, RELAX NG provides the named patterns define and ref to make the definition and reference of user-defined data types easy and flexible:

<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
 <define name="studentID">
  <attribute name="name"> <text/> </attribute>

 <element name="studentRoll">
  <ref name="studentID "/>

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date