# $Id$

start = publidoc


publidoc = element publidoc {
   publidoc.attributes,
   publidoc.content
}

publidoc.attributes =
   version.attribute
version.attribute = attribute version { "1.0" }

publidoc.content =
   document
 | top.topic


# =============================================================================
#                                  TOP LEVEL
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ document ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

document = element document { document.attributes, document.content }

document.attributes =
   document.id.attribute
document.id.attribute = attribute xml:id { xsd:ID }

document.content =
   top.head?,
   (division | topic)+

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ topic ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

top.topic = element topic { top.topic.attributes, top.topic.content }

top.topic.attributes =
   topic.id.attribute
 & topic.type.attribute?
topic.id.attribute = attribute xml:id { xsd:ID }
topic.type.attribute = attribute type { xsd:NCName }

top.topic.content =
   top.head?,
   section+


# =============================================================================
#                                 HEAD LEVEL
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ head ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

top.head = element head { top.head.content }
division.head = element head { division.head.content }
component.head = element head { component.head.content }
section.head = element head { section.head.content }
block.head = element head { block.head.content }

top.head.content =
   (title, subtitle?)?
 & identifier*
 & language?
 & copyright?
 & author*
 & publisher?
 & date?
 & place?
 & source*
 & keywordset?
 & subjectset?
 & abstract?
 & cover?
 & annotation*
division.head.content =
   (title, subtitle?)?
 & language?
 & annotation*
component.head.content =
   (title, subtitle?)?
 & language?
 & author*
 & abstract?
 & annotation*
section.head.content =
   (title, subtitle?)?
 & language?
 & abstract?
 & annotation*
block.head.content =
   (title, subtitle?)?

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ title, subtitle ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

title = element title { title.content }
subtitle = element subtitle { title.content }

title.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ identifier ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

identifier.uri = element identifier {
   attribute type { "uri" }, xsd:anyURI
}

identifier.ean = element identifier {
   attribute type { "ean" }, xsd:token {pattern = "\d{13}"}
}

identifier = identifier.uri | identifier.ean

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ language ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

language = element language { language.attributes }

language.attributes =
   lang.attribute
lang.attribute = attribute xml:lang { xsd:language }

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ copyright ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

copyright = element copyright { copyright.content }

copyright.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ author, publisher ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~ author
author = element author { author.content }

author.content =
   firstname?,
   lastname,
   role*

# ~~~~~~ publisher
publisher = element publisher { publisher.content }

publisher.content =
   label,
   link?

# ~~~~~~ firstname
firstname = element firstname { firstname.content }

firstname.content = inlines

# ~~~~~~ lastname
lastname = element lastname { lastname.content }

lastname.content = inlines

# ~~~~~~ role
role = element role { role.attributes }

role.attributes =
   role.of.attribute
role.of.attribute = attribute of { "author" | "illustrator" }

# ~~~~~~ label
label = element label { label.content }

label.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ place ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

place = element place { place.content }

place.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ source ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~ source
source = element source {
   (  (attribute type { "book" }, source.content.book)
    | (attribute type { "file" }, source.content.file)),
   source.content
}

source.content.book =
   identifier.ean,
   title?,
   pagenumber*
source.content.file =
   identifier.uri
source.content =
   annotation*

# ~~~~~~ pagenumber
pagenumber = element pagenumber { pagenumber.attributes }

pagenumber.attributes =
   pagenumber.value.attribute
pagenumber.value.attribute = attribute value { xsd:positiveInteger }

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ keyword & subject ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~ keywordset
keywordset = element keywordset { keywordset.content }

keywordset.content = keyword+

# ~~~~~~ keyword
keyword = element keyword { keyword.content }

keyword.content = mixed { sup*, sub* }

# ~~~~~~ subjectset
subjectset = element subjectset { subjectset.content }

subjectset.content = subject+

# ~~~~~~ subject
subject = element subject { subject.attributes }

subject.attributes =
   subject.id.attribute
subject.id.attribute = attribute id { xsd:NMTOKEN }

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ abstract ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

abstract = element abstract { abstract.content }

abstract.content =
   p+

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cover ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

cover = element cover { cover.content }

cover.content =
   cover.image

cover.image = element image { cover.image.attributes }

cover.image.attributes =
   cover.image.id.attribute
cover.image.id.attribute = attribute id { xsd:NMTOKEN }

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ annotation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

annotation = element annotation { annotation.content }

annotation.content = inlines


# =============================================================================
#                                 DIVISION LEVEL
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ division ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

division = element division { division.content }

division.content =
   division.head?,
   (division | topic)+


# =============================================================================
#                               COMPONENT LEVEL
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ topic ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

topic = element topic { topic.attributes, topic.content }

topic.attributes =
   topic.id.attribute?
 & topic.type.attribute?

topic.content =
   component.head?,
   section+


# =============================================================================
#                                 SECTION LEVEL
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ section ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

section = element section { section.attributes, section.content }

section.attributes =
   section.type.attribute?
section.type.attribute = attribute type { xsd:NCName }

section.content =
   section.head?,
   (section+ | block+)


# =============================================================================
#                                 BLOCK LEVEL
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ block ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

block = (p | speech | \list | blockquote | table | media)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ p ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

p = element p { p.content }

p.content =
   inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ speech ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~ speech
speech = element speech { speech.content }

speech.content =
   speaker?,
   stage?,
   p+

# ~~~~~~ speaker
speaker = element speaker { speaker.content }

speaker.content = inlines

