Co-Occurrence Constraints

Another, and considerably more frequent, use of value patterns is to define co-occurrence constraints, in which the value of a node (often an attribute) changes the content model of another node (often an element). In our library, the author and character elements are very similar. You can group them under the person element and use a type attribute to differentiate between the kind of “person” being described. To make the example clearer, to make it more visually obvious that something is different between the two, I’m going add some additional elements describing Peppermint Patty, creating an instance document that contains:

<person id="CMS" type="author">
  <name>Charles M Schulz</name>
  <born>1922-11-26</born>
  <dead>2000-02-12</dead>
</person>

and:

<person id="PP" type="character">
 <name>Peppermint Patty</name>
 <born>1966-08-22</born>
 <qualification>bold, brash and tomboyish</qualification>
 <shoecolor>green</shoecolor>
 <hairstyle>thatched roof</hairstyle>
 <favoriteathlete>that black and white kid with the big nose</favoriteathlete> 
 <likelycareer>olympic coach or unemployed gym teacher</likelycareer>
</person>

You can see that both examples use the person element, yet because of the type attribute’s contents, a different set of child elements is listed. Support for this approach is a key area in which RELAX NG allows more functionality than other schema languages. In these kind of schemas, validation tools need to recognize that the content models might vary ...

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.