Defining Named Patterns
RELAX NG uses named patterns to address both modularity and recursion. Named patterns are reusable patterns that can be referenced by their name.
In the XML syntax, named patterns are defined using
define
elements. To define named patterns that contain the
title element, write:
<define name="title-element"> <element name="title"> <text/> </element> </define>
The compact syntax uses a construction similar to a programming language format. The same definition would be written in the compact syntax as:
title-element = element title {text}You’re not limited to embedding a single element or
attribute definition in a named pattern. Note that the group shown in
Figure 5-2, an id attribute, a
name element, and an optional
born element are present in the same order and
with the same definition in both the author and
the character element.
![]() |
To define a named pattern for this group, write:
<define name="common-content"> <attribute name="id"/> <element name="name"> <text/> </element> <optional> <element name="born"> <text/> </element> </optional> </define>
or:
common-content =
attribute id { text },
element name { text },
element born { text }?Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
