RELAX NG by Eric van der Vlist will be published by O'Reilly & Associates (ISBN: 0596004214)
You are welcome to use our annotation system to give your feedback.
We now have all the patterns needed to write a full schema that would express all we've talked about for this example:
<?xml version = '1.0' encoding = 'utf-8' ?> <element xmlns="http://relaxng.org/ns/structure/1.0" name="library"> <oneOrMore> <element name="book"> <attribute name="id"/> <attribute name="available"/> <element name="isbn"> <text/> </element> <element name="title"> <attribute name="xml:lang"/> <text/> </element> <oneOrMore> <element name="author"> <attribute name="id"/> <element name="name"> <text/> </element> <optional> <element name="born"> <text/> </element> </optional> <optional> <element name="died"> <text/> </element> </optional> </element> </oneOrMore> <zeroOrMore> <element name="character"> <attribute name="id"/> <element name="name"> <text/> </element> <optional> <element name="born"> <text/> </element> </optional> <element name="qualification"> <text/> </element> </element> </zeroOrMore> </element> </oneOrMore> </element> |
RELAX NG directly supports four kinds of occurrence constraints on nodes: they may appear as exactly once (the default), optional, zero or more and one or more. These are the most common cases in document design. If applications need a finer level of control, they can achieve it by using or combining these four basic occurrence constraints. If, for instance, we needed to define that each book's description should have between two and six character elements, we could write the definition as two mandatory characters followed by four optional ones:
<!-- 1 --> <element name="character"> <attribute name="id"/> <element name="name"> <text/> </element> <optional> <element name="born"> <text/> </element> </optional> <element name="qualification"> <text/> </element> </element> <!-- 2 --> <element name="character"> .../... </element> <!-- 3 --> <optional> <element name="character"> .../... </element> </element> </optional> <!-- 4 --> <optional> <element name="character"> .../... </element> </optional> <!-- 5 --> <optional> <element name="character"> .../... </element> </optional> <!-- 6 --> <optional> <element name="character"> .../... </element> </optional> |
This is certainly verbose, but in later chapters we will see how we can define and reuse patterns to reduce verbosity.
Note | |
---|---|
W3C XML Schema offers much more control over how many times an element may appear, but this degree of control creates a number of processing complexities. While RELAX NG's approach may, in this respect, seem less powerful, it compensates by imposing far fewer costs. |
Figure�1 shows the schema and the instance document side by side:
Even though information has been added to the schema that describes the content of the text nodes and the number of their occurrences, the schema keeps the same hierarchical structure as the instance document.
A schema where different definitions are embedded in each other, as this one where the definition of the library element physically contains the definition of the author element, which physically contains the definition of the name element, is often called a "Russian doll" schema after the nested matruschka dolls. We will see, in Chapter 5: Flattening Our First Schema, how Russian doll schemas may be broken into independent patterns and then combined together to reproduce the structure of the instance document. Before we do that, we'll examine the equivalent compact syntax for RELAX NG.
You are welcome to use our annotation system to give your feedback.
[Annotations for this page]
All text is copyright Eric van der Vlist, Dyomedea. During development, I give permission for non-commercial copying for educational and review purposes. After publication, all text will be released under the Free Software Foundation GFDL.