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>
</element>
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"?>
<grammar
xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
>
<start>
<element name="book">
<attribute name="isbn">
<data type="nonNegativeInteger"/>
</attribute>
<element name="title">
<data type="token"/>
</element>
<element name="author">
<data type="token"/>
</element>
……….
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>
</define>
<start>
<element name="studentRoll">
<oneOrMore>
<ref name="studentID "/>
</oneOrMore>
</start>
</grammar>