Referencing Named Patterns

Defining a named pattern is easy, as shown the earlier example, but referencing a named pattern rather than defining it again is even simpler.

Using the XML syntax, references to named patterns defined elsewhere in the schema are done using a ref element. For instance, to define the author element, use a reference to the name-element pattern:

 <element name="author">
  <attribute name="id"/>
  <ref name="name-element"/>
  <optional>
   <element name="born">
    <text/>
    </element>
  </optional>
  <optional>
   <element name="died">
    <text/>
   </element>
  </optional>
 </element>

To reference a named pattern in the compact syntax, just use its name directly:

element author {
  attribute id { text },
  name-element,
  element born { text }?,
  element died { text }?
}

The same approach can reference the common-content named pattern:

 <element name="author">
  <ref name="common-content"/>
  <optional>
   <element name="died">
    <text/>
   </element>
  </optional>
 </element>

or:

element author {
  common-content,
  element died { text }?
}

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.