# $Id$

start = publiset


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

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

publiset.content =
   (composition | selection)+


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

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ composition ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

composition.attributes =
   composition.id.attribute
 & path.attribute?
 & (xpath.attribute | xslt.attribute)?
 & remap.attributes
 & lang.attribute?
composition.id.attribute = attribute id { xsd:NMTOKEN }
path.attribute = attribute path { xsd:anyURI }
xslt.attribute = attribute xslt { xsd:anyURI }
xpath.attribute = attribute xpath { text }
lang.attribute = attribute xml:lang { xsd:language }

composition.content =
   composition.head?,
   (composition.division | composition.file)+

# ~~~~~~ composition.head
composition.head = element head {
   composition.head.attributes, composition.head.content }

composition.head.attributes =
   remap.attributes

composition.head.content =
   (title, shorttitle?, subtitle*)?
 & identifier*
 & copyright*
 & collection?
 & contributors?
 & date?
 & (source, source?)?
 & keywordset?
 & subjectset?
 & abstract?
 & cover?

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ selection ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

selection.attributes =
   selection.id.attribute
 & path.attribute?
 & lang.attribute?
selection.id.attribute = attribute id { xsd:NMTOKEN }

selection.content =
   selection.head?,
   (selection.division | selection.file | link)+

# ~~~~~~ selection.head
selection.head = element head { composition.head.content }


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

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

# ~~~~~~ division
composition.division = element division {
   composition.division.attributes,
   composition.division.content
}
selection.division = element division {
   selection.division.attributes,
   selection.division.content
}

composition.division.attributes =
   path.attribute?
 & (xpath.attribute | xslt.attribute)?
 & remap.attributes
selection.division.attributes =
   path.attribute?

composition.division.content =
   composition.head?,
   (composition.division | composition.file)+
selection.division.content =
   selection.head?,
   (selection.division | selection.file | link)+

# ~~~~~~ division.head
division.head = element head { division.head.content }

division.head.content =
   (title, shorttitle?, subtitle*)?


# =============================================================================
#                                  FILE LEVEL
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

composition.file = element file { composition.file.attributes, file.content }
selection.file = element file { selection.file.attributes, file.content }

composition.file.attributes =
   path.attribute?
 & (xpath.attribute | xslt.attribute)?
 & argument.attribute?
selection.file.attributes =
   path.attribute?
argument.attribute = attribute argument { text }
   
file.content = xsd:anyURI


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

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

p = element p { p.content }

p.content =
   inlines


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

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

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

title.content = inlines

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

# ~~~~~~ identifier
identifier = element identifier {
   (attribute type { "ean" }, for.attribute?, xsd:token {pattern = "\d{13}"})
 | (attribute type { "uri" }, for.attribute?, xsd:anyURI)
}

for.attribute = attribute for { xsd:NCName }

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

# ~~~~~~ identifier.uri
identifier.uri = element identifier {
   attribute type { "uri" }, for.attribute?, xsd:anyURI
}

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

copyright = element copyright { copyright.content }

copyright.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ collection ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

collection = element collection { collection.content }

collection.content = inlines

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ contributors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

contributors.content =
   contributor+

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

contributor.content =
   identifier.uri?,
   (((firstname, secondname?)?, lastname) | label),
   address?,
   link?,
   role+

# ~~~~~~ firstname, secondname, lastname, label
firstname = element firstname { firstname.content }
secondname = element secondname { secondname.content }
lastname = element lastname { lastname.content }
label = element label { label.content }

firstname.content = inlines
secondname.content = inlines
lastname.content = inlines
label.content = inlines

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

address.content = inlines

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

role.content = "author" | "illustrator" | "publisher" | "packager"

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

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

source.content.book =
   identifier.ean?
 & title
 & subtitle?
 & copyright?
 & collection?
 & folio?
 & pages?
 
source.content.file =
   identifier.uri

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

folio.content = xsd:positiveInteger | xsd:token {pattern = "[DLXVI]+"}

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

pages.content = 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.content }

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

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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 }


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

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

inlines = mixed {
   sup*
 & sub*
 & date*
 & name*
 & highlight*
 & link*
}

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

sup = element sup { inlines }

sub = element sub { inlines }

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

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

date.attributes =
   date.value.attribute
date.value.attribute = attribute value { xsd:date | xsd:gYearMonth | xsd:gYear }

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" | "painting"
 | "movie" }

name.content = mixed { sup* }

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

highlight = element highlight { highlight.content }

highlight.content = inlines

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

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

link.attributes =
   uri.attribute
uri.attribute = attribute uri { xsd:anyURI }

link.content = mixed { sup* & sub* & date* & name* & highlight* }


# =============================================================================
#                               REMAP ATTRIBUTES
# =============================================================================

remap.attributes =
   as.attribute?
 & attributes.attribute?
 & transform.attribute?
as.attribute = attribute as { xsd:QName }
attributes.attribute =
   attribute attributes {list { (xsd:token {pattern = "\c+=\S+"})+ }}
transform.attribute = attribute transform { xsd:anyURI }
