A Simple Example

Let’s take a look at an example. Figure 2-1 shows the book element with its two attributes and four different subelements:

A complete example of the book element
Figure 2-1. A complete example of the book element

With a DTD and, to a lesser extent, with W3C XML Schema, you are stuck defining lists of attributes and elements you can’t mix or combine. W3C XML Schema has introduced the concept of types, abstract descriptions that have no direct corollary in the contents of XML documents. Types provide descriptions of the contents of elements or attributes, but types still can’t be freely combined together. This means that you can split the description of elements into blocks such as those shown in Figure 2-2, but can mix the blocks in a limited number of ways.

The blocks of the book element, seen from a W3C XML Schema perspective
Figure 2-2. The blocks of the book element, seen from a W3C XML Schema perspective

RELAX NG patterns, however, can freely mix different types of nodes (elements, text and attributes). Figure 2-3 shows how, if you want to, you can use RELAX NG to split the definition of the book element into a first pattern composed of the attributes id, title, and author and the element character, and then a second pattern composed of the available attribute and the other character elements.

Figure 2-3. An alternate approach to the document structure, made possible with RELAX ...

Get RELAX NG now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.