ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index
Contents > Simple types

Simple types

Accessing data-types

In XML Schema, the datatype is referenced by the QName. The namespace must be mapped to the prefix.

In XML Schema, the datatype is referenced by the QName. If the XML Schema is the default namespace, then you can reference the datatypes without a prefix.

Relax NG can use the XML Schema datatypes. They can be imported using the "datatypeLibrary" attribute.

...
Restrictions

Restricting simpleType is relatively easy. Here we will require the value of the element "root" to be integer and less than 25.

Relax NG can use the XML Schema datatypes. They can be imported using the "datatypeLibrary" attribute. Restrictions are performed using the "param" element. Let's have the same example as above - "root" must be an integer and less than 25.

...
Multiple restrictions

Now, we want the element "root" to be from the range 0-100 or 300-400 (including the border values). We will make a union from two intervals.

As far as I know, such complex derivations are not possible in Relax NG. However, we can overcome this using the "choice" element. And this should be the same - logical OR corresponds to operation "union".

...
Element is either a fixed string or an integer

Now, we want the element "root" to be either an integer or a string "N/A". We will make a union from an "integer" type and "string" type.

As in previous example, we will use the "choice" element.

...
Element can contain a string from an enumerated set

Now, we want the element "root" to have a value "N/A" or "#REF!".

As in previous example, we will use the "choice" element.

...
Element can contain a mixture of elements

Now, we want the element "root" to contain elements "aaa", "bbb", and "ccc" in any order. We will use the "all" element.

We will use the "interleave" pattern.

...
Defining a group of attributes

Let's say we want to define a group of common attributes, which will be reused. The root element is named "root", it must contain the "aaa" and "bbb" elements, and these elements must have attributes "x" and "y".

Relax NG is not so discriminating when it comes to differences between elements and attributes :-). The default behaviour of Relax NG is as follows: if you do not specify explicitly that the use should be optional, it's implicitly required.

...
Element can have [x,y] either as attributes or as elements

Now, we want the "root" element to have either "x" and "y" attributes, or "x" and "y" elements (but not both at the same time). I do not know, how to do this with XML Schema.

You can easily choose between a group of attributes and a group of elements.

...
Attribute contains a list of values

Now, we want the "root" element to have attribute "xyz", which contains a list of three integers. We will define a general list (element "list") of integers and then restrict it (element "restriction") to have exact length (element "length") of three items.

We will use the "list" type and an "integer" type (the latter borrowed from XML Schema datatypes). It's not possible to easily specify exact number - we must enumerate them.

...
Element contains a list of values

Now, we want the "root" element to contain a list of three integers. We will define a general list (element "list") of integers and then restrict it (element "restriction") to have exact length (element "length") of three items.

We will use the "list" type and an "integer" type (the latter borrowed from XML Schema datatypes).

...