<schema xmlns="http://www.ascc.net/xml/schematron">
  <pattern name="main">
    <rule context="/*">
      <assert test="name() = 'sonnet'">
        Root element must be &lt;sonnet&gt;.
      </assert>
      <assert test="@type = 'Shakespearean' or
                    @type = 'Petrarchan'">
        Valid values for the type attribute are 
        "Shakespearean" or "Petrarchan."
      </assert>
      <assert test="count(*) &lt; 4">
        The &lt;sonnet&gt; element contains a &lt;author&gt;
        element, an optional &lt;title&gt; element, and a 
        &lt;lines&lt; element. 
      </assert>
      <assert test="*[1] = author">
        The first child of the &lt;sonnet&gt; element must be
        an &lt;author&gt; element. 
      </assert>
      <assert test="(count(*) = 2 and *[2] = lines)   or
                    (count(*) = 3 and 
                     *[2] = title and *[3] = lines)">
        If you use the optional &lt;title&gt; element, the 
        &lt;sonnet&gt; element must contain the &lt;author&gt;, 
        &lt;title&gt; and &lt;lines&gt; elements in that order. 
      </assert>
    </rule>

    <rule context="author">
      <assert test="count(*) &lt; 6">
        An &lt;author&gt; element must have the elements
        &lt;lastName&gt;, &lt;firstName&gt;, &lt;nationality&gt;
        &lt;yearOfBirth&gt; and &lt;yearOfDeath&gt;.  (The
        &lt;yearOfBirth&gt; and &lt;yearOfDeath&gt; elements
        are optional.
      </assert>
      <assert test="*[1] = lastName">
        The first child of the &lt;author&gt; element must be
        a &lt;lastName&gt; element.
      </assert>
      <assert test="*[2] = firstName">
        The second child of the &lt;author&gt; element must be 
        a &lt;firstName&gt; element.
      </assert>
      <assert test="*[3] = nationality">
        The third child of the &lt;author&gt; element must be 
        a &lt;nationality&gt; element.
      </assert>
      <assert test="(count(*) = 4 and *[4] = yearOfBirth)  or 
                    (count(*) = 5 and
                     *[4] = yearOfBirth and *[5] = yearOfDeath)">
        The fourth child of the &lt;author&gt; element must be
        a &lt;yearOfBirth&gt; element. 
      </assert>
    </rule>

    <rule context="lines">
      <assert test="count(line) = 14">
        A sonnet must have 14 &lt;line&gt;s.
      </assert>
      <assert test="count(line) = count(*)">
        The &lt;lines&gt; element can only contain 
        &lt;line&gt; elements. 
      </assert>
    </rule>
  </pattern>
</schema>

