Digital humanities

Maintained by: David J. Birnbaum ( [Creative Commons BY-NC-SA 3.0 Unported License] Last modified: 2022-01-31T14:36:29+0000

Relax NG practice

Below are some practice Relax NG patterns or problems written in prose. Read the scenario, draft a constraint or response you find most appropriate, and toggle the Show/Hide solution button beside each problem to see what we did.

Beginner tasks

A poem contains stanzas and a stanza contains lines. How would you model that in Relax NG?

This is a modification of the preceding example. A poem may contain stanzas, in which case a stanza contains lines. But not all poems are stanzaic; some just contain lines directly. (Note, though, that if a poem contains stanzas, all of its lines are inside stanzas. The only poems that contain lines directly are those that are not stanzaic.) How would you model that in Relax NG?

A book contains an introduction, a body, and a conclusion, in that order. The introduction and conclusion are optional, but the body is required. The body contains chapters. Chapters begin with a title, followed by paragraphs. The introduction and conclusion contain only paragraphs (no titles). How can you model this in Relax NG?

A paragraph contains a combination of plain text plus zero or more instances of three types of elements: references, dates, and quotes. The elements inside a paragraph may occur in any order, and they all contain only plain text. How would you model this in Relax NG? (Don’t worry about where paragraphs can occur; assume that you’ve specified that elsewhere in your schema.)

Intermediate tasks

In a corpus of plays, the plays all have a title, followed by a cast list, followed by one or more acts. The cast list contains all of the cast members, and each act contains only speeches (no scenes). The speeches contain the speaker’s name as an attribute value, and the textual content of a speech is plain text, with optional stage directions mixed in. How would you represent this in Relax NG?

If your XML contains an element that is a reserved word in Relax NG (an element called <text>, <div>, <list>, etc.), you’ll get an error if you write:

text = element text { text }

How can you declare an element called <text> without raising an error?

You cannot define an element twice in a Relax NG schema. What if paragraphs in the body of your document contain plain text mixed in with footnote elements, but paragraphs inside footnotes contain only plain text, without footnote elements? How can you define paragraphs in different ways in those different contexts?

You might also want to consult our Patterns, anti-patterns, and other Relax NG details.