Inhaltsverzeichnis

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:

For the official documentation, see the MEI Guidelines: Introduction.

Very short version

You can think of MEI like this:

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:

<note pname="c" oct="4" dur="4"/>

This means:

XML is made of elements and attributes.

Elements

Elements are the named building blocks, for example:

Some elements have opening and closing tags:

<measure>
  ...
</measure>

Some are empty and written in a short form:

<note pname="c" oct="4" dur="4"/>

Attributes

Attributes add information to an element.

Example:

<note pname="f" oct="5" dur="2"/>

Here:

A useful beginner distinction is:

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:

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:

Why schema matters

For editorial and research work, schema is important because it helps us avoid errors.

It can catch problems such as:

So schema validation is one of the reasons MEI is useful in scholarly work:

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:

A very simple MEI example

<measure n="1">
  <staff n="1">
    <layer n="1">
      <note pname="c" oct="4" dur="4"/>
      <note pname="d" oct="4" dur="4"/>
      <note pname="e" oct="4" dur="4"/>
      <note pname="f" oct="4" dur="4"/>
    </layer>
  </staff>
</measure>

You can read this as:

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 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:

These are often used for things such as:

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:

Example:

<note pname="c" oct="4" dur="4"/>

This means:

Pitch

Pitch in beginner MEI examples is usually encoded with:

Example:

<note pname="g" oct="5" dur="2"/>

This means G5 as a half note.

So the basic pitch information is split into:

Accidentals

A written accidental can be encoded with the accid attribute.

Example:

<note pname="f" oct="4" dur="4" accid="s"/>

This means an F sharp quarter note.

For beginners, the important idea is:

In other words:

Duration

Duration is encoded with dur.

Common beginner values include:

Example:

<note pname="a" oct="4" dur="8"/>

Measures

Measures are encoded with <measure>.

Example:

<measure n="12">
  ...
</measure>

Here:

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:

<measure n="1">
  <staff n="1">
    <layer n="1">
      <note pname="c" oct="4" dur="4"/>
    </layer>
  </staff>
</measure>

For beginners:

Key signatures

Key signatures are generally defined at the score or staff level, often through:

Example:

<scoreDef keysig="1s"/>

A beginner reading of this is:

Similarly:

<scoreDef keysig="2f"/>

means two flats.

For beginners it is enough to understand that:

Meter

Meter is also typically defined in scoreDef or staffDef.

The most common beginner attributes are:

Example:

<scoreDef meter.count="4" meter.unit="4"/>

This means 4/4.

Another example:

<scoreDef meter.count="3" meter.unit="8"/>

This means 3/8.

Clef

Clefs are usually part of the score or staff definition as well.

Typical attributes are:

Example:

<staffDef n="1" clef.shape="G" clef.line="2"/>

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:

<note pname="g" oct="4" dur="4" xml:id="n42"/>

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:

This is especially useful when we want to say:

Example: linking a dynamic to a note

<measure n="10">
  <staff n="1">
    <layer n="1">
      <note pname="f" oct="4" dur="4"/>
      <note pname="g" oct="4" dur="4" xml:id="n10_2"/>
      <note pname="a" oct="4" dur="4"/>
      <note pname="c" oct="5" dur="4"/>
    </layer>
  </staff>
 
  <dynam startid="#n10_2">f</dynam>
</measure>

Here:

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:

In practical terms:

A helpful mental model

A good beginner analogy is:

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:

<note pname="c" oct="4" dur="4"/>

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

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.

<score>
  <scoreDef keysig="1s" meter.count="4" meter.unit="4"/>
  <section>
    <measure n="1">
      <staff n="1">
        <layer n="1">
          <note pname="g" oct="4" dur="4" xml:id="m1n1"/>
          <note pname="a" oct="4" dur="4" accid="s" xml:id="m1n2"/>
          <note pname="b" oct="4" dur="4" xml:id="m1n3"/>
          <note pname="c" oct="5" dur="4" xml:id="m1n4"/>
        </layer>
      </staff>
      <dynam startid="#m1n2">f</dynam>
    </measure>
  </section>
</score>

This example shows:

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:

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:

Why the earlier example was incomplete

This earlier example:

<score>
  <scoreDef keysig="1s" meter.count="4" meter.unit="4"/>
  <section>
    <measure n="1">
      <staff n="1">
        <layer n="1">
          <note pname="g" oct="4" dur="4" xml:id="m1n1"/>
          <note pname="a" oct="4" dur="4" accid="s" xml:id="m1n2"/>
          <note pname="b" oct="4" dur="4" xml:id="m1n3"/>
          <note pname="c" oct="5" dur="4" xml:id="m1n4"/>
        </layer>
      </staff>
      <dynam startid="#m1n2">f</dynam>
    </measure>
  </section>
</score>

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:

<mei xmlns="http://www.music-encoding.org/ns/mei" meiversion="5.1">
  <meiHead>
    <fileDesc>
      <titleStmt>
        <title>Minimal MEI example</title>
      </titleStmt>
      <pubStmt/>
    </fileDesc>
  </meiHead>
 
  <music>
    <body>
      <mdiv>
        <score>
          <scoreDef keysig="1s" meter.count="4" meter.unit="4">
            <staffGrp>
              <staffDef n="1" lines="5" clef.shape="G" clef.line="2"/>
            </staffGrp>
          </scoreDef>
 
          <section>
            <measure n="1">
              <staff n="1">
                <layer n="1">
                  <note pname="g" oct="4" dur="4" xml:id="m1n1"/>
                  <note pname="a" oct="4" dur="4" accid="s" xml:id="m1n2"/>
                  <note pname="b" oct="4" dur="4" xml:id="m1n3"/>
                  <note pname="c" oct="5" dur="4" xml:id="m1n4"/>
                </layer>
              </staff>
              <dynam startid="#m1n2">f</dynam>
            </measure>
          </section>
        </score>
      </mdiv>
    </body>
  </music>
</mei>

Important: In a normal five-line staff, <staffDef> should usually include lines=„5“. Without this, some renderers may not display the staff correctly.

You can try to render this score yourself via Verovio Editor

How to read this complete example

This example now includes the parts that were missing before:

Only inside this larger wrapper do we get to the actual note material.

The role of ''meiHead''

The <meiHead> 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:

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 <music>, the musical content is usually placed inside <body>.

Inside the body, <mdiv> represents a musical division. Depending on the source, this may represent:

For simple teaching examples, one <mdiv> is usually enough.

A practical beginner rule

When writing MEI for testing or rendering, it is safest to think in this order:

  1. start with <mei>
  2. add <meiHead>
  3. add <music>
  4. inside music, add <body>
  5. inside body, add <mdiv>
  6. inside mdiv, add <score>
  7. inside score, add <scoreDef> and <section>
  8. 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:

Reference

For the official reference documentation, see: