<grammar xmlns="http://relaxng.org/ns/structure/0.9"
         ns="http://www.w3.org/1999/xhtml"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">

  <xsd:annotation>
    <xsd:documentation>
      XHTML Ruby Module in RELAX NG

        Ruby Elements

          ruby, rbc, rtc, rb, rt, rp

      This module defines grammars to support ruby annotation markup.
      This module is based on the W3C Ruby Annotation Specification:

        http://www.w3.org/TR/ruby

      Revision: $Id: xhtml-ruby-1.rng,v 1.3 2001/06/12 05:19:33 mimasa Exp $

      DISCLAIMER: This RELAX NG module is purely experimental.
        This is basically a rewrite of Ruby RELAX module in RELAX NG.
        Most probably this module contains errors.
    </xsd:documentation>
  </xsd:annotation>

  <div>
    <xsd:annotation>
      <xsd:documentation>
        patterns for the content model of the ruby element
      </xsd:documentation>
    </xsd:annotation>

    <define name="Ruby.content.simple">
      <xsd:annotation>
        <xsd:documentation>Content model of simple ruby</xsd:documentation>
      </xsd:annotation>

      <group>
        <ref name="rb"/>
        <choice>
          <ref name="rt-simple"/>
          <group>
            <ref name="rp"/>
            <ref name="rt-simple"/>
            <ref name="rp"/>
          </group>
        </choice>
      </group>
    </define>

    <define name="Ruby.content.complex">
      <xsd:annotation>
        <xsd:documentation>Content model of complex ruby</xsd:documentation>
      </xsd:annotation>

      <group>
        <ref name="rbc"/>
        <ref name="rtc"/>
        <optional>
          <ref name="rtc"/>
        </optional>
      </group>
    </define>

    <define name="Ruby.content">
      <xsd:annotation>
        <xsd:documentation>Simple ruby is used by default</xsd:documentation>
      </xsd:annotation>

      <ref name="Ruby.content.simple"/>
    </define>
  </div>

  <div>
    <xsd:annotation>
      <xsd:documentation>ruby element</xsd:documentation>
    </xsd:annotation>

    <define name="ruby">
      <element name="ruby">
        <ref name="Ruby.content"/>
        <ref name="Ruby.common.attrib"/>
      </element>
    </define>
  </div>

  <div>
    <xsd:annotation>
      <xsd:documentation>rbc (ruby base component) element</xsd:documentation>
    </xsd:annotation>

    <define name="rbc">
      <element name="rbc">
        <oneOrMore>
          <ref name="rb"/>
        </oneOrMore>
        <ref name="Ruby.common.attrib"/>
      </element>
    </define>
  </div>

  <div>
    <xsd:annotation>
      <xsd:documentation>rtc (ruby text component) element</xsd:documentation>
    </xsd:annotation>

    <define name="rtc">
      <element name="rtc">
        <oneOrMore>
          <ref name="rt-complex"/>
        </oneOrMore>
        <ref name="Ruby.common.attrib"/>
      </element>
    </define>
  </div>

  <div>
    <xsd:annotation>
      <xsd:documentation>rb (ruby base) element</xsd:documentation>
    </xsd:annotation>

    <define name="rb">
      <element name="rb">
        <mixed>
          <zeroOrMore>
            <ref name="NoRuby.content"/>
          </zeroOrMore>
        </mixed>
        <ref name="Ruby.common.attrib"/>
      </element>
    </define>
  </div>

  <div>
    <xsd:annotation>
      <xsd:documentation>rt (ruby text) element</xsd:documentation>
    </xsd:annotation>

    <define name="rt-simple">
      <xsd:annotation>
        <xsd:documentation>
          elementRule for simple ruby

          rbspan attribute is not allowed in simple ruby
        </xsd:documentation>
      </xsd:annotation>

      <element name="rt">
        <mixed>
          <zeroOrMore>
            <ref name="NoRuby.content"/>
          </zeroOrMore>
        </mixed>
        <ref name="Ruby.common.attrib"/>
      </element>
    </define>

    <define name="rt-complex">
      <xsd:annotation>
        <xsd:documentation>
          elementRule for complex ruby
        </xsd:documentation>
      </xsd:annotation>

      <element name="rt">
        <mixed>
          <zeroOrMore>
            <ref name="NoRuby.content"/>
          </zeroOrMore>
        </mixed>
        <ref name="Ruby.common.attrib"/>
        <optional>
          <attribute name="rbspan">
            <data type="positiveInteger"/>
          </attribute>
        </optional>
      </element>
    </define>
  </div>

  <div>
    <xsd:annotation>
      <xsd:documentation>rp (ruby parenthesis) element</xsd:documentation>
    </xsd:annotation>

    <define name="rp">
      <element name="rp">
        <text/>
        <ref name="Ruby.common.attrib"/>
      </element>
    </define>
  </div>

  <div>
   <xsd:annotation>
      <xsd:documentation>
        Ruby Common Attributes

        Ruby elements are intended to have common attributes of its
        parent markup language. The pattern "Ruby.common.attrib" MUST
        be defined to integrate this module.
      </xsd:documentation>
    </xsd:annotation>

    <xsd:annotation>
      <xsd:documentation>
        Content models of the rb and the rt elements are intended to
        allow other inline-level elements of its parent markup language,
        but it should not include ruby descendent elements. The pattern
        'NoRuby.content' MUST be defined to integrate this module.
      </xsd:documentation>
    </xsd:annotation>
  </div>

</grammar>
