Escaping Named Pattern Identifiers in the Compact Syntax

In the previous chapter, I introduced the compact syntax and noted that any word reserved for use by RELAX NG can be used as an element or attribute name. That’s no longer the case for the identifiers of named patterns, because they can appear in the same position as the keywords.

If you want to define a named pattern named “text,” “start,” or “element,” for instance, the identifier of this named pattern can be confused with the keyword. In this case you need to escape being confused with the identifier by a leading backslash. For instance to define (and by extension to make a reference) to a named pattern named “start,” write:

grammar{

start = \start

\start = element start { text }

}

In the XML syntax, this translates into:

<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
  <start>
    <ref name="start"/>
  </start>
  <define name="start">
    <element name="start">
      <text/>
    </element>
  </define>
</grammar>

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.