====== Introduction to MEI and XML Schema ======
===== Why MEI? =====
The **Music Encoding Initiative (MEI)** is a framework for representing music notation in a structured, machine-readable way. It is based on **XML**, which means that MEI files are plain text files with a clear hierarchical structure.
For musicologists, this is useful because it allows us to:
* describe notation precisely
* document editorial decisions
* process musical data computationally
* validate encodings automatically
* exchange data between tools and projects
For the official documentation, see the [[https://music-encoding.org/guidelines/v5/content/introduction.html|MEI Guidelines: Introduction]].
===== Very short version =====
You can think of MEI like this:
* **music notation** = the thing we want to describe
* **XML** = the language used to write that description
* **schema** = the rulebook that says what is allowed
* **MEI** = a music-specific XML vocabulary
So MEI is not "just XML".
It is a **specialized XML language for music notation**.
===== What is XML? =====
XML stands for **eXtensible Markup Language**.
For absolute beginners, the easiest way to understand XML is:
> XML is a way of writing structured information using labeled elements.
Example:
This means:
* the element is called ''note''
* it has a pitch name: ''c''
* it has an octave: ''4''
* it has a duration: ''4''
XML is made of **elements** and **attributes**.
==== Elements ====
Elements are the named building blocks, for example:
* ''''
* ''''
* ''''
* ''''
* ''''
Some elements have opening and closing tags:
...
Some are empty and written in a short form:
==== Attributes ====
Attributes add information to an element.
Example:
Here:
* ''pname'' = pitch name
* ''oct'' = octave
* ''dur'' = duration
A useful beginner distinction is:
* **element** = what kind of thing this is
* **attribute** = extra information about that thing
===== What is a schema? =====
A **schema** is a set of rules for an XML language.
For beginners, the most useful mental model is:
> A schema is like a grammar or rulebook for XML.
It tells us things like:
* which elements are allowed
* where they are allowed
* which attributes they may have
* which values are valid
Without a schema, you could write almost anything.
With a schema, a computer can check whether your file follows the rules.
So a schema helps answer questions like:
* Is a '''' allowed here?
* May a '''' contain a ''''?
* Is ''pname="h"'' valid?
* Does this link point to an existing element?
===== Why schema matters =====
For editorial and research work, schema is important because it helps us avoid errors.
It can catch problems such as:
* invalid element names
* misplaced elements
* missing required information
* invalid attribute values
* broken references
So schema validation is one of the reasons MEI is useful in scholarly work:
* it makes encodings more consistent
* it makes collaboration safer
* it improves interoperability
===== How this works in MEI =====
MEI is an XML-based encoding framework.
Its rules are not invented separately by each project. Instead, MEI defines a structured vocabulary for music notation.
For beginners, the important point is simply:
* XML gives the syntax
* MEI gives the music-specific elements and attributes
* the schema checks whether the encoding is valid
===== A very simple MEI example =====
You can read this as:
* one measure
* containing one staff
* containing one layer
* containing four notes
===== Basic structure in MEI =====
A very common beginner structure is:
score
└── section
└── measure
└── staff
└── layer
└── note / rest / chord / etc.
A practical way to understand this is:
* ''score'' = the whole musical object
* ''measure'' = one bar
* ''staff'' = one staff in that measure
* ''layer'' = one musical stream or voice on that staff
* ''note'' = one event inside that layer
===== Score definition and staff definition =====
Two important elements are:
* ''''
* ''''
These do not usually encode the individual notes themselves.
Instead, they define the notational environment.
You can think of them like this:
* ''scoreDef'' = settings that can apply to the whole score
* ''staffDef'' = settings for a particular staff
These are often used for things such as:
* clef
* key signature
* meter
* staff properties
A useful beginner idea is that these elements often act like **defaults**:
they remain in force until they are changed later.
===== Basic notation in MEI =====
===== Notes =====
In Common Music Notation (CMN), MEI defines a note through three basic parameters:
* ''pname'' = pitch name
* ''oct'' = octave
* ''dur'' = duration
Example:
This means:
* pitch name = C
* octave = 4
* duration = quarter note
===== Pitch =====
Pitch in beginner MEI examples is usually encoded with:
* ''pname''
* ''oct''
Example:
This means G5 as a half note.
So the basic pitch information is split into:
* note name
* octave
===== Accidentals =====
A written accidental can be encoded with the ''accid'' attribute.
Example:
This means an F sharp quarter note.
For beginners, the important idea is:
* ''pname'' gives the diatonic pitch name
* ''accid'' adds the written accidental
In other words:
* ''pname="f"'' = F
* ''accid="s"'' = sharp sign written on that note
===== Duration =====
Duration is encoded with ''dur''.
Common beginner values include:
* ''1'' = whole note
* ''2'' = half note
* ''4'' = quarter note
* ''8'' = eighth note
Example:
===== Measures =====
Measures are encoded with ''''.
Example:
...
Here:
* ''measure'' identifies the bar
* ''n'' stores the measure number or label
The measure acts as a container for the musical material belonging to that bar.
===== Staves and layers =====
Inside a measure, notes are usually organized through:
* ''''
* ''''
Example:
For beginners:
* ''staff'' tells us which staff this is
* ''layer'' tells us which musical stream or voice on that staff
===== Key signatures =====
Key signatures are generally defined at the score or staff level, often through:
* ''''
* ''''
* the ''keysig'' attribute
Example:
A beginner reading of this is:
* ''keysig'' stores the written key signature
* ''1s'' means one sharp
Similarly:
means two flats.
For beginners it is enough to understand that:
* the key signature is usually a **context-setting property**
* it applies to the following music until changed
===== Meter =====
Meter is also typically defined in ''scoreDef'' or ''staffDef''.
The most common beginner attributes are:
* ''meter.count'' = top number
* ''meter.unit'' = bottom number
Example:
This means 4/4.
Another example:
This means 3/8.
===== Clef =====
Clefs are usually part of the score or staff definition as well.
Typical attributes are:
* ''clef.shape''
* ''clef.line''
Example:
This gives a treble clef on line 2.
===== Why ''xml:id'' is especially important =====
One of the most important attributes in MEI is:
xml:id
This gives an element a **unique identifier** inside the document.
Example:
The key idea is:
> ''xml:id'' gives this exact note its own identity.
That matters because other elements can refer to it.
===== What ''xml:id'' does =====
For beginners, ''xml:id'' does three important things:
* it makes an element uniquely identifiable
* it allows linking
* it helps keep references stable
This is especially useful when we want to say:
* a dynamic starts at this note
* a slur begins here and ends there
* this annotation belongs to that symbol
* this facsimile zone corresponds to this note
===== Example: linking a dynamic to a note =====
f
Here:
* the second note has ''xml:id="n10_2"''
* the dynamic uses ''startid="#n10_2"''
* the ''#'' means "point to the element with this id"
So the dynamic is explicitly attached to that note.
===== Why this is important in editorial work =====
For editorial and analytical work, ''xml:id'' is crucial because it lets us refer to specific musical objects.
That is important when:
* encoding slurs, ties, dynamics, and other control elements
* linking notation to facsimile zones
* attaching annotations
* documenting editorial intervention
* aligning musical data across tools
In practical terms:
* the note is no longer just "the third note in bar 10"
* it becomes a uniquely addressable object
===== A helpful mental model =====
A good beginner analogy is:
* the element itself is the musical object
* ''xml:id'' is its name tag
* another element can point to that name tag
Without ''xml:id'', references are harder.
With ''xml:id'', you can point exactly to one specific note or symbol.
===== Do all notes need ''xml:id''? =====
Not always.
In principle, a note can be encoded without ''xml:id''.
Example:
But as soon as you want to point to that note from elsewhere, a unique identifier becomes extremely useful, and often necessary in practice.
For editorial workflows, it is often a very good idea to assign stable ''xml:id'' values systematically.
===== Beginners should remember these core ideas =====
* MEI is an XML language for music notation
* XML consists of elements and attributes
* a schema is the rulebook for valid XML
* notes in MEI are often defined by ''pname'', ''oct'', and ''dur''
* measures group the music into bars
* ''scoreDef'' and ''staffDef'' define context such as clef, key, and meter
* ''xml:id'' gives a specific element a unique identity
* other elements can point to that identity using attributes like ''startid'' and ''endid''
===== Minimal example bringing things together =====
The following snippet shows the core musical logic of notes, accidentals, meter, key signature, and ''xml:id''.
However, it is still only a score fragment.
A complete MEI document usually requires a larger outer structure, which is explained in the next section.
This example shows:
* a score definition with key and meter
* one measure
* one staff
* one layer
* notes with pitch, octave, and duration
* one accidental
* unique ''xml:id'' values
* a dynamic linked to a specific note
===== The overall structure of a complete MEI file =====
So far, many examples in this introduction have shown only small fragments, such as a single measure or a short score passage.
That is useful for learning individual elements, but it is important to understand that a **complete MEI file** normally needs a larger structure around those fragments.
A beginner should think of MEI on two levels:
* **document level** = the whole file
* **music level** = the encoded notation inside the file
===== The usual top-level structure =====
A normal MEI document usually has this overall form:
mei
├── meiHead
└── music
└── body
└── mdiv
└── score
└── section
└── measure
└── staff
└── layer
└── note / rest / chord / etc.
This can be read as:
* '''' = the whole MEI document
* '''' = metadata about the document
* '''' = the musical content
* '''' = the main musical body
* '''' = a musical division, for example a movement or other large unit
* '''' = the score representation
* '''' = a segment of music
* '''' = one measure
* '''' = one staff
* '''' = one layer or voice stream
* '''' = one musical event
===== Why the earlier example was incomplete =====
This earlier example:
is useful as a **score fragment**, but it is not yet a complete MEI document.
In practice, tools such as Verovio often expect a fuller MEI wrapper around the score data.
===== A fuller MEI example =====
The following example shows the same musical idea inside a more complete MEI structure:
Minimal MEI example
**Important:** In a normal five-line staff, '''' should usually include ''lines="5"''.
Without this, some renderers may not display the staff correctly.
You can try to render this score yourself via [[https://editor.verovio.org/|Verovio Editor]]
===== How to read this complete example =====
This example now includes the parts that were missing before:
* '''' as the document root
* ''xmlns'' to declare the MEI XML namespace
* ''meiversion'' to indicate the MEI version
* '''' for metadata
* '''' for the musical content
* '''' and '''' as structural containers around the score
* '''' and '''' to define the notational context
Only inside this larger wrapper do we get to the actual note material.
===== The role of ''meiHead'' =====
The '''' element contains metadata.
For beginners, it is enough to understand that this is where information about the encoded object and the encoding can be stored, for example:
* title
* source information
* editorial responsibility
* encoding notes
* revision history
In small teaching examples, the header may be very short.
In research projects, it can become much more detailed.
===== The role of ''body'' and ''mdiv'' =====
Inside '''', the musical content is usually placed inside ''''.
Inside the body, '''' represents a musical division.
Depending on the source, this may represent:
* a whole piece
* a movement
* an act
* a scene
* another large formal division
For simple teaching examples, one '''' is usually enough.
===== A practical beginner rule =====
When writing MEI for testing or rendering, it is safest to think in this order:
- start with ''''
- add ''''
- add ''''
- inside music, add ''''
- inside body, add ''''
- inside mdiv, add ''''
- inside score, add '''' and ''''
- inside section, add measures, staves, layers, and notes
This avoids the impression that a musical fragment by itself is already a complete MEI file.
===== Simplified memory aid =====
A useful memory formula is:
* **MEI document** = metadata + music
* **music** = body + divisions + score content
* **score content** = sections + measures + staves + layers + events
===== Reference =====
For the official reference documentation, see:
* [[https://music-encoding.org/guidelines/v5/content/introduction.html|MEI Guidelines: Introduction]]