# ~~~~~~ stage
stage = element stage { stage.content }

stage.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ list ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~ list
\list = element list { \list.content }

\list.content =
   block.head?,
   (  (attribute type {"ordered"} ?, item, item+)
    | (attribute type {"glossary"}, glossary.item+) )

# ~~~~~~ item
item = element item { item.content }

item.content =
   inlines
 | block+

# ~~~~~~ glossary.item
glossary.item = element item { glossary.item.content }

glossary.item.content =
   label,
   block+

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ blockquote ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~ blockquote
blockquote = element blockquote { blockquote.content }

blockquote.content =
   block.head?,
   (p | speech | \list)+,
   attribution?

# ~~~~~~ attribution
attribution = element attribution { attribtuion.content }

attribtuion.content =
   inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

table = element table { table.content }

table.content =
   block.head?,
   tgroup+,
   caption?

# ~~~~~~ tgroup
tgroup = element tgroup { tgroup.attributes, tgroup.content }

tgroup.content =
   thead?,
   tfoot?,
   tbody

tgroup.attributes =
   cols.attribute
 & align.attribute?
 & colsep.attribute?
 & rowsep.attribute?
cols.attribute = attribute cols { xsd:positiveInteger }
align.attribute = attribute align { "left"  |  "right"  | "center" | "justify" }
colsep.attribute = attribute colsep { "0" | "1" }
rowsep.attribute = attribute rowsep { "0" | "1" }

# ~~~~~~ thead, tfoot, tbody
thead = element thead { thead.attributes, thead.content }
tfoot = element tfoot { tfoot.attributes, tfoot.content }
tbody = element tbody { tbody.attributes, tbody.content }

thead.attributes =
   valign.attribute?
tfoot.attributes =
   valign.attribute?
tbody.attributes =
   valign.attribute?
valign.attribute = attribute valign { "top" | "middle" | "bottom" }
   
thead.content =
   row+
tfoot.content =
   row+
tbody.content =
   row+

# ~~~~~~ row
row = element row { row.attribute, row.content }

row.attribute =
   valign.attribute?

row.content =
  entry+

# ~~~~~~ entry
entry = element entry { entry.content }

entry.content =
   inlines
 | (p | media)+

# ~~~~~~ caption
caption = element caption { caption.content }

caption.content =
   inlines
 | p+

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ media ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# ~~~~~~ media
media = element media { media.content }

media.content =
   block.head?,
   (image | audio)+,
   caption?,
   link?

# ~~~~~~ image
image = element image { image.attributes, image.content }

image.attributes =
   media.id.attribute,
   image.type?
media.id.attribute = attribute id { xsd:NMTOKEN }
image.type = attribute type { "cover" | "thumbnail" }

image.content =
   copyright?

# ~~~~~~ audio
audio = element audio { audio.attributes }

audio.attributes =
   media.id.attribute


# =============================================================================
#                                 INLINE LEVEL
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ inlines ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

inlines = mixed {
   sup*
 & sub*
 & var*
 & number*
 & math*
 & date*
 & name*
 & note*
 & foreign*
 & highlight*
 & mentioned*
 & quote*
 & acronym*
 & term*
 & link*
 & anchor*
}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ sub, sup, var ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sup = element sup { text }

sub = element sub { text }

var = element var { text }

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ math ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

math = element math {
   ( attribute notation {"tex"}, math.attributes, math.tex.content )
 | ( math.attributes, math.content)
}

math.attributes =
   wide.attribute?
wide.attribute = attribute wide { xsd:boolean }

math.content = mixed { sup* & sub* & var* }
math.tex.content = text

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ number ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

number = element number {
   ( attribute type {"roman"}, number.attributes, number.roman.content )
 | ( number.attributes, number.content)
}

number.attributes =
   number.value.attribute?
number.value.attribute = attribute value { xsd:integer | xsd:decimal }

number.content = mixed { sup* }
number.roman.content = xsd:token {pattern = "[dlxvi]+"}

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ date ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

date = element date { date.attributes, date.content }

date.attributes =
   date.value.attribute
 & date.of.attribute?
date.value.attribute = attribute value { xsd:date | xsd:gYearMonth | xsd:gYear }
date.of.attribute = attribute of { "birth" | "death" }

date.content = mixed { sup* }

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ name ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

name = element name { name.attributes, name.content }

name.attributes =
   name.of.attribute?
name.of.attribute = attribute of
   { "person" | "company" | "book" | "newspaper" | "party" }

name.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ note ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

note = element note { note.attributes, note.content }

note.attributes =
   note.label.attribute?
note.label.attribute = attribute label { text }

note.content =
   inlines
 | p+

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ foreign ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

foreign = element foreign { foreign.attributes, foreign.content }

foreign.attributes =
   lang.attribute?

foreign.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ highlight ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

highlight = element highlight { highlight.content }

highlight.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ mentioned ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

mentioned = element mentioned { mentioned.content }

mentioned.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ quote ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

quote = element quote { quote.content }

quote.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ acronym ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

acronym = element acronym { acronym.content }

acronym.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ term ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

term = element term { term.content }

term.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ link ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

link = element link { link.attributes, link.content }

link.attributes =
   (uri.attribute | idref.attribute)
uri.attribute = attribute uri { xsd:anyURI }
idref.attribute = attribute idref { xsd:IDREF }

link.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ anchor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

anchor = element anchor { anchor.attributes }

anchor.attributes =
   anchor.id.attribute
anchor.id.attribute = attribute xml:id { xsd:ID }
