Recursive Models

As mentioned earlier, named patterns are the only way to represent recursive models. We don’t yet have all the building blocks needed to define a recursive XHTML div element, for example, but let’s start with a simpler example. If our library is divided into categories, each having a title, zero or more embedded category elements, and zero or more books, you can write (assuming that named patterns have been defined for the book element):

<define name="category">
 <element name="category">
  <element name="title">
   <text/>
  </element>
  <zeroOrMore>
   <ref name="category"/>
  </zeroOrMore>
  <zeroOrMore>
   <ref name="book"/>
  </zeroOrMore>
 </element>
</define>

or:

category = element category{
 element title{text},
 category *,
 book*
}

Note that in this case, the recursive reference to the category named pattern must be optional. Otherwise the document is required to have an infinite depth!

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.