<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <!-- $Id$ -->
  <start>
    <ref name="publiset"/>
  </start>
  <define name="publiset">
    <element name="publiset">
      <ref name="publiset.attributes"/>
      <ref name="publiset.content"/>
    </element>
  </define>
  <define name="publiset.attributes">
    <ref name="version.attribute"/>
  </define>
  <define name="version.attribute">
    <attribute name="version">
      <value>1.0</value>
    </attribute>
  </define>
  <define name="publiset.content">
    <oneOrMore>
      <choice>
        <ref name="composition"/>
        <ref name="selection"/>
      </choice>
    </oneOrMore>
  </define>
  <!--
    =============================================================================
                                     TOP LEVEL
    =============================================================================
  -->
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ composition ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="composition">
    <element name="composition">
      <ref name="composition.attributes"/>
      <ref name="composition.content"/>
    </element>
  </define>
  <define name="composition.attributes">
    <interleave>
      <ref name="composition.id.attribute"/>
      <optional>
        <ref name="path.attribute"/>
      </optional>
      <optional>
        <ref name="xpath.attribute"/>
      </optional>
      <optional>
        <ref name="xslt.attribute"/>
      </optional>
      <ref name="remap.attributes"/>
    </interleave>
  </define>
  <define name="composition.id.attribute">
    <attribute name="xml:id">
      <data type="ID"/>
    </attribute>
  </define>
  <define name="path.attribute">
    <attribute name="path">
      <data type="anyURI"/>
    </attribute>
  </define>
  <define name="xslt.attribute">
    <attribute name="xslt">
      <data type="anyURI"/>
    </attribute>
  </define>
  <define name="xpath.attribute">
    <attribute name="xpath"/>
  </define>
  <define name="composition.content">
    <optional>
      <ref name="composition.head"/>
    </optional>
    <oneOrMore>
      <choice>
        <ref name="composition.division"/>
        <ref name="composition.file"/>
      </choice>
    </oneOrMore>
  </define>
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ selection ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="selection">
    <element name="selection">
      <ref name="selection.attributes"/>
      <ref name="selection.content"/>
    </element>
  </define>
  <define name="selection.attributes">
    <interleave>
      <ref name="selection.id.attribute"/>
      <optional>
        <ref name="path.attribute"/>
      </optional>
    </interleave>
  </define>
  <define name="selection.id.attribute">
    <attribute name="xml:id">
      <data type="ID"/>
    </attribute>
  </define>
  <define name="selection.content">
    <optional>
      <ref name="selection.head"/>
    </optional>
    <oneOrMore>
      <choice>
        <ref name="selection.division"/>
        <ref name="selection.file"/>
        <ref name="link"/>
      </choice>
    </oneOrMore>
  </define>
  <!--
    =============================================================================
                                     HEAD LEVEL
    =============================================================================
  -->
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ head ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="composition.head">
    <element name="head">
      <ref name="head.attributes"/>
      <ref name="top.head.content"/>
    </element>
  </define>
  <define name="selection.head">
    <element name="head">
      <ref name="top.head.content"/>
    </element>
  </define>
  <define name="division.head">
    <element name="head">
      <ref name="division.head.content"/>
    </element>
  </define>
  <define name="head.attributes">
    <ref name="remap.attributes"/>
  </define>
  <define name="top.head.content">
    <interleave>
      <optional>
        <ref name="title"/>
        <optional>
          <ref name="subtitle"/>
        </optional>
      </optional>
      <zeroOrMore>
        <ref name="identifier"/>
      </zeroOrMore>
      <optional>
        <ref name="language"/>
      </optional>
      <optional>
        <ref name="copyright"/>
      </optional>
      <zeroOrMore>
        <ref name="author"/>
      </zeroOrMore>
      <optional>
        <ref name="publisher"/>
      </optional>
      <optional>
        <ref name="abstract"/>
      </optional>
      <optional>
        <ref name="cover"/>
      </optional>
    </interleave>
  </define>
  <define name="division.head.content">
    <interleave>
      <optional>
        <ref name="title"/>
        <optional>
          <ref name="subtitle"/>
        </optional>
      </optional>
      <optional>
        <ref name="language"/>
      </optional>
    </interleave>
  </define>
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ title, subtitle ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="title">
    <element name="title">
      <ref name="title.content"/>
    </element>
  </define>
  <define name="subtitle">
    <element name="subtitle">
      <ref name="title.content"/>
    </element>
  </define>
  <define name="title.content">
    <ref name="inlines"/>
  </define>
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ identifier ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="identifier.uri">
    <element name="identifier">
      <attribute name="type">
        <value>uri</value>
      </attribute>
      <data type="anyURI"/>
    </element>
  </define>
  <define name="identifier.ean">
    <element name="identifier">
      <attribute name="type">
        <value>ean</value>
      </attribute>
      <data type="token">
        <param name="pattern">\d{13}</param>
      </data>
    </element>
  </define>
  <define name="identifier">
    <choice>
      <ref name="identifier.uri"/>
      <ref name="identifier.ean"/>
    </choice>
  </define>
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ language ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="language">
    <element name="language">
      <ref name="language.attributes"/>
    </element>
  </define>
  <define name="language.attributes">
    <ref name="lang.attribute"/>
  </define>
  <define name="lang.attribute">
    <attribute name="xml:lang">
      <data type="language"/>
    </attribute>
  </define>
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ copyright ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="copyright">
    <element name="copyright">
      <ref name="copyright.content"/>
    </element>
  </define>
  <define name="copyright.content">
    <ref name="inlines"/>
  </define>
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ author, publisher ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <!-- ~~~~~~ author -->
  <define name="author">
    <element name="author">
      <ref name="author.content"/>
    </element>
  </define>
  <define name="author.attributes">
    <optional>
      <ref name="role.attribute"/>
    </optional>
  </define>
  <define name="role.attribute">
    <attribute name="role">
      <choice>
        <value>author</value>
        <value>illustrator</value>
      </choice>
    </attribute>
  </define>
  <define name="author.content">
    <optional>
      <ref name="firstname"/>
    </optional>
    <ref name="lastname"/>
    <zeroOrMore>
      <ref name="role"/>
    </zeroOrMore>
  </define>
  <!-- ~~~~~~ publisher -->
  <define name="publisher">
    <element name="publisher">
      <ref name="publisher.content"/>
    </element>
  </define>
  <define name="publisher.content">
    <ref name="label"/>
    <optional>
      <ref name="link"/>
    </optional>
  </define>
  <!-- ~~~~~~ firstname -->
  <define name="firstname">
    <element name="firstname">
      <ref name="firstname.content"/>
    </element>
  </define>
  <define name="firstname.content">
    <ref name="inlines"/>
  </define>
  <!-- ~~~~~~ lastname -->
  <define name="lastname">
    <element name="lastname">
      <ref name="lastname.content"/>
    </element>
  </define>
  <define name="lastname.content">
    <ref name="inlines"/>
  </define>
  <!-- ~~~~~~ role -->
  <define name="role">
    <element name="role">
      <ref name="role.attributes"/>
    </element>
  </define>
  <define name="role.attributes">
    <ref name="role.of.attribute"/>
  </define>
  <define name="role.of.attribute">
    <attribute name="of">
      <choice>
        <value>author</value>
        <value>illustrator</value>
      </choice>
    </attribute>
  </define>
  <!-- ~~~~~~ label -->
  <define name="label">
    <element name="label">
      <ref name="label.content"/>
    </element>
  </define>
  <define name="label.content">
    <ref name="inlines"/>
  </define>
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ abstract ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="abstract">
    <element name="abstract">
      <ref name="abstract.content"/>
    </element>
  </define>
  <define name="abstract.content">
    <oneOrMore>
      <ref name="p"/>
    </oneOrMore>
  </define>
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cover ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="cover">
    <element name="cover">
      <ref name="cover.content"/>
    </element>
  </define>
  <define name="cover.content">
    <ref name="cover.image"/>
  </define>
  <define name="cover.image">
    <element name="image">
      <ref name="cover.image.attributes"/>
    </element>
  </define>
  <define name="cover.image.attributes">
    <ref name="cover.image.id.attribute"/>
  </define>
  <define name="cover.image.id.attribute">
    <attribute name="id">
      <data type="NMTOKEN"/>
    </attribute>
  </define>
  <!--
    =============================================================================
                                   DIVISION LEVEL
    =============================================================================
  -->
  <define name="composition.division">
    <element name="division">
      <ref name="composition.division.attributes"/>
      <ref name="composition.division.content"/>
    </element>
  </define>
  <define name="selection.division">
    <element name="division">
      <ref name="selection.division.attributes"/>
      <ref name="selection.division.content"/>
    </element>
  </define>
  <define name="composition.division.attributes">
    <interleave>
      <optional>
        <ref name="path.attribute"/>
      </optional>
      <optional>
        <ref name="xpath.attribute"/>
      </optional>
      <optional>
        <ref name="xslt.attribute"/>
      </optional>
      <ref name="remap.attributes"/>
    </interleave>
  </define>
  <define name="selection.division.attributes">
    <optional>
      <ref name="path.attribute"/>
    </optional>
  </define>
  <define name="composition.division.content">
    <optional>
      <ref name="composition.head"/>
    </optional>
    <oneOrMore>
      <choice>
        <ref name="composition.division"/>
        <ref name="composition.file"/>
      </choice>
    </oneOrMore>
  </define>
  <define name="selection.division.content">
    <optional>
      <ref name="selection.head"/>
    </optional>
    <oneOrMore>
      <choice>
        <ref name="selection.division"/>
        <ref name="selection.file"/>
        <ref name="link"/>
      </choice>
    </oneOrMore>
  </define>
  <!--
    =============================================================================
                                     FILE LEVEL
    =============================================================================
  -->
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="composition.file">
    <element name="file">
      <ref name="composition.file.attributes"/>
    </element>
  </define>
  <define name="selection.file">
    <element name="file">
      <ref name="selection.file.attributes"/>
      <ref name="file.content"/>
    </element>
  </define>
  <define name="composition.file.attributes">
    <interleave>
      <ref name="name.attribute"/>
      <optional>
        <ref name="xpath.attribute"/>
      </optional>
      <optional>
        <ref name="xslt.attribute"/>
      </optional>
    </interleave>
  </define>
  <define name="selection.file.attributes">
    <interleave>
      <ref name="name.attribute"/>
      <optional>
        <ref name="uri.attribute"/>
      </optional>
    </interleave>
  </define>
  <define name="name.attribute">
    <attribute name="name">
      <data type="anyURI"/>
    </attribute>
  </define>
  <define name="file.content">
    <ref name="inlines"/>
  </define>
  <!--
    =============================================================================
                                    BLOCK LEVEL
    =============================================================================
  -->
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ p ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="p">
    <element name="p">
      <ref name="p.content"/>
    </element>
  </define>
  <define name="p.content">
    <ref name="inlines"/>
  </define>
  <!--
    =============================================================================
                                    INLINE LEVEL
    =============================================================================
  -->
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ inlines ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="inlines">
    <mixed>
      <interleave>
        <zeroOrMore>
          <ref name="sup"/>
        </zeroOrMore>
        <zeroOrMore>
          <ref name="sub"/>
        </zeroOrMore>
      </interleave>
    </mixed>
  </define>
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ sub, sup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="sup">
    <element name="sup">
      <ref name="inlines"/>
    </element>
  </define>
  <define name="sub">
    <element name="sub">
      <ref name="inlines"/>
    </element>
  </define>
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ link ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <define name="link">
    <element name="link">
      <ref name="link.attributes"/>
      <ref name="link.content"/>
    </element>
  </define>
  <define name="link.attributes">
    <ref name="uri.attribute"/>
  </define>
  <define name="uri.attribute">
    <attribute name="uri">
      <data type="anyURI"/>
    </attribute>
  </define>
  <define name="link.content">
    <ref name="inlines"/>
  </define>
  <!--
    =============================================================================
                                  REMAP ATTRIBUTES
    =============================================================================
  -->
  <define name="remap.attributes">
    <interleave>
      <optional>
        <ref name="as.attribute"/>
      </optional>
      <optional>
        <ref name="attributes.attribute"/>
      </optional>
    </interleave>
  </define>
  <define name="as.attribute">
    <attribute name="as">
      <data type="QName"/>
    </attribute>
  </define>
  <define name="attributes.attribute">
    <attribute name="attributes">
      <list>
        <oneOrMore>
          <data type="token">
            <param name="pattern">\c+=\S+</param>
          </data>
        </oneOrMore>
      </list>
    </attribute>
  </define>
</grammar>
