Merging Grammars

In the preceding sections, you have seen how an external grammar can be used as a single pattern. This is useful in cases in which you want to include a content model described by an external schema at a single point, not unlike when you mount a Unix filesystem. The description contained in the external grammar is mounted at the point where you make your reference.

The main drawback to this approach is that you can’t individually reuse the definitions contained in the external schema. To do so, you need a new pattern, with a different meaning, which will let you control how two grammars are merged into a single one.

Merging Without Redefinition

In the simplest case, you will want to reuse patterns defined in common libraries of patterns without modifying them. Let’s say we have defined a grammar with some common patterns, common.rng, which can be reused in many different schemas, such as:

 <?xml version="1.0" encoding="UTF-8"?> <grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"> <define name="element-name"> <element name="name"> <data type="token" datatypeLibrary=""/> </element> </define> <define name="element-born"> <element name="born"> <data type="date"/> </element> </define> <define name="attribute-id"> <attribute name="id"> <data type="ID"/> </attribute> </define> <define name="content-person"> <ref name="attribute-id"/> <ref name="element-name"/> <optional> <ref name="element-born"/> </optional> </define> ...

Get RELAX NG now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.