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>


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

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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.