<?xml version="1.0" encoding="UTF-8"?>

<!-- ************************************************************** -->
<!--
NAME:     File      = 2mei.xsl (version 1.0)
          Vers Date = 2006/09/01

NOTICE:   Copyright (c) 2001-2006 Perry Roland and the Rector and
          Visitors of the University of Virginia.

          Licensed under the Educational Community License version 1.0.

          This Original Work, including software, source code,
          documents, or other related items, is being provided by the
          copyright holder(s) subject to the terms of the Educational
          Community License. By obtaining, using and/or copying this
          Original Work, you agree that you have read, understand, and
          will comply with the following terms and conditions of the
          Educational Community License:

          Permission to use, copy, modify, merge, publish, distribute,
          and sublicense this Original Work and its documentation, with
          or without modification, for any purpose, and without fee or
          royalty to the copyright holder(s) is hereby granted, provided
          that you include the following on ALL copies of the Original
          Work or portions thereof, including modifications or
          derivatives, that you make:

          The full text of the Educational Community License in a
          location viewable to users of the redistributed or derivative
          work. 

          Any pre-existing intellectual property disclaimers, notices,
          or terms and conditions. 

          Notice of any changes or modifications to the Original Work,
          including the date the changes were made. 

          Any modifications of the Original Work must be distributed in
          such a manner as to avoid any confusion with the Original Work
          of the copyright holders.

          THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
          KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
          WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
          PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
          COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
          LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
          OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
          SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

          The name and trademarks of copyright holder(s) may NOT be used
          in advertising or publicity pertaining to the Original or
          Derivative Works without specific, written prior permission.
          Title to copyright in the Original Work and any associated
          documentation will at all times remain with the copyright
          holders.

PURPOSE:  This XSLT stylesheet transforms a time-based MusicXML file to
          a Music Encoding Initiative (MEI) file.
          
AUTHOR:   Perry Roland
          pdr4h@virginia.edu
          University of Virginia
          Charlottesville, VA 22903

NOTES:    Not all MusicXML constructs are converted, but enough to create
          a basic MEI file.  Features not currently transformed are ignored.
          Subsequent versions of this stylesheet will handle these missing
          features.  Since not all features are dealt with, some manual
          revision of the MEI file may be necessary.

TO DO:    

CHANGES: 
                                                                    -->
<!-- ************************************************************** -->

<!DOCTYPE xsl:stylesheet [
<!ENTITY startbeam      "&#xE501;">
<!ENTITY endbeam        "&#xE502;">
<!ENTITY % MusicChars SYSTEM
   'http://text.lib.virginia.edu/charent/musicchar.ent'> %MusicChars;
]>

<xsl:stylesheet
     version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:saxon="http://saxon.sf.net/"
     exclude-result-prefixes="saxon">

  <!-- Stylesheet paramaters -->
  <xsl:param name="accidattr">no</xsl:param> <!-- If set to 'yes', puts accidentals in attributes -->
  <xsl:param name="articattr">no</xsl:param> <!-- If set to 'yes', puts articulations in attributes -->
  <xsl:param name="sylattr">no</xsl:param>   <!-- If set to 'yes', puts lyric syllables in attributes -->
  <xsl:param name="genid">no</xsl:param>     <!-- If set to 'yes', creates an ID for the MEI file -->

  <xsl:character-map name="music-chars">
    <xsl:output-character character="&#x1D110;" string="&amp;ferm;"/>
    <xsl:output-character character="&#x1D111;" string="&amp;fermbelow;"/>
    <xsl:output-character character="&#x1D1B8;" string="&amp;long;"/>
    <xsl:output-character character="&#x1D15C;" string="&amp;breve;"/>
    <xsl:output-character character="&#x1D15D;" string="&amp;note1;"/>
    <xsl:output-character character="&#x1D15E;" string="&amp;note2;"/>
    <xsl:output-character character="&#x1D15F;" string="&amp;note4;"/>
    <xsl:output-character character="&#x1D160;" string="&amp;note8;"/>
  </xsl:character-map>

  <xsl:character-map name="delimiters">
    <xsl:output-character character="&startbeam;" string="&lt;beam&gt;"/>
    <xsl:output-character character="&endbeam;" string="&lt;/beam&gt;"/>
  </xsl:character-map>

  <xsl:output method="xml" indent="no" encoding="UTF-8"
       omit-xml-declaration="no" standalone="no"
       doctype-system="http://www.lib.virginia.edu/digital/resndev/mei/mei17b/mei17b.dtd"
       use-character-maps="music-chars delimiters"/>
       <!-- saxon:next-in-chain="2mei2.xsl"/> -->

  <xsl:strip-space elements="*"/>

  <xsl:variable name="nl"><xsl:text>
</xsl:text>
  </xsl:variable>

  <!-- Create a 'default layout lookup table'. This is used for various
       tasks later. -->
  <xsl:variable name="defaultlayout">
    <xsl:apply-templates select="score-timewise/part-list"/>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:apply-templates select="score-timewise"/>
    <xsl:value-of select="$nl"/>
  </xsl:template>

  <xsl:template match="score-timewise">
    <mei version="1.7b">
      <!-- Create the mei header -->
      <meihead>
        <meiid>
          <!-- Generate an id only on request -->
          <xsl:if test="$genid='yes'">
            <xsl:value-of select="format-dateTime(current-dateTime(), '[Y][M01][D01][H01][m01][s01][f001]')"/>
          </xsl:if>
        </meiid>
        <filedesc>
          <titlestmt>
            <title>
              <xsl:value-of select="work/work-title"/>
              <xsl:if test="work/work-number">
                <xsl:text>, </xsl:text><xsl:value-of select="work/work-number"/>
              </xsl:if>
              <xsl:if test="movement-number">
                <xsl:text>, </xsl:text><xsl:value-of select="movement-number"/>
              </xsl:if>
              <xsl:if test="movement-title">
                <xsl:if test="work/work-title|work/work-number|movement-number">
                  <xsl:text>. </xsl:text>
                </xsl:if>
                <xsl:text>"</xsl:text><xsl:value-of select="movement-title"/><xsl:text>"</xsl:text>
              </xsl:if>
            </title>
            <xsl:if test="identification/creator">
              <respstmt>
                <xsl:for-each select="identification/creator">
                  <xsl:value-of select="$nl"/>
                  <agent>
                    <xsl:attribute name="type"><xsl:value-of select="./@type"/></xsl:attribute>
                    <xsl:value-of select="."/>
                  </agent>
                </xsl:for-each>
              </respstmt>
            </xsl:if>
          </titlestmt>
          <pubstmt>
            <xsl:if test="identification/encoding/encoder">
              <respstmt>
                <xsl:for-each select="identification/encoding/encoder">
                  <agent>
                    <xsl:attribute name="type"><xsl:value-of select="./@type"/></xsl:attribute>
                    <xsl:value-of select="."/>
                  </agent>
                </xsl:for-each>
              </respstmt>
            </xsl:if>
            <xsl:if test="identification/rights">
              <availability>
                <userestrict><xsl:value-of select="identification/rights"/></userestrict>
              </availability>
            </xsl:if>
          </pubstmt>
          <xsl:if test="identification/source | identification/encoding">
            <notesstmt>
              <xsl:for-each select="identification/source | identification/encoding/encoding-date | 
                                    identification/encoding/software | identification/encoding/encoding-description">
                <bibnote>
                  <xsl:attribute name="type"><xsl:value-of select="name(.)"/></xsl:attribute>
                  <xsl:value-of select="."/>
                </bibnote>
              </xsl:for-each>
            </notesstmt>
          </xsl:if>
        </filedesc>
        <profiledesc>
          <langusage>
            <xsl:call-template name="langusage"/>
          </langusage>
        </profiledesc>
        <revisiondesc>
          <change>
            <changedesc>
              <p>
                <xsl:text>Transcoded from MusicXML</xsl:text>
                <xsl:if test="@version">
                  <xsl:text> version </xsl:text>
                  <xsl:value-of select="@version"/>
                </xsl:if>
              </p>
            </changedesc>
            <date><xsl:value-of select="current-date()"/></date>
          </change>
        </revisiondesc>
      </meihead>
      <work>
        <music>
          <mdiv>
            <score>
              <scoredef>
                <xsl:if test="//measure/part/attributes/staff-details/@print-object='no'">
                  <!-- If any staves are not printed, then staff optimization is in effect. -->
                  <xsl:attribute name="optimize">yes</xsl:attribute>
                </xsl:if>

                <!-- Handle score defaults -->
                <xsl:apply-templates select="defaults"/>

                <!-- Create page headers and footers -->
                <xsl:call-template name="credits"/>

                <xsl:copy-of select="$defaultlayout"/>
              </scoredef>

              <!-- Process score measures -->
              <!-- Measures are grouped based on criteria in the following group-ending-with attribute -->
              <xsl:for-each-group select="measure"
                          group-ending-with="measure[part/barline/repeat[@direction='backward'] or 
following-sibling::measure[1][part/barline[@location='left']/repeat[@direction='forward']] or 
part/barline/ending[@type='stop'] or 
part/barline[@location='right']/bar-style='light-light' or 
following-sibling::measure[1][part/barline/ending[@type='start']] or 
following-sibling::measure[1][part/attributes[time or key]]
]">

<!-- Potential (sub?) section-ending conditions:
following-sibling::measure[1][print/page-layout] or
following-sibling::measure[1][print/system-layout] or
following-sibling::measure[1][print/staff-layout] or
following-sibling::measure[1][print/measure-layout] or
following-sibling::measure[1][attributes[staves]] or
following-sibling::measure[1][attributes[not(preceding-sibling::note)]] -->

                <!-- Create sections/endings based on the grouping of measures -->
                <xsl:choose>
                  <xsl:when test="part/barline/ending[@type='start']">
                    <ending>
                      <xsl:attribute name="label">
                        <xsl:choose>
                          <xsl:when test="part/barline/ending[@type='start'] != ''">
                            <xsl:value-of select="part/barline/ending[@type='start']"/>
                          </xsl:when>
                          <xsl:otherwise>
                            <xsl:value-of select="part/barline/ending[@type='start']/@number"/>
                          </xsl:otherwise>
                        </xsl:choose>
                      </xsl:attribute>
                      <xsl:for-each-group select="current-group()" group-starting-with="measure[part/attributes[time or key]]">
                        <xsl:apply-templates select="current-group()"/>
                      </xsl:for-each-group>
                    </ending>
                  </xsl:when>
                  <xsl:otherwise>
                    <section>
                      <xsl:for-each-group select="current-group()" group-starting-with="measure[part/attributes[time or key]]">
                        <xsl:apply-templates select="current-group()"/>
                      </xsl:for-each-group>
                    </section>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each-group>
            </score>
          </mdiv>
        </music>
      </work>
    </mei>
  </xsl:template>

  <xsl:template match="defaults">
    <!-- Handle scaling.  As in MusicXML, Record page scale as a ratio of virtual units (tenths of interline space)
         to real-world units (millimeters) -->
    <xsl:for-each select="scaling">
      <xsl:attribute name="page.scale"><xsl:value-of select="tenths"/>:<xsl:value-of select="millimeters"/></xsl:attribute>
    </xsl:for-each>

    <!-- MusicXML real-world units are millimeters -->
    <xsl:attribute name="page.units">mm</xsl:attribute>

    <!-- Process page layout options -->
    <xsl:for-each select="page-layout">
      <xsl:attribute name="page.height"><xsl:value-of select="page-height"/></xsl:attribute>
      <xsl:attribute name="page.width"><xsl:value-of select="page-width"/></xsl:attribute>
      <xsl:for-each select="page-margins[1]">
        <xsl:attribute name="page.leftmar"><xsl:value-of select="left-margin"/></xsl:attribute>
        <xsl:attribute name="page.rightmar"><xsl:value-of select="right-margin"/></xsl:attribute>
        <xsl:attribute name="page.topmar"><xsl:value-of select="top-margin"/></xsl:attribute>
        <xsl:attribute name="page.botmar"><xsl:value-of select="bottom-margin"/></xsl:attribute>
      </xsl:for-each>
    </xsl:for-each>

    <!-- Process system layout options -->
    <xsl:for-each select="system-layout">
      <xsl:for-each select="system-margins">
        <xsl:attribute name="system.leftmar"><xsl:value-of select="left-margin"/></xsl:attribute>
        <xsl:attribute name="system.rightmar"><xsl:value-of select="right-margin"/></xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="system-distance">
        <xsl:attribute name="spacing.system"><xsl:value-of select="."/></xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="top-system-distance">
        <xsl:attribute name="system.topmar"><xsl:value-of select="."/></xsl:attribute>
      </xsl:for-each>
    </xsl:for-each>

    <!-- Process staff layout options -->
    <xsl:for-each select="staff-layout">
      <xsl:for-each select="staff-distance">
        <xsl:attribute name="spacing.staff"><xsl:value-of select="."/></xsl:attribute>
      </xsl:for-each>
    </xsl:for-each>

    <!-- Process various font options -->
    <xsl:for-each select="music-font">
      <xsl:attribute name="music.name"><xsl:value-of select="@font-family"/></xsl:attribute>
      <xsl:attribute name="music.size"><xsl:value-of select="@font-size"/></xsl:attribute>
    </xsl:for-each>
    <xsl:for-each select="word-font">
      <xsl:attribute name="text.name"><xsl:value-of select="@font-family"/></xsl:attribute>
      <xsl:attribute name="text.size"><xsl:value-of select="@font-size"/></xsl:attribute>
    </xsl:for-each>
    <xsl:for-each select="lyric-font">
      <xsl:attribute name="lyric.name"><xsl:value-of select="@font-family"/></xsl:attribute>
      <xsl:attribute name="lyric.size"><xsl:value-of select="@font-size"/></xsl:attribute>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="part-list">
    <!-- Create the basic content of $defaultlayout. -->
    <xsl:variable name="outergrp">
      <staffgrp>
        <xsl:variable name="temptree">
          <xsl:apply-templates select="score-part|part-group" mode="staffdef"/>
        </xsl:variable>
        <xsl:variable name="temptree2">
          <xsl:apply-templates select="$temptree" mode="countstaves"/>
        </xsl:variable>
        <xsl:copy-of select="$temptree2/staffgrp|$temptree2/staffdef"/>
        <xsl:apply-templates select="$temptree2/part-group[@type='start' and group-symbol='bracket']" mode="bracketelements"/>
        <xsl:apply-templates select="$temptree2/part-group[@type='start' and group-symbol='brace']" mode="braceelements"/>
      </staffgrp>
    </xsl:variable>
    <xsl:apply-templates select="$outergrp" mode="outergrpfix"/>
  </xsl:template>

  <xsl:template match="staffdef|staffgrp" mode="outergrpfix">
    <!-- Continue processing of the contents of $defaultlayout. -->
    <xsl:choose>
      <xsl:when test="name()='staffgrp' and count(ancestor::*)=0">
        <xsl:choose>
          <xsl:when test="count(staffgrp) = count(child::*) and count(child::*) = 1">
            <xsl:apply-templates select="staffgrp" mode="outergrpfix"/> 
          </xsl:when>
          <xsl:otherwise>
            <xsl:copy-of select="(.)"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="(.)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="part-group[@type='start' and group-symbol='bracket']" mode="bracketelements">
    <!-- Create stand-off bracket staff grouping symbols. -->
    <grpsym symbol="bracket">
      <xsl:attribute name="start"><xsl:value-of select="following-sibling::staffdef[1]/@n"/></xsl:attribute>
      <xsl:variable name="level"><xsl:value-of select="@number"/></xsl:variable>
      <xsl:attribute name="end">
        <xsl:for-each select="following-sibling::part-group[@type='stop' and @number=$level][1]">
          <xsl:value-of select="preceding-sibling::staffdef[1]/@n"/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:if test="group-name">
        <xsl:attribute name="label.full"><xsl:value-of select="group-name"/></xsl:attribute>
      </xsl:if>
      <xsl:if test="group-abbreviation">
        <xsl:attribute name="label.abbr"><xsl:value-of select="group-abbreviation"/></xsl:attribute>
      </xsl:if>
      <xsl:if test="group-barline='yes'">
        <xsl:attribute name="barthru">yes</xsl:attribute>
      </xsl:if>
    </grpsym>
  </xsl:template>

  <xsl:template match="part-group[@type='start' and group-symbol='brace']" mode="braceelements">
    <!-- Create stand-off brace staff grouping symbols. -->
    <grpsym symbol="brace">
      <xsl:attribute name="start"><xsl:value-of select="following-sibling::staffdef[1]/@n"/></xsl:attribute>
      <xsl:variable name="level"><xsl:value-of select="@number"/></xsl:variable>
      <xsl:attribute name="end">
        <xsl:for-each select="following-sibling::part-group[@type='stop' and @number=$level][1]">
          <xsl:value-of select="preceding-sibling::staffdef[1]/@n"/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:if test="group-name">
        <xsl:attribute name="label.full"><xsl:value-of select="group-name"/></xsl:attribute>
      </xsl:if>
      <xsl:if test="group-abbreviation">
        <xsl:attribute name="label.abbr"><xsl:value-of select="group-abbreviation"/></xsl:attribute>
      </xsl:if>
      <xsl:if test="group-barline='yes'">
        <xsl:attribute name="barthru">yes</xsl:attribute>
      </xsl:if>
    </grpsym>
  </xsl:template>

  <xsl:template match="staffdef|staffgrp|part-group" mode="countstaves">
    <!-- Number staves in $defaultlayout -->
    <xsl:choose>
      <xsl:when test="name()='staffdef'">
        <staffdef>
          <xsl:attribute name="n"><xsl:value-of select="count(preceding::staffdef) + 1"/></xsl:attribute>
          <xsl:copy-of select="@*|node()"/>
        </staffdef>
      </xsl:when>
      <xsl:when test="name()='staffgrp'">
        <staffgrp>
          <xsl:copy-of select="@*|instrdef"/>
          <xsl:for-each select="staffdef">
            <staffdef>
              <xsl:attribute name="n"><xsl:value-of select="count(preceding::staffdef) + 1"/></xsl:attribute>
              <xsl:copy-of select="@*|node()"/>
            </staffdef>
          </xsl:for-each>
        </staffgrp>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="(.)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="part-group" mode="staffdef">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="score-part" mode="staffdef">
    <!-- Create staffdef elements in $defaultlayout -->
    <xsl:variable name="partID"><xsl:value-of select="@id"/></xsl:variable>
    <xsl:variable name="staves">
      <xsl:choose>
        <xsl:when test="following::measure/part[@id=$partID]/attributes/staves">
          <xsl:value-of select="max(following::measure/part[@id=$partID]/attributes/staves)"/>
        </xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$staves=1">
        <!-- When the part uses a single staff, create a staffdef element and its attributes. -->
        <staffdef>
          <xsl:attribute name="id"><xsl:value-of select="$partID"/></xsl:attribute>
          <xsl:attribute name="label.full"><xsl:value-of select="part-name"/></xsl:attribute>
          <xsl:if test="part-abbreviation!=''">
            <xsl:attribute name="label.abbr"><xsl:value-of select="part-abbreviation"/></xsl:attribute>
          </xsl:if>
          <xsl:choose>
            <xsl:when test="midi-instrument">
              <xsl:for-each select="midi-instrument">
                <instrdef>
                  <xsl:variable name="midiID"><xsl:value-of select="@id"/></xsl:variable>
                  <xsl:attribute name="id"><xsl:value-of select="$midiID"/></xsl:attribute>
                  <xsl:attribute name="midi.channel"><xsl:value-of select="midi-channel"/></xsl:attribute>
                  <xsl:attribute name="midi.instr"><xsl:value-of select="midi-program"/></xsl:attribute>
                  <xsl:attribute name="midi.instrname"><xsl:value-of select="preceding-sibling::score-instrument[@id=$midiID]"/></xsl:attribute>
                </instrdef>
              </xsl:for-each>
            </xsl:when>
            <xsl:when test="score-instrument">
              <xsl:for-each select="score-instrument">
                <instrdef>
                  <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
                  <xsl:attribute name="midi.channel"><xsl:value-of select="count(preceding::score-instrument)+1"/></xsl:attribute>
                  <xsl:attribute name="midi.instr">1</xsl:attribute>
                </instrdef>
              </xsl:for-each>
            </xsl:when>
          </xsl:choose>
        </staffdef>
      </xsl:when>
      <xsl:otherwise>
        <!-- When there's more than one staff in the part, create a staffgrp and its midi instrument
             definitions and then the required number of staves. -->
        <staffgrp>
          <xsl:attribute name="id"><xsl:value-of select="$partID"/></xsl:attribute>
          <xsl:attribute name="symbol">brace</xsl:attribute>
          <xsl:attribute name="label.full"><xsl:value-of select="part-name"/></xsl:attribute>
          <xsl:if test="part-abbreviation!=''">
            <xsl:attribute name="label.abbr"><xsl:value-of select="part-abbreviation"/></xsl:attribute>
          </xsl:if>
          <xsl:for-each select="midi-instrument">
            <instrdef>
              <xsl:variable name="midiID"><xsl:value-of select="@id"/></xsl:variable>
              <xsl:attribute name="id"><xsl:value-of select="$midiID"/></xsl:attribute>
              <xsl:attribute name="midi.channel"><xsl:value-of select="midi-channel"/></xsl:attribute>
              <xsl:attribute name="midi.instr"><xsl:value-of select="midi-program"/></xsl:attribute>
              <xsl:attribute name="midi.instrname"><xsl:value-of select="preceding-sibling::score-instrument[@id=$midiID]"/></xsl:attribute>
            </instrdef>
          </xsl:for-each>
          <xsl:call-template name="makestaffdef">
            <xsl:with-param name="num"><xsl:value-of select="$staves"/></xsl:with-param>
            <xsl:with-param name="label.full"></xsl:with-param>
            <xsl:with-param name="label.abbr"></xsl:with-param>
          </xsl:call-template>
        </staffgrp>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="makestaffdef">
    <!-- This template is called recursively to create the desired number of staves for a part in $defaultlayout. -->
    <xsl:param name="num">1</xsl:param>
    <xsl:param name="label.full"></xsl:param>
    <xsl:param name="label.abbr"></xsl:param>
    <xsl:if test="$num &gt; 0">
      <staffdef>
        <xsl:if test="$label.full!=''">
          <xsl:attribute name="label.full"><xsl:value-of select="$label.full"/></xsl:attribute>
        </xsl:if>
        <xsl:if test="$label.abbr!=''">
          <xsl:attribute name="label.abbr"><xsl:value-of select="$label.abbr"/></xsl:attribute>
        </xsl:if>
      </staffdef>
      <xsl:call-template name="makestaffdef">
        <xsl:with-param name="num"><xsl:value-of select="$num - 1"/></xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name="credits">
    <!-- Transform MusicXML credits element into MEI pghead and pgfoot elements -->
    <xsl:variable name="pageheight"><xsl:value-of select="defaults/page-layout/page-height"/></xsl:variable>
    <xsl:if test="credit/credit-words[@default-y &gt; ($pageheight div 2)]">
      <!-- Credits above the middle of the page go in pghead. -->
      <pghead1><fw>
        <xsl:variable name="creditsrepaired">
          <xsl:apply-templates select="credit" mode="repair"/>
        </xsl:variable>
        <xsl:variable name="creditgroup">
          <xsl:copy-of select="$creditsrepaired//credit-words[@default-y &gt; ($pageheight div 2)]"/>
        </xsl:variable>
        <xsl:variable name="creditgroup2">
          <xsl:for-each-group select="$creditgroup//credit-words" group-by="@default-y">
            <!-- Credits are sorted in the order of appearance down the page and then across the page. -->
            <xsl:sort select="@default-y" order="descending" data-type="number"/>
            <xsl:sort select="@default-x" order="ascending" data-type="number"/>
            <fwr>
              <xsl:attribute name="y"><xsl:value-of select="@default-y"/></xsl:attribute>
              <xsl:copy-of select="current-group()"/>
            </fwr>
          </xsl:for-each-group>
        </xsl:variable>
        <xsl:apply-templates select="$creditgroup2/fwr"/>
      </fw></pghead1>
    </xsl:if>

    <xsl:if test="credit/credit-words[@default-y &lt; ($pageheight div 2)]">
      <!-- Credits below the middle of the page go in pgfoot. -->
      <pgfoot1><fw>
        <xsl:variable name="creditsrepaired">
          <xsl:apply-templates select="credit" mode="repair"/>
        </xsl:variable>
        <xsl:variable name="creditgroup">
          <xsl:copy-of select="$creditsrepaired//credit-words[@default-y &lt; ($pageheight div 2)]"/>
        </xsl:variable>
        <xsl:variable name="creditgroup2">
          <xsl:for-each-group select="$creditgroup//credit-words" group-by="@default-y">
            <xsl:sort select="@default-y" order="descending" data-type="number"/>
            <fwr>
              <xsl:attribute name="y"><xsl:value-of select="@default-y"/></xsl:attribute>
              <xsl:copy-of select="current-group()"/>
            </fwr>
          </xsl:for-each-group>
        </xsl:variable>
        <xsl:apply-templates select="$creditgroup2/fwr"/>
      </fw></pgfoot1>
    </xsl:if>
  </xsl:template>

  <xsl:template match="credit" mode="repair">
    <!-- A kludgey fix to deal with the fact that not all credits have y coordinates -->
    <credit>
      <xsl:for-each select="credit-words">
        <credit-words>
          <xsl:if test="not(@default-y)">
            <xsl:copy-of select="preceding-sibling::credit-words[@default-y][1]/@default-y"/>
          </xsl:if>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates/>
        </credit-words>
      </xsl:for-each>
    </credit>
  </xsl:template>

  <xsl:template match="fwr">
    <fwr>
      <xsl:copy-of select="@*"/>
      <xsl:for-each select="credit-words[@default-y and @default-x]">
        <!-- Within each row sort the cells in order of appearance across the page. -->
        <xsl:sort select="@default-x" order="ascending" data-type="number"/>
        <fwd>
          <xsl:attribute name="x"><xsl:value-of select="@default-x"/></xsl:attribute>
          <xsl:call-template name="textlanguage"/>
          <xsl:call-template name="fontproperties"/>
          <xsl:for-each select="following-sibling::credit-words[not(@default-x)]">
            <!-- When there's no x coordinate on the next credit, make it part of this credit. -->
            <lb/>
            <xsl:call-template name="textlanguage"/>
            <xsl:call-template name="fontproperties"/>
          </xsl:for-each>
        </fwd>
      </xsl:for-each>
    </fwr>
  </xsl:template>

  <xsl:template match="pghead1">
    <pghead1>
      <xsl:for-each select="fw">
        <fw>
          <xsl:copy-of select="@*"/>
          <xsl:for-each select="fwr">
            <xsl:sort select="@y" order="descending" data-type="number"/>
            <fwr>
              <xsl:copy-of select="@*"/>
              <xsl:for-each select="fwd">
                <xsl:sort select="@x" order="ascending" data-type="number"/>
                <xsl:copy-of select="."/>
              </xsl:for-each>
            </fwr>
          </xsl:for-each>
        </fw>          
      </xsl:for-each>
    </pghead1>
  </xsl:template>

  <xsl:template match="pgfoot1">
    <pgfoot1>
      <xsl:for-each select="fw">
        <fw>
          <xsl:copy-of select="@*"/>
          <xsl:for-each select="fwr">
            <xsl:sort select="@y" order="descending" data-type="number"/>
            <fwr>
              <xsl:copy-of select="@*"/>
              <xsl:for-each select="fwd">
                <xsl:sort select="@x" order="ascending" data-type="number"/>
                <xsl:copy-of select="."/>
              </xsl:for-each>
            </fwr>
          </xsl:for-each>
        </fw>          
      </xsl:for-each>
    </pgfoot1>
  </xsl:template>

  <xsl:template name="langusage">
    <xsl:if test="not(contains(string(distinct-values(//*/@xml:lang)), 'it'))">
      <!-- Italian is supposed to be the default language. -->
      <language id="it"/>
    </xsl:if>
    <xsl:for-each select="distinct-values(//*/@xml:lang)">
      <!-- Identify all the languages used anywhere in the document. -->
      <language>
        <xsl:attribute name="id"><xsl:value-of select="."/></xsl:attribute>
      </language>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="fontproperties">
    <!-- When there are typographic properties, wrap a rend sub-element around the content. -->
    <xsl:choose>
      <xsl:when test="@font-family|@font-style|@font-size|@font-weight|@justify|@halign|@valign">
        <rend>
          <xsl:if test="@font-family">
            <xsl:attribute name="fontfam"><xsl:value-of select="@font-family"/></xsl:attribute>
          </xsl:if>
          <xsl:if test="@font-style">
            <xsl:attribute name="fontstyle"><xsl:value-of select="@font-style"/></xsl:attribute>
          </xsl:if>
          <xsl:if test="@font-size">
            <xsl:attribute name="fontsize"><xsl:value-of select="@font-size"/></xsl:attribute>
          </xsl:if>
          <xsl:if test="@font-weight and @font-weight != 'normal'">
            <xsl:attribute name="fontweight"><xsl:value-of select="@font-weight"/></xsl:attribute>
          </xsl:if>
          <xsl:if test="@justify">
            <xsl:attribute name="rend"><xsl:value-of select="@justify"/></xsl:attribute>
          </xsl:if>
          <xsl:if test="@halign|@valign">
              <xsl:copy-of select="@halign|@valign"/>
          </xsl:if>
          <!-- replace a significant linebreak with <lb/> -->
          <xsl:choose>
            <xsl:when test="contains(.,'&#xA;')">
              <xsl:value-of select="substring-before(.,'&#xA;')"/><lb/>
              <xsl:value-of select="substring-after(.,'&#xA;')"/>
            </xsl:when>
            <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
          </xsl:choose>
        </rend>
      </xsl:when>
      <xsl:otherwise>
        <!-- replace a significant linebreak with <lb/> -->
        <xsl:choose>
          <xsl:when test="contains(.,'&#xA;')">
            <xsl:value-of select="substring-before(.,'&#xA;')"/><lb/>
            <xsl:value-of select="substring-after(.,'&#xA;')"/>
          </xsl:when>
          <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="position">
    <!-- Make positional attributes -->
    <xsl:if test="@default-x">
      <xsl:attribute name="x"><xsl:value-of select="@default-x"/></xsl:attribute>
    </xsl:if>
    <xsl:if test="@relative-x">
      <xsl:attribute name="ho"><xsl:value-of select="@relative-x"/></xsl:attribute>
    </xsl:if>
    <xsl:if test="@default-y">
      <xsl:attribute name="y"><xsl:value-of select="@default-y"/></xsl:attribute>
    </xsl:if>
    <xsl:if test="@relative-y">
      <xsl:attribute name="vo"><xsl:value-of select="@relative-y"/></xsl:attribute>
    </xsl:if>
  </xsl:template>

  <xsl:template name="textlanguage">
    <!-- Create lang(uage) attribute -->
    <xsl:if test="@xml:lang">
      <xsl:attribute name="lang"><xsl:value-of select="@xml:lang"/></xsl:attribute>
    </xsl:if>
  </xsl:template>

  <xsl:template match="measure">
    <!-- Process each measure -->
    <!-- Page breaks and system breaks precede the measure. -->
    <xsl:if test="part[print/@new-page='yes']">
      <pb>
        <xsl:attribute name="n"><xsl:value-of select="part[print/@new-page='yes'][1]/print/@page-number"/></xsl:attribute>
      </pb>
    </xsl:if>
    <xsl:if test="part[print/@new-system='yes']">
      <sb/>
    </xsl:if>

    <!-- Score-level info precedes the measure. -->
    <xsl:if test="part/attributes[not(preceding-sibling::note)][time or key]|part[print/page-layout or print/system-layout]">
      <scoredef>
        <xsl:if test="part/attributes[1]/time">
          <xsl:attribute name="meter.count"><xsl:value-of select="part[attributes[1]/time/beats][1]/attributes/time/beats"/></xsl:attribute>
          <xsl:attribute name="meter.unit"><xsl:value-of select="part[attributes[1]/time/beat-type][1]/attributes/time/beat-type"/></xsl:attribute>
          <xsl:choose>
            <xsl:when test="part/attributes[1]/time/@symbol='common'">
              <xsl:attribute name="meter.sym">common</xsl:attribute>
            </xsl:when>
            <xsl:when test="part/attributes[1]/time/@symbol='cut'">
              <xsl:attribute name="meter.sym">cut</xsl:attribute>
            </xsl:when>
            <xsl:when test="part/attributes[1]/time/@symbol='single-number'">
              <xsl:attribute name="meter.rend">denomsym</xsl:attribute>
            </xsl:when>
            <xsl:when test="part/attributes[1]/time/senza-misura">
              <xsl:attribute name="meter.rend">invis</xsl:attribute>
            </xsl:when>
          </xsl:choose>
        </xsl:if>
        <xsl:if test="part/attributes[1]/key">
          <xsl:variable name="keysig"><xsl:value-of select="part[1]/attributes[1]/key/fifths"/></xsl:variable>
          <xsl:choose>
            <xsl:when test="$keysig=0">
              <xsl:attribute name="key.sig"><xsl:value-of select="$keysig"/></xsl:attribute>
            </xsl:when>
            <xsl:when test="$keysig &gt; 0">
              <xsl:attribute name="key.sig"><xsl:value-of select="$keysig"/>s</xsl:attribute>
            </xsl:when>
            <xsl:when test="$keysig &lt; 0">
              <xsl:attribute name="key.sig">
                <xsl:value-of select="abs($keysig)"/>f</xsl:attribute>
            </xsl:when>
          </xsl:choose>
          <xsl:if test="part/attributes[1]/key/mode">
            <xsl:attribute name="key.mode"><xsl:value-of select="part[1]/attributes[1]/key/mode"/></xsl:attribute>
          </xsl:if>
        </xsl:if>

        <!-- Page layout info precedes the measure. -->
        <xsl:for-each select="part[print/page-layout][1]/print/page-layout">
          <xsl:for-each select="page-height">
            <xsl:attribute name="page.height"><xsl:value-of select="."/></xsl:attribute>
          </xsl:for-each>
          <xsl:for-each select="page-width">
            <xsl:attribute name="page.width"><xsl:value-of select="."/></xsl:attribute>
          </xsl:for-each>
          <xsl:for-each select="page-margins">
            <xsl:attribute name="page.leftmar"><xsl:value-of select="left-margin"/></xsl:attribute>
            <xsl:attribute name="page.rightmar"><xsl:value-of select="right-margin"/></xsl:attribute>
            <xsl:attribute name="page.topmar"><xsl:value-of select="top-margin"/></xsl:attribute>
            <xsl:attribute name="page.botmar"><xsl:value-of select="bottom-margin"/></xsl:attribute>
          </xsl:for-each>
        </xsl:for-each>

        <!-- System layout info precedes the measure. -->
        <xsl:for-each select="part[print/system-layout][1]/print/system-layout">
          <xsl:for-each select="system-margins">
            <xsl:attribute name="system.leftmar"><xsl:value-of select="left-margin"/></xsl:attribute>
            <xsl:attribute name="system.rightmar"><xsl:value-of select="right-margin"/></xsl:attribute>
          </xsl:for-each>
          <xsl:for-each select="system-distance">
            <xsl:attribute name="spacing.system"><xsl:value-of select="."/></xsl:attribute>
          </xsl:for-each>
          <xsl:for-each select="top-system-distance">
            <xsl:attribute name="system.topmar"><xsl:value-of select="."/></xsl:attribute>
          </xsl:for-each>
        </xsl:for-each>
      </scoredef>
    </xsl:if>

    <!-- Staff-level info precedes the measure. This process can result in multiple declarations for
         the same staff. -->
    <xsl:for-each select="part">
      <xsl:variable name="partID"><xsl:value-of select="@id"/></xsl:variable>
      <xsl:for-each select="attributes[not(preceding-sibling::note)][1]">
        <!-- Use 'attributes' elements that precede all notes -->
        <xsl:if test="not(preceding-sibling::forward)">
          <!-- 'attributes' also cannot be preceded by a 'forward' element -->
          <xsl:choose>
            <xsl:when test="clef">
              <!-- Record clef(s) -->
              <xsl:for-each select="clef">
                <xsl:sort select="@number"/>
                <staffdef>
                  <xsl:variable name="clefnum">
                    <xsl:choose>
                      <xsl:when test="@number"><xsl:value-of select="@number"/></xsl:when>
                      <xsl:otherwise>1</xsl:otherwise>
                    </xsl:choose>
                  </xsl:variable>
                  <xsl:attribute name="n">
                    <xsl:call-template name="getstaffnum">
                      <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
                      <xsl:with-param name="partstaff"><xsl:value-of select="$clefnum"/></xsl:with-param>
                    </xsl:call-template>
                  </xsl:attribute>

                  <xsl:choose>
                    <xsl:when test="not(sign='percussion')">
                      <xsl:attribute name="clef.line"><xsl:value-of select="line"/></xsl:attribute>
                      <xsl:attribute name="clef.shape"><xsl:value-of select="sign"/></xsl:attribute>
                      <xsl:if test="abs(number(clef-octave-change)) != 0">
                        <xsl:attribute name="clef.trans">
                          <xsl:choose>
                            <xsl:when test="clef-octave-change = '2'">15va</xsl:when>
                            <xsl:when test="clef-octave-change = '1'">8va</xsl:when>
                            <xsl:when test="clef-octave-change = '-1'">8vb</xsl:when>
                            <xsl:when test="clef-octave-change = '-2'">15vb</xsl:when>
                          </xsl:choose>
                        </xsl:attribute>
                      </xsl:if>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:attribute name="clef.shape">perc</xsl:attribute>
                    </xsl:otherwise>
                  </xsl:choose>

                  <!-- MIDI divisions are set per staff. -->
                  <xsl:if test="../divisions">
                    <xsl:attribute name="midi.div"><xsl:value-of select="../divisions"/></xsl:attribute>
                  </xsl:if>

                  <!-- Handle transposed staves, including those transposed by octave -->
                  <xsl:if test="../transpose">
                    <xsl:attribute name="trans.semi">
                      <xsl:choose>
                        <xsl:when test="../transpose/octave-change">
                          <xsl:variable name="octavechange"><xsl:value-of select="../transpose/octave-change"/></xsl:variable>
                          <xsl:variable name="chromatic"><xsl:value-of select="../transpose/chromatic"/></xsl:variable>
                          <xsl:value-of select="$chromatic + (12 * $octavechange)"/>
                        </xsl:when>
                        <xsl:otherwise>
                          <xsl:value-of select="../transpose/chromatic"/>
                        </xsl:otherwise>
                      </xsl:choose>
                    </xsl:attribute>
                    <xsl:if test="../transpose/diatonic">
                      <xsl:attribute name="trans.diat"><xsl:value-of select="../transpose/diatonic"/></xsl:attribute>
                    </xsl:if>
                  </xsl:if>
                </staffdef>
              </xsl:for-each>
            </xsl:when>
            <xsl:when test="divisions or transpose">
              <staffdef>
                <xsl:if test="divisions">
                  <xsl:attribute name="midi.div"><xsl:value-of select="divisions"/></xsl:attribute>
                </xsl:if>

                <xsl:if test="transpose">
                  <xsl:attribute name="trans.semi">
                    <xsl:choose>
                      <xsl:when test="transpose/octave-change">
                        <xsl:variable name="octavechange"><xsl:value-of select="transpose/octave-change"/></xsl:variable>
                        <xsl:variable name="chromatic"><xsl:value-of select="transpose/chromatic"/></xsl:variable>
                        <xsl:value-of select="$chromatic + (12 * $octavechange)"/>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="transpose/chromatic"/>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:attribute>
                  <xsl:if test="transpose/diatonic">
                    <xsl:attribute name="trans.diat"><xsl:value-of select="transpose/diatonic"/></xsl:attribute>
                  </xsl:if>
                </xsl:if>
              </staffdef>
            </xsl:when>
          </xsl:choose>
        </xsl:if>
      </xsl:for-each>

      <!-- Staff layout info precedes the measure -->
      <xsl:for-each select="print/staff-layout">
        <xsl:sort select="@number"/>
        <staffdef>
          <xsl:variable name="clefnum">
            <xsl:choose>
              <xsl:when test="@number"><xsl:value-of select="@number"/></xsl:when>
              <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:attribute name="n">
            <xsl:call-template name="getstaffnum">
              <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
              <xsl:with-param name="partstaff"><xsl:value-of select="$clefnum"/></xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <xsl:attribute name="spacing"><xsl:value-of select="staff-distance"/></xsl:attribute>
        </staffdef>
      </xsl:for-each>
    </xsl:for-each>

    <!-- Hold the measure in $measure for later processing -->
    <xsl:variable name="measure">
      <measure>
        <!-- Measure attributes -->
        <xsl:if test="@implicit='yes'">
          <xsl:attribute name="complete">i</xsl:attribute>
        </xsl:if>
        <xsl:analyze-string select="normalize-space(@number)" regex="^([0-9]+)$">
          <xsl:matching-substring>
            <xsl:attribute name="n"><xsl:value-of select="regex-group(1)"/></xsl:attribute>
          </xsl:matching-substring>
        </xsl:analyze-string>
        <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
        <xsl:choose>
          <xsl:when test="following-sibling::measure[1]/part/barline[@location='left']/bar-style">
          <!-- When the *following measure* has its left barline attribute set, also set the right
               attribute on *this* measure -->
            <xsl:variable name="barstyle"><xsl:value-of select="following-sibling::measure[1]/part[1]/barline/bar-style"/></xsl:variable>
            <xsl:choose>
              <xsl:when test="$barstyle='dotted'">
                <xsl:attribute name="right">dotted</xsl:attribute>
              </xsl:when>
              <xsl:when test="$barstyle='dashed'">
                <xsl:attribute name="right">dashed</xsl:attribute>
              </xsl:when>
              <xsl:when test="$barstyle='light-light'">
                <xsl:attribute name="right">dbl</xsl:attribute>
              </xsl:when>
              <xsl:when test="$barstyle='heavy-light'">
                <xsl:choose>
                  <xsl:when test="following-sibling::measure[1]/part/barline/repeat/@direction='forward'">
                    <xsl:choose>
                      <xsl:when test="part/barline/repeat/@direction='backward'">
                        <xsl:attribute name="right">rptboth</xsl:attribute>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:attribute name="right">rptstart</xsl:attribute>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:attribute name="right">dbl</xsl:attribute>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:when test="$barstyle='light-heavy'">
                <xsl:choose>
                  <xsl:when test="part/barline/repeat/@direction='backward'">
                    <xsl:attribute name="right">rptend</xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:attribute name="right">end</xsl:attribute>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:when test="$barstyle='heavy-heavy'">
                <xsl:choose>
                  <xsl:when test="part/barline/repeat/@direction='backward' and following-sibling::measure[1]/part/barline/repeat/@direction='forward'">
                    <xsl:attribute name="right">rptboth</xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                     <xsl:attribute name="right">dbl</xsl:attribute>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:when test="$barstyle='none'">
                <xsl:attribute name="right">invis</xsl:attribute>
              </xsl:when>
            </xsl:choose>
          </xsl:when>
          <xsl:when test="part/barline[@location='right']/bar-style">
            <!-- Set this measure's right attribute -->
            <xsl:variable name="barstyle"><xsl:value-of select="part[1]/barline/bar-style"/></xsl:variable>
            <xsl:choose>
              <xsl:when test="$barstyle='dotted'">
                <xsl:attribute name="right">dotted</xsl:attribute>
              </xsl:when>
              <xsl:when test="$barstyle='dashed'">
                <xsl:attribute name="right">dashed</xsl:attribute>
              </xsl:when>
              <xsl:when test="$barstyle='light-light'">
                <xsl:attribute name="right">dbl</xsl:attribute>
              </xsl:when>
              <xsl:when test="$barstyle='light-heavy'">
                <xsl:choose>
                  <xsl:when test="part/barline/repeat/@direction='backward'">
                    <xsl:choose>
                      <xsl:when test="following-sibling::measure[1]/part/barline/repeat/@direction='forward'">
                        <xsl:attribute name="right">rptboth</xsl:attribute>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:attribute name="right">rptend</xsl:attribute>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:attribute name="right">end</xsl:attribute>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:when test="$barstyle='heavy-light'">
                <xsl:choose>
                  <xsl:when test="following-sibling::measure[1]/part/barline/repeat[@direction='forward']">
                    <xsl:attribute name="right">rptstart</xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:attribute name="right">dbl</xsl:attribute>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:when test="$barstyle='heavy-heavy'">
                <xsl:choose>
                  <xsl:when test="part/barline/repeat[@direction='backward'] and following-sibling::measure[1]/part/barline/repeat[@direction='forward']">
                    <xsl:attribute name="right">rptboth</xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:attribute name="right">dbl</xsl:attribute>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:when test="$barstyle='none'">
                <xsl:attribute name="right">invis</xsl:attribute>
              </xsl:when>
            </xsl:choose>
          </xsl:when>
          <!-- This stylesheet doesn't handle a barline in the middle of a measure -->
        </xsl:choose>

        <xsl:if test="part/barline[@location='left']/bar-style">
          <!-- Set this measure's left attribute -->
          <xsl:variable name="lbarstyle"><xsl:value-of select="part/barline/bar-style"/></xsl:variable>
          <xsl:choose>
            <xsl:when test="$lbarstyle='dotted'">
              <xsl:attribute name="left">dotted</xsl:attribute>
            </xsl:when>
            <xsl:when test="$lbarstyle='dashed'">
              <xsl:attribute name="left">dashed</xsl:attribute>
            </xsl:when>
            <xsl:when test="$lbarstyle='light-light'">
              <xsl:attribute name="left">dbl</xsl:attribute>
            </xsl:when>
            <xsl:when test="$lbarstyle='light-heavy'">
              <xsl:choose>
                <xsl:when test="part/barline/repeat/@direction='backward'">
                  <xsl:choose>
                    <xsl:when test="preceding-sibling::measure[1]/part/barline/repeat/@direction='backward'">
                      <xsl:attribute name="left">rptend</xsl:attribute>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:attribute name="left">end</xsl:attribute>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:attribute name="right">end</xsl:attribute>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <xsl:when test="$lbarstyle='heavy-light'">
              <xsl:choose>
                <xsl:when test="part/barline/repeat/@direction='forward'">
                  <xsl:attribute name="left">rptstart</xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:attribute name="left">dbl</xsl:attribute>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <xsl:when test="$lbarstyle='heavy-heavy'">
              <xsl:choose>
                <xsl:when test="part/barline/repeat/@direction='forward' and preceding-sibling::measure[1]/part/barline/repeat/@direction='backward'">
                  <xsl:attribute name="left">rptboth</xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:attribute name="left">dbl</xsl:attribute>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <xsl:when test="$lbarstyle='none'">
              <xsl:attribute name="left">invis</xsl:attribute>
            </xsl:when>
          </xsl:choose>
        </xsl:if>

        <xsl:if test="@width">
          <!-- If it exists, grab the measure width -->
          <xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
        </xsl:if>

        <!-- Process measure contents -->
        <xsl:for-each select="part">
          <!-- Events  -->
          <xsl:for-each-group select="note[not(chord)]|backup|attributes[preceding-sibling::note]|
                                      attributes[preceding-sibling::forward]|forward"
                                      group-ending-with="backup">
            <xsl:apply-templates select="current-group()"/>
          </xsl:for-each-group>
        </xsl:for-each>

        <xsl:for-each select="part">
          <!-- Control events -->
          <xsl:variable name="controlevents">
            <xsl:apply-templates select="note/notations/articulations/caesura"/>
            <xsl:apply-templates select="note/notations/dynamics"/>
            <xsl:apply-templates select="direction"/>
            <xsl:apply-templates select="note/notations/tuplet[@type='start']"/>
            <xsl:apply-templates select="note/notations/slur[@type='start' or @type='continue']"/>
            <xsl:apply-templates select="note/notations/fermata"/>
            <xsl:apply-templates select="note/notations/ornaments"/>
            <xsl:apply-templates select="note[not(chord) and notations/arpeggiate]" mode="arpeg"/>
            <xsl:apply-templates select="harmony"/>
            <xsl:apply-templates select="note/notations/technical/pull-off[@type='start']"/>
          </xsl:variable>
          <xsl:for-each select="$controlevents/*">
            <!-- For ease of reading, sort the control events by timestamp -->
            <xsl:sort select="@tstamp"/>
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </xsl:for-each>
      </measure>
    </xsl:variable>

    <!-- Further process $measure -->
    <xsl:for-each select="$measure/measure">
      <measure>
        <xsl:copy-of select="@*"/>
        <!-- Create temporary part elements for use in the step below -->
        <xsl:for-each-group select="chord|clefchange|note|rest|space|msrest" group-by="@part">
          <xsl:variable name="partorg">
          <part>
            <xsl:attribute name="n"><xsl:value-of select="current-grouping-key()"/></xsl:attribute>
            <xsl:for-each-group select="current-group()" group-by="@layer">
              <layer>
                <xsl:attribute name="n"><xsl:value-of select="current-grouping-key()"/></xsl:attribute>
                <xsl:copy-of select="current-group()"/>
              </layer>
            </xsl:for-each-group>
          </part>
          </xsl:variable>

          <!-- Further process $partorg: create staff and layer elements. -->
          <xsl:variable name="stafforg">
            <xsl:for-each select="$partorg/part">
              <xsl:variable name="thispart"><xsl:value-of select="@n"/></xsl:variable>
              <xsl:for-each select="layer">
                <xsl:variable name="thislayer"><xsl:value-of select="@n"/></xsl:variable>
                <xsl:variable name="staves"><xsl:value-of select="distinct-values(*/@staff)"/></xsl:variable>
                <xsl:variable name="countstaves"><xsl:value-of select="count(distinct-values(*/@staff))"/></xsl:variable>
                <xsl:choose>
                  <xsl:when test="$countstaves=1">
                    <!-- The 'voice' lies on a single staff -->
                    <xsl:for-each select="distinct-values(*/@staff)">
                      <staff>
                        <xsl:attribute name="def"><xsl:value-of select="."/></xsl:attribute>
                        <xsl:copy-of select="$partorg/part[@n=$thispart]/layer[@n=$thislayer]/*" />
                      </staff>
                    </xsl:for-each>
                  </xsl:when>
                  <xsl:otherwise>
                    <!-- The 'voice' crosses staves -->
                    <xsl:for-each select="distinct-values(*/@staff)">
                      <xsl:variable name="thisstaff"><xsl:value-of select="."/></xsl:variable>
                      <staff>
                        <xsl:attribute name="def"><xsl:value-of select="$thisstaff"/></xsl:attribute>
                        <xsl:for-each select="$partorg/part[@n=$thispart]/layer[@n=$thislayer]/*">
                          <!-- Fill the unused time on 'the other staff' with space -->
                          <xsl:choose>
                            <xsl:when test="@staff=$thisstaff">
                              <xsl:copy-of select="."/>
                            </xsl:when>
                            <xsl:otherwise>
                              <xsl:apply-templates select="." mode="insertspace"/>
                            </xsl:otherwise>
                          </xsl:choose>
                        </xsl:for-each>
                      </staff>
                    </xsl:for-each>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:for-each>
            </xsl:for-each>
          </xsl:variable>

          <!-- Further process $stafforg: sort by staff, then by layer -->
          <xsl:variable name="stafforg2">
            <xsl:for-each select="$stafforg/staff">
              <xsl:sort select="@def"/>
              <staff>
                <xsl:copy-of select="@*"/>
                <xsl:for-each-group select="child::*" group-by="@layer">
                  <xsl:sort select="current-grouping-key()"/>
                  <layer>
                    <xsl:attribute name="def"><xsl:value-of select="current-grouping-key()"/></xsl:attribute>
                    <xsl:copy-of select="current-group()"/>
                  </layer>
                </xsl:for-each-group>
              </staff>
            </xsl:for-each>
          </xsl:variable>

          <!-- Further process $stafforg2: create beam elements -->
          <xsl:variable name="stafforg3">
            <xsl:for-each-group select="$stafforg2/staff" group-by="@def">
              <xsl:variable name="thisstaff"><xsl:value-of select="current-grouping-key()"/></xsl:variable>
              <xsl:variable name="stafflayer">
                <staff>
                  <xsl:copy-of select="@*"/>
                  <xsl:copy-of select="$stafforg2/staff[@def=$thisstaff]/layer" />
                </staff>
              </xsl:variable>
              <xsl:for-each select="$stafflayer/staff">
                <staff>
                  <xsl:copy-of select="@*"/>
                  <xsl:for-each select="layer">
                    <xsl:sort select="@def"/>
                    <layer>
                      <xsl:copy-of select="@*[not(name()='def')]"/>
                      <xsl:attribute name="def"><xsl:value-of select="position()"/></xsl:attribute>
                      <xsl:if test="*[@tstamp][1]/@tstamp != 0">
                        <!-- If the 1st event in the layer doesn't have a timestamp of 0, insert space. -->
                        <space>
                          <xsl:attribute name="id"><xsl:value-of select="generate-id(*[@tstamp][1]/@tstamp)"/></xsl:attribute>
                          <xsl:attribute name="tstamp">0</xsl:attribute>
                          <xsl:attribute name="dur.ges"><xsl:value-of select="*[@tstamp][1]/@tstamp"/></xsl:attribute>
                        </space>
                      </xsl:if>
                      <xsl:for-each select="*">
                        <xsl:choose>
                          <xsl:when test="starts-with(@beam,'i')">
                            <xsl:text>&startbeam;</xsl:text>
                            <xsl:apply-templates select="." mode="dropattrs"/>
                          </xsl:when>
                          <xsl:when test="starts-with(@beam,'t')">
                            <xsl:apply-templates select="." mode="dropattrs"/>
                            <xsl:text>&endbeam;</xsl:text>
                          </xsl:when>
                          <xsl:otherwise>
                            <xsl:apply-templates select="." mode="dropattrs"/>
                          </xsl:otherwise>
                        </xsl:choose>
                      </xsl:for-each>
                    </layer>
                  </xsl:for-each>
                </staff>
              </xsl:for-each>
            </xsl:for-each-group>
          </xsl:variable>

          <!-- Emit the final results -->
          <xsl:copy-of select="$stafforg3"/>

        </xsl:for-each-group>

        <!-- Copy controlevents -->
        <xsl:copy-of select="annot|arpeg|beam2|bend|curve|dir|dynam|gliss|hairpin|harm|
                             line|lyrics|midi|mordent|octave|pedal|phrase|reh|slur|tempo|
                             tie|trill|tuplet2|turn" />
      </measure>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="*" mode="dropattrs">
    <!-- Drop unnecesssary attributes -->
    <xsl:variable name="thiselement"><xsl:value-of select="name()"/></xsl:variable>
    <xsl:choose>
      <xsl:when test="name()='space'">
        <xsl:element name="{$thiselement}">
          <xsl:copy-of select="@*[name()!='part' and name()!='staff' and name()!='layer']"/>
          <xsl:if test="@staff != ancestor::staff[1]/@def">
            <xsl:copy-of select="@staff"/>
          </xsl:if>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="{$thiselement}">
          <xsl:copy-of select="@*[name()!='part' and name()!='staff' and name()!='layer']"/>
          <xsl:copy-of select="child::*"/>
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*" mode="insertspace">
    <!-- Insert a time-filling space -->
    <xsl:if test="not(@grace)">
      <space>
        <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
        <xsl:copy-of select="@tstamp|@dur|@dur.ges|@part|@layer|@staff" />
        <xsl:choose>
          <xsl:when test="@beam">
            <xsl:copy-of select="@beam"/>
          </xsl:when>
          <xsl:when test="*[@beam]">
            <xsl:copy-of select="*[@beam][1]/@beam" />
          </xsl:when>
        </xsl:choose>
      </space>
    </xsl:if>
  </xsl:template>

  <xsl:template name="getsyldur">
    <!-- Find the terminating note of a slur -->
    <xsl:param name="syldur"></xsl:param>
    <xsl:variable name="thisdur"><xsl:value-of select="duration"/></xsl:variable>
    <xsl:choose>
      <xsl:when test="following-sibling::note[1][not(lyric) and not(grace)]">
        <xsl:variable name="nextdur"><xsl:value-of select="following-sibling::note[1][not(lyric) and not(grace)]/duration"/></xsl:variable>
        <xsl:variable name="totaldur"><xsl:value-of select="$syldur + $nextdur"/></xsl:variable>
        <xsl:for-each select="following-sibling::note[1][not(lyric) and not(grace)]">
          <xsl:call-template name="getsyldur">
            <xsl:with-param name="syldur"><xsl:value-of select="$totaldur"/></xsl:with-param>
          </xsl:call-template>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$syldur"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="pull-off">
    <!-- In MEI a pull off is a directive -->
    <dir>
      <xsl:attribute name="tstamp">
        <xsl:for-each select="ancestor::note[1]">
          <xsl:call-template name="gettstamp"/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:attribute name="place">
        <xsl:choose>
          <xsl:when test="@placement != ''"><xsl:value-of select="@placement"/></xsl:when>
          <xsl:otherwise>above</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
      <xsl:variable name="partstaff">
        <xsl:choose>
          <xsl:when test="ancestor::note[1]/staff">
            <xsl:value-of select="ancestor::note[1]/staff"/>
          </xsl:when>
          <xsl:otherwise>1</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:attribute name="staff">
        <xsl:call-template name="getstaffnum">
          <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
          <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="start">
        <xsl:for-each select="ancestor::note[1]">
          <xsl:value-of select="generate-id()"/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:attribute name="end">
        <xsl:choose>
          <xsl:when test="@number">
            <xsl:variable name="level"><xsl:value-of select="@number"/></xsl:variable>
            <xsl:for-each select="following::pull-off[@type='stop' and @number=$level][1]">
              <xsl:for-each select="ancestor::note[1]">
                <xsl:value-of select="generate-id()"/>
              </xsl:for-each>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select="following::pull-off[@type='stop'][1]">
              <xsl:for-each select="ancestor::note[1]">
                <xsl:attribute name="end"><xsl:value-of select="generate-id()"/></xsl:attribute>
              </xsl:for-each>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:attribute name="dur"><xsl:call-template name="getdurpulloff"/></xsl:attribute>
      <xsl:call-template name="position"/>
      <xsl:value-of select="."/>
    </dir>
  </xsl:template>

  <xsl:template match="direction">
    <!-- Make appropriate MEI elements.  Some MusicXML direction types (brackets, coda, dashes,
         damp, damp-all, eyeglasses, harp-pedals, and scordatura) are not handled yet. -->
    <xsl:for-each select="direction-type">
      <xsl:if test="bracket">
        <!-- MusicXML uses time stamps for brackets, while MEI uses xy coordinates! -->
        <!--
        <xsl:for-each select="bracket[@type='start']">
          <xsl:variable name="number"><xsl:value-of select="@number"/></xsl:variable>
          <line type="bracket">
            <xsl:choose>
              <xsl:when test="@line-type='solid'">
                <xsl:attribute name="rend">narrow</xsl:attribute>
              </xsl:when>
              <xsl:when test="@line-type">
                <xsl:attribute name="rend"><xsl:value-of select="@line-type"/></xsl:attribute>
              </xsl:when>
            </xsl:choose>
            <xsl:attribute name="x1"/>
            <xsl:attribute name="y1"/>
            <xsl:for-each select="following::bracket[@type='stop' and @number=$number][1]">
              <xsl:attribute name="x2"/>
              <xsl:attribute name="y2"/>
            </xsl:for-each>
          </line>
          <xsl:if test="@line-end">
            <line type="start-hook">
              <xsl:attribute name="x1"/>
              <xsl:attribute name="x2"/>
              <xsl:attribute name="y1"/>
              <xsl:attribute name="y2"/>
            </line>
          </xsl:if>
          <xsl:for-each select="following::bracket[@type='stop' and @number=$number][1]">
            <xsl:if test="@line-end">
              <line type="end-hook">
                <xsl:attribute name="x1"/>
                <xsl:attribute name="x2"/>
                <xsl:attribute name="y1"/>
                <xsl:attribute name="y2"/>
              </line>
            </xsl:if>
          </xsl:for-each>
        </xsl:for-each>
        -->
      </xsl:if>
      <xsl:if test="coda">
      </xsl:if>
      <xsl:if test="dashes">
        <!-- Same as brackets -->
      </xsl:if>
      <xsl:if test="damp">
      </xsl:if>
      <xsl:if test="damp-all">
      </xsl:if>
      <xsl:if test="dynamics">
        <dynam>
          <xsl:attribute name="tstamp">
            <xsl:for-each select="ancestor::direction[1]">
              <xsl:call-template name="gettstamp"/>
            </xsl:for-each>
          </xsl:attribute>
          <xsl:attribute name="place">
            <xsl:choose>
              <xsl:when test="ancestor::direction/@placement != ''"><xsl:value-of select="ancestor::direction/@placement"/></xsl:when>
              <xsl:otherwise>below</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
          <xsl:variable name="partstaff">
            <xsl:choose>
              <xsl:when test="staff">
                <xsl:value-of select="staff"/>
              </xsl:when>
              <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:attribute name="staff">
            <xsl:call-template name="getstaffnum">
              <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
              <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <xsl:for-each select="dynamics">
            <xsl:call-template name="position"/>
            <xsl:choose>
              <xsl:when test="@font-family|@font-style|@font-size|@font-weight">
                <rend>
                  <xsl:if test="../@font-family">
                    <xsl:attribute name="fontfam"><xsl:value-of select="../@font-family"/></xsl:attribute>
                  </xsl:if>
                  <xsl:if test="../@font-style">
                    <xsl:attribute name="fontstyle"><xsl:value-of select="../@font-style"/></xsl:attribute>
                  </xsl:if>
                  <xsl:if test="../@font-size">
                    <xsl:attribute name="fontsize"><xsl:value-of select="../@font-size"/></xsl:attribute>
                  </xsl:if>
                  <xsl:if test="../@font-weight and ../@font-weight != 'normal'">
                    <xsl:attribute name="fontweight"><xsl:value-of select="../@font-weight"/></xsl:attribute>
                  </xsl:if>
                  <xsl:for-each select="*">
                    <xsl:choose>
                      <xsl:when test="name()='other-dynamics'">
                        <xsl:value-of select="."/>
                        <xsl:if test="position() != last()">
                          <xsl:text> </xsl:text>
                        </xsl:if>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="name()"/>
                        <xsl:if test="position() != last()">
                          <xsl:text> </xsl:text>
                        </xsl:if>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:for-each>
                </rend>
              </xsl:when>
              <xsl:otherwise>
                <xsl:for-each select="*">
                  <xsl:choose>
                    <xsl:when test="name()='other-dynamics'">
                      <xsl:value-of select="."/>
                      <xsl:if test="position() != last()">
                        <xsl:text> </xsl:text>
                      </xsl:if>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="name()"/>
                      <xsl:if test="position() != last()">
                        <xsl:text> </xsl:text>
                      </xsl:if>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:for-each>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each>
        </dynam>
      </xsl:if>
      <xsl:if test="eyeglasses">
      </xsl:if>
      <xsl:if test="harp-pedals">
      </xsl:if>
      <xsl:if test="metronome">
        <tempo>
          <xsl:attribute name="tstamp">
            <xsl:for-each select="ancestor::direction[1]">
              <xsl:call-template name="gettstamp"/>
            </xsl:for-each>
          </xsl:attribute>
          <xsl:attribute name="place">
            <xsl:choose>
              <xsl:when test="ancestor::direction/@placement != ''"><xsl:value-of select="ancestor::direction/@placement"/></xsl:when>
              <xsl:otherwise>above</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
          <xsl:variable name="partstaff">
            <xsl:choose>
              <xsl:when test="ancestor::note[1]/staff">
                <xsl:value-of select="ancestor::note[1]/staff"/>
              </xsl:when>
              <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:attribute name="staff">
            <xsl:call-template name="getstaffnum">
              <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
              <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <xsl:for-each select="metronome">
            <xsl:call-template name="position"/>
          </xsl:for-each>
          <xsl:choose>
            <xsl:when test="metronome/@font-family or
                            metronome/@font-size or
                            metronome/@font-style='italic' or
                            metronome/@font-weight">
              <rend>
                <xsl:if test="metronome/@font-family">
                  <xsl:attribute name="fontfam">
                    <xsl:value-of select="metronome/@font-family"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:if test="metronome/@font-size">
                  <xsl:attribute name="fontsize">
                    <xsl:value-of select="metronome/@font-size"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:if test="metronome/@font-style='italic'">
                  <xsl:attribute name="fontstyle">ital</xsl:attribute>
                </xsl:if>
                <xsl:if test="metronome/@font-weight='bold'">
                  <xsl:attribute name="fontweight">bold</xsl:attribute>
                </xsl:if>
                <xsl:if test="metronome/@parentheses='yes'">
                  <xsl:text>(</xsl:text>
                </xsl:if>
                <xsl:for-each select="metronome/beat-unit">
                  <xsl:call-template name="beatunitdur"/>
                </xsl:for-each>
                <xsl:if test="metronome/beat-unit-dot">
                  <xsl:text>.</xsl:text>
                </xsl:if>
                <xsl:text>=</xsl:text>
                <xsl:value-of select="metronome/per-minute"/>
                <xsl:if test="metronome/@parentheses='yes'">
                  <xsl:text>)</xsl:text>
                </xsl:if>
              </rend>
            </xsl:when>
            <xsl:otherwise>
                <xsl:if test="metronome/@parentheses='yes'">
                  <xsl:text>(</xsl:text>
                </xsl:if>
                <xsl:for-each select="metronome/beat-unit">
                  <xsl:call-template name="beatunitdur"/>
                </xsl:for-each>
                <xsl:if test="metronome/beat-unit-dot">
                  <xsl:text>.</xsl:text>
                </xsl:if>
                <xsl:text>=</xsl:text>
                <xsl:value-of select="metronome/per-minute"/>
                <xsl:if test="metronome/@parentheses='yes'">
                  <xsl:text>)</xsl:text>
                </xsl:if>
            </xsl:otherwise>
          </xsl:choose>
        </tempo>
      </xsl:if>
      <xsl:if test="octave-shift[not(@type='stop')]">
        <!-- Octave-shift is 'backwards', i.e. indicates written instead of sounding octave.
             Encoded pitches are subject to whether or not an octave-shift is in force. -->
        <octave>
          <xsl:attribute name="tstamp">
            <xsl:for-each select="ancestor::direction[1]">
              <xsl:call-template name="gettstamp"/>
            </xsl:for-each>
          </xsl:attribute>
          <xsl:attribute name="dis"><xsl:value-of select="octave-shift/@size"/></xsl:attribute>
          <xsl:attribute name="place">
            <xsl:choose>
              <xsl:when test="octave-shift/@type='up'">below</xsl:when>
              <xsl:when test="octave-shift/@type='down'">above</xsl:when>
            </xsl:choose>
          </xsl:attribute>
          <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
          <xsl:variable name="partstaff">
            <xsl:choose>
              <xsl:when test="staff">
                <xsl:value-of select="staff"/>
              </xsl:when>
              <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:attribute name="staff">
            <xsl:call-template name="getstaffnum">
              <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
              <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <xsl:if test="offset">
            <xsl:attribute name="ho"><xsl:value-of select="offset"/></xsl:attribute>
          </xsl:if>
          <xsl:for-each select="octave-shift">
            <xsl:call-template name="position"/>
          </xsl:for-each>
          <xsl:attribute name="dur">
            <xsl:call-template name="getduroctave"/>
          </xsl:attribute>
        </octave>
      </xsl:if>
      <xsl:if test="pedal">
        <pedal>
          <xsl:attribute name="tstamp">
            <xsl:for-each select="ancestor::direction[1]">
              <xsl:call-template name="gettstamp"/>
            </xsl:for-each>
          </xsl:attribute>
          <xsl:attribute name="place">below</xsl:attribute>
          <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
          <xsl:variable name="partstaff">
            <xsl:choose>
              <xsl:when test="staff">
                <xsl:value-of select="staff"/>
              </xsl:when>
              <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:attribute name="staff">
            <xsl:call-template name="getstaffnum">
              <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
              <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <xsl:for-each select="pedal">
            <xsl:attribute name="dir">
              <xsl:choose>
                <xsl:when test="@type='start'">down</xsl:when>
                <xsl:when test="@type='stop'">up</xsl:when>
                <xsl:when test="@type='change'">half</xsl:when>
              </xsl:choose>
            </xsl:attribute>
            <xsl:attribute name="style">
              <xsl:choose>
                <xsl:when test="@line='yes'">line</xsl:when>
                <xsl:otherwise>pedstar</xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:call-template name="position"/>
          </xsl:for-each>
        </pedal>
      </xsl:if>
      <xsl:if test="rehearsal">
        <reh>
          <xsl:attribute name="tstamp">
            <xsl:for-each select="ancestor::direction[1]">
              <xsl:call-template name="gettstamp"/>
            </xsl:for-each>
          </xsl:attribute>
          <xsl:attribute name="place">
            <xsl:choose>
              <xsl:when test="ancestor::direction/@placement != ''"><xsl:value-of select="ancestor::direction/@placement"/></xsl:when>
              <xsl:otherwise>above</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
          <xsl:variable name="partstaff">
            <xsl:choose>
              <xsl:when test="staff">
                <xsl:value-of select="staff"/>
              </xsl:when>
              <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:attribute name="staff">
            <xsl:call-template name="getstaffnum">
              <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
              <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <xsl:for-each select="rehearsal">
            <xsl:attribute name="enclose">
              <xsl:choose>
                <xsl:when test="@enclosure"><xsl:value-of select="@enclosure"/></xsl:when>
                <xsl:otherwise>box</xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:call-template name="position"/>
            <xsl:call-template name="fontproperties"/>
          </xsl:for-each>
        </reh>
      </xsl:if>
      <xsl:if test="scordatura">
      </xsl:if>
      <xsl:if test="segno">
        <dir>
          <xsl:attribute name="tstamp">
            <xsl:for-each select="ancestor::direction[1]">
              <xsl:call-template name="gettstamp"/>
            </xsl:for-each>
          </xsl:attribute>
          <xsl:attribute name="place">
            <xsl:choose>
              <xsl:when test="ancestor::direction/@placement != ''"><xsl:value-of select="ancestor::direction/@placement"/></xsl:when>
              <xsl:otherwise>above</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
          <xsl:variable name="partstaff">
            <xsl:choose>
              <xsl:when test="staff">
                <xsl:value-of select="staff"/>
              </xsl:when>
              <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:attribute name="staff">
            <xsl:call-template name="getstaffnum">
              <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
              <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <xsl:for-each select="segno">
            <xsl:call-template name="position"/>
          </xsl:for-each>
          <xsl:choose>
            <xsl:when test="segno/@font-family or
                            segno/@font-size or
                            segno/@font-style='italic' or
                            segno/@font-weight">
              <rend>
                <xsl:if test="segno/@font-family">
                  <xsl:attribute name="fontfam">
                    <xsl:value-of select="segno/@font-family"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:if test="segno/@font-size">
                  <xsl:attribute name="fontsize">
                    <xsl:value-of select="segno/@font-size"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:if test="segno/@font-style='italic'">
                  <xsl:attribute name="fontstyle">ital</xsl:attribute>
                </xsl:if>
                <xsl:if test="segno/@font-weight='bold'">
                  <xsl:attribute name="fontweight">bold</xsl:attribute>
                </xsl:if>
                <xsl:text disable-output-escaping="yes">&amp;segno;</xsl:text>
              </rend>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text disable-output-escaping="yes">&amp;segno;</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </dir>
      </xsl:if>
      <xsl:if test="wedge[not(@type='stop')]">
        <hairpin>
          <xsl:attribute name="tstamp">
            <xsl:for-each select="ancestor::direction[1]">
              <xsl:call-template name="gettstamp"/>
            </xsl:for-each>
          </xsl:attribute>
          <xsl:choose>
            <xsl:when test="wedge/@type='crescendo'">
              <xsl:attribute name="form">cres</xsl:attribute>
            </xsl:when>
            <xsl:when test="wedge/@type='diminuendo'">
              <xsl:attribute name="form">dim</xsl:attribute>
            </xsl:when>
          </xsl:choose>
          <xsl:attribute name="place">
            <xsl:choose>
              <xsl:when test="ancestor::direction/@placement != ''"><xsl:value-of select="ancestor::direction/@placement"/></xsl:when>
              <xsl:otherwise>below</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
          <xsl:variable name="partstaff">
            <xsl:choose>
              <xsl:when test="staff">
                <xsl:value-of select="staff"/>
              </xsl:when>
              <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:attribute name="staff">
            <xsl:call-template name="getstaffnum">
              <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
              <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <xsl:if test="wedge/@default-y">
            <xsl:attribute name="y1"><xsl:value-of select="wedge/@default-y"/></xsl:attribute>
          </xsl:if>
          <xsl:if test="wedge/@spread">
            <xsl:attribute name="width"><xsl:value-of select="wedge/@spread"/></xsl:attribute>
          </xsl:if>
          <xsl:if test="offset">
            <xsl:attribute name="holeft"><xsl:value-of select="offset"/></xsl:attribute>
          </xsl:if>
          <xsl:choose>
            <xsl:when test="wedge/@number">
              <xsl:variable name="wedgenumber"><xsl:value-of select="wedge/@number"/></xsl:variable>
              <xsl:if test="following::direction[direction-type/wedge[@type='stop' and @number=$wedgenumber]][1]/@default-y">
                <xsl:attribute name="yright"><xsl:value-of select="following::direction[direction-type/wedge[@type='stop' and @number=$wedgenumber]][1]/@default-y"/></xsl:attribute>
              </xsl:if>
              <xsl:if test="following::direction[direction-type/wedge[@type='stop' and @number=$wedgenumber]][1]/@relative-y">
                <xsl:attribute name="yright"><xsl:value-of select="following::direction[direction-type/wedge[@type='stop' and @number=$wedgenumber]][1]/@relative-y"/></xsl:attribute>
              </xsl:if>
              <xsl:if test="following::direction[direction-type/wedge[@type='stop' and @number=$wedgenumber]][1]/offset">
                <xsl:attribute name="horight">
                  <xsl:value-of select="following::direction[direction-type/wedge[@type='stop' and @number=$wedgenumber]][1]/offset"/>
                </xsl:attribute>
              </xsl:if>
              <xsl:if test="following::direction[direction-type/wedge[@type='stop' and @number=$wedgenumber and @spread &gt; 0]]">
                <xsl:attribute name="width"><xsl:value-of select="following::direction[direction-type/wedge[@type='stop' and @number=$wedgenumber]][1]/direction-type/wedge[@type='stop' and @number=$wedgenumber]/@spread"/></xsl:attribute>
              </xsl:if>
            </xsl:when>
            <xsl:otherwise>
              <xsl:if test="following::direction[direction-type/wedge[@type='stop']][1]/@default-y">
                <xsl:attribute name="y2"><xsl:value-of select="following::direction[direction-type/wedge[@type='stop']][1]/@default-y"/></xsl:attribute>
              </xsl:if>
              <xsl:if test="following::direction[direction-type/wedge[@type='stop']][1]/@relative-y">
                <xsl:attribute name="y2"><xsl:value-of select="following::direction[direction-type/wedge[@type='stop']][1]/@relative-y"/></xsl:attribute>
              </xsl:if>
              <xsl:if test="following::direction[direction-type/wedge[@type='stop']][1]/offset">
                <xsl:attribute name="horight">
                  <xsl:value-of select="following::direction[direction-type/wedge[@type='stop']][1]/offset"/>
                </xsl:attribute>
              </xsl:if>
              <xsl:if test="following::direction[direction-type/wedge[@type='stop' and @spread &gt; 0]]">
                <xsl:attribute name="width"><xsl:value-of select="following::direction[direction-type/wedge[@type='stop']][1]/direction-type/wedge[@type='stop']/@spread"/></xsl:attribute>
              </xsl:if>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:attribute name="dur">
            <xsl:call-template name="getdurhairpin"/>
          </xsl:attribute>
        </hairpin>
      </xsl:if>
      <xsl:if test="words">
        <dir>
          <xsl:attribute name="tstamp">
            <xsl:for-each select="ancestor::direction[1]">
              <xsl:call-template name="gettstamp"/>
            </xsl:for-each>
          </xsl:attribute>
          <xsl:attribute name="place">
            <xsl:choose>
              <xsl:when test="ancestor::direction/@placement != ''"><xsl:value-of select="ancestor::direction/@placement"/></xsl:when>
              <xsl:otherwise>above</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
          <xsl:variable name="partstaff">
            <xsl:choose>
              <xsl:when test="ancestor::note[1]/staff">
                <xsl:value-of select="ancestor::note[1]/staff"/>
              </xsl:when>
              <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:attribute name="staff">
            <xsl:call-template name="getstaffnum">
              <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
              <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <!-- Convert ho and vo attributes to inter-line distances later -->
          <xsl:for-each select="words">
            <xsl:call-template name="position"/>
            <xsl:for-each select="../../following-sibling::*[1]/dashes[@type='start']">
              <xsl:attribute name="dur">
                <xsl:call-template name="getdurwords"/>
              </xsl:attribute>
            </xsl:for-each>
          </xsl:for-each>
          <xsl:choose>
            <xsl:when test="words/@xml:lang">
              <xsl:attribute name="lang"><xsl:value-of select="words/@xml:lang"/></xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
              <xsl:attribute name="lang">it</xsl:attribute>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:choose>
            <xsl:when test="words/@font-family or 
                            words/@font-size or 
                            words/@font-style='italic' or
                            words/@font-weight">
              <rend>
                <xsl:if test="words/@font-family">
                  <xsl:attribute name="fontfam">
                    <xsl:value-of select="words/@font-family"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:if test="words/@font-size">
                  <xsl:attribute name="fontsize">
                    <xsl:value-of select="words/@font-size"/>
                  </xsl:attribute>
                </xsl:if>
                <xsl:if test="words/@font-style='italic'">
                  <xsl:attribute name="fontstyle">ital</xsl:attribute>
                </xsl:if>
                <xsl:if test="words/@font-weight='bold'">
                  <xsl:attribute name="fontweight">bold</xsl:attribute>
                </xsl:if>
                <xsl:value-of select="words"/>
              </rend>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="words"/>
            </xsl:otherwise>
          </xsl:choose>
        </dir>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="harmony">
    <!-- Handle harmony indications, such as guitar chord grids -->
    <harm>
      <xsl:attribute name="tstamp"><xsl:call-template name="gettstamp"/></xsl:attribute>
      <xsl:attribute name="place">above</xsl:attribute>
      <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
      <xsl:variable name="partstaff">1</xsl:variable>
      <xsl:attribute name="staff">
        <xsl:call-template name="getstaffnum">
          <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
          <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:call-template name="position"/>
      <xsl:choose>
        <xsl:when test="@font-family|@font-style|@font-size|@font-weight">
          <rend>
            <xsl:if test="@font-family">
              <xsl:attribute name="fontfam"><xsl:value-of select="@font-family"/></xsl:attribute>
            </xsl:if>
            <xsl:if test="@font-style">
              <xsl:attribute name="fontstyle"><xsl:value-of select="@font-style"/></xsl:attribute>
            </xsl:if>
            <xsl:if test="@font-size">
              <xsl:attribute name="fontsize"><xsl:value-of select="@font-size"/></xsl:attribute>
            </xsl:if>
            <xsl:if test="@font-weight and @font-weight != 'normal'">
              <xsl:attribute name="fontweight"><xsl:value-of select="@font-weight"/></xsl:attribute>
            </xsl:if>
            <xsl:value-of select="root/root-step"/>
            <xsl:choose>
              <xsl:when test="root/root-alter = 2">
                <xsl:text disable-output-escaping="yes">&amp;dblsharp;</xsl:text>
              </xsl:when>
              <xsl:when test="root/root-alter = 1">
                <xsl:text disable-output-escaping="yes">&amp;sharp;</xsl:text>
              </xsl:when>
              <xsl:when test="root/root-alter = -1">
                <xsl:text disable-output-escaping="yes">&amp;flat</xsl:text>
              </xsl:when>
              <xsl:when test="root/root-alter = -2">
                <xsl:text disable-output-escaping="yes">&amp;dblflat;</xsl:text>
              </xsl:when>
            </xsl:choose>
            <xsl:value-of select="kind/@text"/>
            <xsl:if test="bass">
              <xsl:text>/</xsl:text><xsl:value-of select="bass/bass-step"/>
              <xsl:choose>
                <xsl:when test="bass/bass-alter = 2">
                  <xsl:text disable-output-escaping="yes">&amp;dblsharp;</xsl:text>
                </xsl:when>
                <xsl:when test="bass/bass-alter = 1">
                  <xsl:text disable-output-escaping="yes">&amp;sharp;</xsl:text>
                </xsl:when>
                <xsl:when test="bass/bass-alter = -1">
                  <xsl:text disable-output-escaping="yes">&amp;flat</xsl:text>
                </xsl:when>
                <xsl:when test="bass/bass-alter = -2">
                  <xsl:text disable-output-escaping="yes">&amp;dblflat;</xsl:text>
                </xsl:when>
              </xsl:choose>
            </xsl:if>
          </rend>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="root/root-step"/>
          <xsl:choose>
            <xsl:when test="root/root-alter = 2">
              <xsl:text disable-output-escaping="yes">&amp;dblsharp;</xsl:text>
            </xsl:when>
            <xsl:when test="root/root-alter = 1">
              <xsl:text disable-output-escaping="yes">&amp;sharp;</xsl:text>
            </xsl:when>
            <xsl:when test="root/root-alter = -1">
              <xsl:text disable-output-escaping="yes">&amp;flat</xsl:text>
            </xsl:when>
            <xsl:when test="root/root-alter = -2">
              <xsl:text disable-output-escaping="yes">&amp;dblflat;</xsl:text>
            </xsl:when>
          </xsl:choose>
          <xsl:value-of select="kind/@text"/>
          <xsl:if test="bass">
            <xsl:text>/</xsl:text><xsl:value-of select="bass/bass-step"/>
            <xsl:choose>
              <xsl:when test="bass/bass-alter = 2">
                <xsl:text disable-output-escaping="yes">&amp;dblsharp;</xsl:text>
              </xsl:when>
              <xsl:when test="bass/bass-alter = 1">
                <xsl:text disable-output-escaping="yes">&amp;sharp;</xsl:text>
              </xsl:when>
              <xsl:when test="bass/bass-alter = -1">
                <xsl:text disable-output-escaping="yes">&amp;flat</xsl:text>
              </xsl:when>
              <xsl:when test="bass/bass-alter = -2">
                <xsl:text disable-output-escaping="yes">&amp;dblflat;</xsl:text>
              </xsl:when>
            </xsl:choose>
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
    </harm>
  </xsl:template>

  <xsl:template match="caesura">
    <!-- In MEI a caesura is a directive -->
    <dir>
      <xsl:attribute name="tstamp">
        <xsl:for-each select="ancestor::note[1]">
          <xsl:call-template name="gettstamp"/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:attribute name="place">
        <xsl:choose>
          <xsl:when test="@placement != ''"><xsl:value-of select="@placement"/></xsl:when>
          <xsl:otherwise>above</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
      <xsl:variable name="partstaff">
        <xsl:choose>
          <xsl:when test="ancestor::note[1]/staff">
            <xsl:value-of select="ancestor::note[1]/staff"/>
          </xsl:when>
          <xsl:otherwise>1</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:attribute name="staff">
        <xsl:call-template name="getstaffnum">
          <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
          <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="start">
        <xsl:for-each select="ancestor::note[1]">
          <xsl:value-of select="generate-id()"/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:call-template name="position"/>
      <xsl:text>//</xsl:text>
    </dir>
  </xsl:template>

  <xsl:template match="note/notations/slur[@type='start']">
    <!-- What MusicXML calls 'slurs' are actually phrase markings in most cases.
         True 'slurs' can also be represented using phrase elements.  So, I use
         phrase elements instead of slur attributes. -->
    <phrase>
      <xsl:attribute name="tstamp">
        <xsl:for-each select="ancestor::note[1]">
          <xsl:call-template name="gettstamp"/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:attribute name="place">
        <xsl:choose>
          <xsl:when test="@placement != ''"><xsl:value-of select="@placement"/></xsl:when>
          <xsl:otherwise>above</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:attribute name="start">
        <xsl:for-each select="ancestor::note[1]">
          <xsl:value-of select="generate-id()"/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:if test="@default-x">
        <xsl:attribute name="startho"><xsl:value-of select="@default-x"/></xsl:attribute>
      </xsl:if>
      <xsl:if test="@default-y">
        <xsl:attribute name="startvo"><xsl:value-of select="@default-y"/></xsl:attribute>
      </xsl:if>
      <xsl:call-template name="getphraseend"/>
    </phrase>
  </xsl:template>

  <xsl:template match="note/notations/slur[@type='continue']">
    <xsl:variable name="slurnumber"><xsl:value-of select="@number"/></xsl:variable>
    <xsl:choose>
      <xsl:when test="count(preceding-sibling::slur[@number=$slurnumber])=0">
        <!-- This is a continue/stop pair -->
        <phrase>
          <xsl:attribute name="tstamp">
            <xsl:for-each select="ancestor::note[1]">
              <xsl:call-template name="gettstamp"/>
            </xsl:for-each>
          </xsl:attribute>
          <xsl:attribute name="place">
            <xsl:choose>
              <xsl:when test="@placement != ''"><xsl:value-of select="@placement"/></xsl:when>
              <xsl:otherwise>
                <!-- The place attribute isn't present; presumably, this phrase mark has the same
                     orientation as preceding::slur[@type='start' and @number={the same number
                     as this (the continuation) slur}][1]. Right now we'll just make it 'above'. -->
                <xsl:text>above</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
          <xsl:variable name="partstaff">
            <xsl:choose>
              <xsl:when test="ancestor::note[1]/staff">
                <xsl:value-of select="ancestor::note[1]/staff"/>
              </xsl:when>
              <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:attribute name="staff">
            <xsl:call-template name="getstaffnum">
              <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
              <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
            </xsl:call-template>
          </xsl:attribute>
          <xsl:attribute name="start">
            <xsl:for-each select="ancestor::note[1]">
              <xsl:value-of select="generate-id()"/>
            </xsl:for-each>
          </xsl:attribute>
          <!-- In the current examples 'continue-stop' type phrases have no
               duration, i.e., they start and end on the same note. -->
          <xsl:attribute name="dur">0</xsl:attribute>
          <xsl:if test="@default-x">
            <xsl:attribute name="startho"><xsl:value-of select="@default-x"/></xsl:attribute>
          </xsl:if>
          <xsl:if test="@default-y">
            <xsl:attribute name="startvo"><xsl:value-of select="@default-y"/></xsl:attribute>
          </xsl:if>
          <xsl:variable name="bezierstart">
            <xsl:value-of select="@bezier-x"/><xsl:text>,</xsl:text><xsl:value-of select="@bezier-y"/>
          </xsl:variable>
          <xsl:for-each select="following::slur[not(@type='start') and @number=$slurnumber][1]">
            <xsl:for-each select="ancestor::note[1]">
              <xsl:attribute name="end"><xsl:value-of select="generate-id()"/></xsl:attribute>
            </xsl:for-each>
            <xsl:if test="@default-x">
              <xsl:attribute name="endho"><xsl:value-of select="@default-x"/></xsl:attribute>
            </xsl:if>
            <xsl:if test="@default-y">
              <xsl:attribute name="endvo"><xsl:value-of select="@default-y"/></xsl:attribute>
            </xsl:if>
            <xsl:variable name="bezierend">
              <xsl:value-of select="@bezier-x"/><xsl:text>,</xsl:text><xsl:value-of select="@bezier-y"/>
            </xsl:variable>
            <xsl:if test="concat($bezierstart,$bezierend) != ',,'">
              <xsl:attribute name="bezier">
                <xsl:value-of select="$bezierstart"/>,<xsl:value-of select="$bezierend"/>
              </xsl:attribute>
            </xsl:if>
          </xsl:for-each>
        </phrase>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="note/notations/tuplet[@type='start']">
    <!-- Create tuplet2 control elements -->
    <tuplet2>
      <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
      <xsl:variable name="partstaff">
        <xsl:choose>
          <xsl:when test="ancestor::note[1]/staff">
            <xsl:value-of select="ancestor::note[1]/staff"/>
          </xsl:when>
          <xsl:otherwise>1</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:attribute name="staff">
        <xsl:call-template name="getstaffnum">
          <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
          <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:attribute name="start">
        <xsl:for-each select="ancestor::note[1]">
          <xsl:value-of select="generate-id()"/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:attribute name="end">
        <xsl:choose>
          <xsl:when test="@number">
            <xsl:variable name="tupletlevel"><xsl:value-of select="@number"/></xsl:variable>
            <xsl:for-each select="following::tuplet[@type='stop' and @number=$tupletlevel][1]">
              <xsl:for-each select="ancestor::note[1]">
                <xsl:value-of select="generate-id()"/>
              </xsl:for-each>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select="following::tuplet[@type='stop'][1]">
              <xsl:for-each select="ancestor::note[1]">
                <xsl:value-of select="generate-id()"/>
              </xsl:for-each>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:for-each select="ancestor::note[1]/time-modification">
        <xsl:attribute name="num"><xsl:value-of select="actual-notes"/>:<xsl:value-of select="normal-notes"/></xsl:attribute>
        <xsl:if test="normal-type">
          <xsl:choose>
            <xsl:when test="normalize-space(normal-type) = 'long'">
              <xsl:attribute name="numbase">long</xsl:attribute>
            </xsl:when>
            <xsl:when test="normalize-space(normal-type) = 'breve'">
              <xsl:attribute name="numbase">breve</xsl:attribute>
            </xsl:when>
            <xsl:when test="normalize-space(normal-type) = 'whole'">
              <xsl:attribute name="numbase">1</xsl:attribute>
            </xsl:when>
            <xsl:when test="normalize-space(normal-type) = 'half'">
              <xsl:attribute name="numbase">2</xsl:attribute>
            </xsl:when>
            <xsl:when test="normalize-space(normal-type) = 'quarter'">
              <xsl:attribute name="numbase">4</xsl:attribute>
            </xsl:when>
            <xsl:when test="normalize-space(normal-type) = 'eighth'">
              <xsl:attribute name="numbase">8</xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
              <xsl:attribute name="numbase">
                <xsl:analyze-string select="normalize-space(normal-type)" regex="^([0-9]+)(.*)$">
                  <xsl:matching-substring>
                    <xsl:value-of select="regex-group(1)"/>
                  </xsl:matching-substring>
                </xsl:analyze-string>
              </xsl:attribute>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:for-each>
      <xsl:choose>
        <xsl:when test="@show-number='none'">
          <xsl:attribute name="num.visible">no</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="num.visible">yes</xsl:attribute>
          <xsl:attribute name="num.place">
            <xsl:choose>
              <xsl:when test="@placement != ''"><xsl:value-of select="@placement"/></xsl:when>
              <xsl:otherwise>above</xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:choose>
            <xsl:when test="@show-number='both'">
              <xsl:attribute name="num.format">ratio</xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
              <xsl:attribute name="num.format">count</xsl:attribute>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:choose>
        <xsl:when test="@bracket='yes'">
          <xsl:attribute name="bracket.visible">yes</xsl:attribute>
          <xsl:attribute name="bracket.place"><xsl:value-of select="@placement"/></xsl:attribute>
        </xsl:when>
        <xsl:when test="@bracket='no'">
          <xsl:attribute name="bracket.visible">no</xsl:attribute>
        </xsl:when>
      </xsl:choose>
    </tuplet2>
  </xsl:template>

  <xsl:template match="note/notations/dynamics">
    <!-- Dynamics -->
    <dynam>
      <xsl:attribute name="tstamp">
        <xsl:for-each select="ancestor::note[1]">
          <xsl:call-template name="gettstamp"/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:attribute name="place">
        <xsl:choose>
          <xsl:when test="@placement != ''"><xsl:value-of select="@placement"/></xsl:when>
          <xsl:otherwise>below</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
      <xsl:variable name="partstaff">
        <xsl:choose>
          <xsl:when test="ancestor::note[1]/staff">
            <xsl:value-of select="ancestor::note[1]/staff"/>
          </xsl:when>
          <xsl:otherwise>1</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:attribute name="staff">
        <xsl:call-template name="getstaffnum">
          <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
          <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
        </xsl:call-template>
      </xsl:attribute>

      <xsl:call-template name="position"/>

      <xsl:choose>
        <xsl:when test="@font-family|@font-style|@font-size|@font-weight">
          <rend>
            <xsl:if test="@font-family">
              <xsl:attribute name="fontfam"><xsl:value-of select="@font-family"/></xsl:attribute>
            </xsl:if>
            <xsl:if test="@font-style">
              <xsl:attribute name="fontstyle"><xsl:value-of select="@font-style"/></xsl:attribute>
            </xsl:if>
            <xsl:if test="@font-size">
              <xsl:attribute name="fontsize"><xsl:value-of select="@font-size"/></xsl:attribute>
            </xsl:if>
            <xsl:if test="@font-weight and @font-weight != 'normal'">
              <xsl:attribute name="fontweight"><xsl:value-of select="@font-weight"/></xsl:attribute>
            </xsl:if>
            <xsl:for-each select="*">
              <xsl:choose>
                <xsl:when test="name()='other-dynamics'">
                  <xsl:value-of select="."/>
                  <xsl:if test="position() != last()">
                    <xsl:text> </xsl:text>
                  </xsl:if>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="name()"/>
                  <xsl:if test="position() != last()">
                    <xsl:text> </xsl:text>
                  </xsl:if>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:for-each>
          </rend>
        </xsl:when>
        <xsl:otherwise>
          <xsl:for-each select="*">
            <xsl:choose>
              <xsl:when test="name()='other-dynamics'">
                <xsl:value-of select="."/>
                <xsl:if test="position() != last()">
                  <xsl:text> </xsl:text>
                </xsl:if>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="name()"/>
                <xsl:if test="position() != last()">
                  <xsl:text> </xsl:text>
                </xsl:if>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each>
        </xsl:otherwise>
      </xsl:choose>
    </dynam>
  </xsl:template>

  <!-- Save this until later!
  <xsl:template match="sound">
  </xsl:template>
  -->

  <xsl:template match="attributes[preceding-sibling::note]|attributes[preceding-sibling::forward]">
    <!-- Process MusicXML 'attributes' elements which don't occur right at the start of a measure -->
    <xsl:if test="clef">
      <!-- Mid-measure clef change -->
      <clefchange>
        <xsl:variable name="partID"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:variable>
        <xsl:variable name="partstaff">
          <xsl:choose>
            <xsl:when test="clef/@number"><xsl:value-of select="clef/@number"/></xsl:when>
            <xsl:otherwise>1</xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:attribute name="part"><xsl:value-of select="ancestor::part[1]/@id"/></xsl:attribute>
        <xsl:attribute name="layer">
          <xsl:choose>
            <xsl:when test="following::note[1]/voice"><xsl:value-of select="following::note[1]/voice"/></xsl:when>
            <xsl:otherwise>1</xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
        <xsl:attribute name="staff">
          <xsl:call-template name="getstaffnum">
            <xsl:with-param name="partID"><xsl:value-of select="$partID"/></xsl:with-param>
            <xsl:with-param name="partstaff"><xsl:value-of select="$partstaff"/></xsl:with-param>
          </xsl:call-template>
        </xsl:attribute>
        <xsl:attribute name="tstamp">
          <xsl:call-template name="gettstamp"/>
        </xsl:attribute>
        <xsl:attribute name="clef.shape">
          <xsl:choose>
            <xsl:when test="clef/sign='percussion'">
              <xsl:text>perc</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="clef/sign"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
        <xsl:attribute name="clef.line"><xsl:value-of select="clef/line"/></xsl:attribute>
        <xsl:if test="clef-octave-change">
          <xsl:attribute name="clef.trans">
            <xsl:choose>
              <xsl:when test="clef-octave-change = '2'">15va</xsl:when>
              <xsl:when test="clef-octave-change = '1'">8va</xsl:when>
              <xsl:when test="clef-octave-change = '-1'">8vb</xsl:when>
              <xsl:when test="clef-octave-change = '-2'">15vb</xsl:when>
            </xsl:choose>
          </xsl:attribute>
        </xsl:if>
      </clefchange>
    </xsl:if>
  </xsl:template>

  <xsl:template match="forward">
    <!-- Forward skips in time have to be filled with space in MEI -->
    <xsl:variable name="fordur"><xsl:value-of select="duration"/></xsl:variable>
    <xsl:choose>
      <xsl:when test="following-sibling::*[1][name()='backup']">
        <xsl:variable name="backdur"><xsl:value-of select="following-sibling::backup[1]"/></xsl:variable>
        <xsl:if test="not($backdur=$fordur)">
          <space>
            <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
            <xsl:attribute name="tstamp"><xsl:call-template name="gettstamp"/></xsl:attribute>
            <xsl:call-template name="gesturaldur"/>
            <xsl:call-template name="part-layer-staff-beam-assign"/>
          </space>
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <space>
          <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
          <xsl:attribute name="tstamp"><xsl:call-template name="gettstamp"/></xsl:attribute>
          <xsl:call-template name="gesturaldur"/>
          <xsl:call-template name="part-layer-staff-beam-assign"/>
        </space>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="backup">
    <!-- This is a no-op!  Backup elements don't require any action in MEI. -->
  </xsl:template>

  <xsl:template match="note[not(chord)]">
    <!-- Non-chord tones -->
    <xsl:choose>
      <xsl:when test="following-sibling::note[1][chord]">
        <xsl:variable name="chordthis">
          <chord>
            <xsl:apply-templates select="." mode="onenote"/>
          </chord>
        </xsl:variable>
        <xsl:apply-templates select="$chordthis/chord" mode="chordthis"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="." mode="onenote"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="chord" mode="chordthis">
    <!-- Copy some note attributes to the parent chord -->
    <xsl:variable name="chordthis2">
      <chord>
        <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
        <!-- Copy note attributes that always rightfully belong to the whole chord -->
        <xsl:copy-of select="note[1]/@dots | note[1]/@dur | note[1]/@dur.ges |
                             note[1]/@stem.dir | note[1]/@stem.len | note[1]/@tstamp |
                             note[1]/@tuplet"/>

        <!-- Copy these attrs if even one of the notes has the attr -->
        <xsl:if test="note[@fermata]">
          <xsl:copy-of select="note[@fermata][1]/@fermata"/>
        </xsl:if>
        <xsl:if test="note[@grace]">
          <xsl:copy-of select="note[@grace][1]/@grace"/>
        </xsl:if>
        <xsl:if test="note[@beam]">
          <xsl:copy-of select="note[@beam][1]/@beam"/>
        </xsl:if>

        <!-- Copy these attrs if all notes have the attr and have the same value -->
        <xsl:if test="count(note[@part])=count(note)">
          <xsl:if test="count(distinct-values(note/@part))=1">
            <xsl:copy-of select="note[@part][1]/@part"/>
          </xsl:if>
        </xsl:if>
        <xsl:if test="count(note[@layer])=count(note)">
          <xsl:if test="count(distinct-values(note/@layer))=1">
            <xsl:copy-of select="note[@staff][1]/@layer"/>
          </xsl:if>
        </xsl:if>
        <xsl:if test="count(note[@staff])=count(note)">
          <xsl:if test="count(distinct-values(note/@staff))=1">
            <xsl:copy-of select="note[@staff][1]/@staff"/>
          </xsl:if>
        </xsl:if>
        <xsl:if test="count(note[@size])=count(note)">
          <xsl:if test="count(distinct-values(note/@size))=1">
            <xsl:copy-of select="note[@size][1]/@size"/>
          </xsl:if>
        </xsl:if>
        <xsl:if test="count(note[@instr])=count(note)">
          <xsl:if test="count(distinct-values(note/@instr))=1">
            <xsl:copy-of select="note[@instr][1]/@instr"/>
          </xsl:if>
        </xsl:if>
        <xsl:if test="count(note[@beam])=count(note)">
          <xsl:if test="count(distinct-values(note/@beam))=1">
            <xsl:copy-of select="note[@beam][1]/@beam"/>
          </xsl:if>
        </xsl:if>
        <xsl:if test="count(note[@tie])=count(note)">
          <xsl:if test="count(distinct-values(note/@tie))=1">
            <xsl:copy-of select="note[@tie][1]/@tie"/>
          </xsl:if>
        </xsl:if>
        <xsl:if test="count(note[@grace.time])=count(note)">
          <xsl:if test="count(distinct-values(note/@grace.time))=1">
            <xsl:copy-of select="note[@grace.time][1]/@grace.time"/>
          </xsl:if>
        </xsl:if>
        <xsl:if test="count(note[@artic])=count(note)">
          <xsl:if test="count(distinct-values(note/@artic))=1">
            <xsl:copy-of select="note[@artic][1]/@artic"/>
          </xsl:if>
        </xsl:if>
        <xsl:if test="count(note[@altsym])=count(note)">
          <xsl:if test="count(distinct-values(note/@altsym))=1">
            <xsl:copy-of select="note[@altsym][1]/@altsym"/>
          </xsl:if>
        </xsl:if>
        <xsl:if test="count(note[@x])=count(note)">
          <xsl:if test="count(distinct-values(note/@x))=1">
            <xsl:copy-of select="note[@x][1]/@x"/>
          </xsl:if>
        </xsl:if>
        <!-- Copy notes -->
        <xsl:copy-of select="note"/>
      </chord>
    </xsl:variable>
    <xsl:apply-templates select="$chordthis2/chord" mode="thinnote"/>
  </xsl:template>

  <xsl:template match="chord" mode="thinnote">
    <!-- Eliminate note attributes copied to parent chord -->
    <chord>
      <xsl:copy-of select="@*"/>
      <xsl:variable name="sortedstaff">
        <xsl:perform-sort select="distinct-values(note/@staff)">
          <xsl:sort select="." data-type="number"/>
        </xsl:perform-sort>
      </xsl:variable>
      <xsl:attribute name="staff"><xsl:value-of select="$sortedstaff"/></xsl:attribute>
      <xsl:variable name="sortedlayer">
        <xsl:perform-sort select="distinct-values(note/@layer)">
          <xsl:sort select="." data-type="number"/>
        </xsl:perform-sort>
      </xsl:variable>
      <xsl:attribute name="layer"><xsl:value-of select="$sortedlayer"/></xsl:attribute>
      <xsl:for-each select="note">
        <xsl:sort select="@staff"/>
        <xsl:sort select="@layer"/>
        <note>
          <xsl:copy-of select="@id"/>
          <xsl:for-each select="@*">
            <xsl:variable name="thisattr"><xsl:value-of select="name(.)"/></xsl:variable>
            <!-- Copy any other note attributes (except @id which was handled above) that
            don't already exist on the parent chord -->
            <xsl:if test="not(ancestor::chord/@*[name()=$thisattr])">
              <xsl:copy-of select="."/>
            </xsl:if>
          </xsl:for-each>
          <!-- Copy any children -->
          <xsl:copy-of select="child::*"/>
        </note>
      </xsl:for-each>
    </chord>
  </xsl:template>

  <xsl:template match="note" mode="onenote">
    <!-- Create a note/rest element -->
    <xsl:choose>
      <xsl:when test="rest">
        <!-- This is a rest! -->
        <xsl:variable name="thisvoice"><xsl:value-of select="voice"/></xsl:variable>
        <xsl:choose>
          <xsl:when test="type">
            <rest>
              <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
              <xsl:attribute name="tstamp"><xsl:call-template name="gettstamp"/></xsl:attribute>
              <xsl:call-template name="notateddur"/>
              <xsl:call-template name="notateddot"/>
              <xsl:call-template name="fermata.attr"/>
              <xsl:call-template name="gesturaldur"/>
              <xsl:call-template name="part-layer-staff-beam-assign"/>
              <xsl:call-template name="position"/>
              <xsl:call-template name="restvo"/>
              <xsl:call-template name="size"/>
            </rest>
          </xsl:when>
          <xsl:when test="count(preceding-sibling::note[voice=$thisvoice]) +
 count(following-sibling::note[voice=$thisvoice]) +
 count(preceding-sibling::forward[voice=$thisvoice]) +
 count(following-sibling::forward[voice=$thisvoice])=0">
            <!-- This is the only rest in this voice -->

            <!-- A test for measure incompleteness is necessary here to properly
                 handle pickup rests without a written duration.  The written duration
                 must be calculated from the gestural duration.  Without this test,
                 pickup rests without written duration are currently transcoded as
                 msrest elements.
            <xsl:when test="../@implicit='yes'">
            </xsl:when>
            -->

            <msrest>
              <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
              <xsl:attribute name="tstamp"><xsl:call-template name="gettstamp"/></xsl:attribute>
              <xsl:call-template name="fermata.attr"/>
              <xsl:call-template name="gesturaldur"/>
              <xsl:call-template name="part-layer-staff-beam-assign"/>
              <xsl:call-template name="position"/>
              <xsl:call-template name="restvo"/>
              <xsl:call-template name="size"/>
            </msrest>
          </xsl:when>
          <xsl:otherwise>
            <!-- The lack of a written duration value will cause a parsing error -->
            <rest>
              <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
              <xsl:attribute name="tstamp"><xsl:call-template name="gettstamp"/></xsl:attribute>
              <xsl:call-template name="notateddur"/>
              <xsl:call-template name="notateddot"/>
              <xsl:call-template name="fermata.attr"/>
              <xsl:call-template name="gesturaldur"/>
              <xsl:call-template name="part-layer-staff-beam-assign"/>
              <xsl:call-template name="position"/>
              <xsl:call-template name="restvo"/>
              <xsl:call-template name="size"/>
            </rest>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <!-- This is a 'pitched' or 'unpitched' note -->
        <!-- None of the Beyond MIDI examples include unpitched notes. -->
        <note>
          <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
          <xsl:attribute name="tstamp"><xsl:call-template name="gettstamp"/></xsl:attribute>
          <xsl:if test="@print-object='no'">
            <xsl:attribute name="visible">no</xsl:attribute>
          </xsl:if>

          <!-- Is this a grace note? -->
          <xsl:if test="grace">
            <xsl:choose>
              <xsl:when test="grace/@steal-time-following">
                <xsl:attribute name="grace">acc</xsl:attribute>
                <xsl:attribute name="grace.time"><xsl:value-of select="grace/@steal-time-following"/></xsl:attribute>
                <xsl:if test="grace/@slash='yes'">
                  <xsl:attribute name="stem.mod">1slash</xsl:attribute>
                </xsl:if>
              </xsl:when>
              <xsl:when test="grace/@steal-time-previous">
                <xsl:attribute name="grace">unacc</xsl:attribute>
                <xsl:attribute name="grace.time"><xsl:value-of select="grace/@steal-time-previous"/></xsl:attribute>
                <xsl:if test="grace/@slash='yes'">
                  <xsl:attribute name="stem.mod">1slash</xsl:attribute>
                </xsl:if>
              </xsl:when> 
              <xsl:otherwise>
                 <xsl:attribute name="grace">unacc</xsl:attribute>
                 <xsl:if test="grace/@slash='yes'">
                   <xsl:attribute name="stem.mod">1slash</xsl:attribute>
                 </xsl:if>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:if>

          <!-- Notated pitch -->
          <xsl:choose>
            <xsl:when test="pitch">
              <xsl:attribute name="pname"><xsl:value-of select="lower-case(pitch/step)"/></xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
              <!-- This note is unpitched, e.g., for unpitched percussion or of indeterminate pitch. -->
              <xsl:attribute name="pname"><xsl:value-of select="lower-case(unpitched/display-step)"/></xsl:attribute>
            </xsl:otherwise>
          </xsl:choose>

          <!-- Notated accidental in attribute. This block is turned off by default, but can be
               turned on with the accidattr stylesheet parameter. -->
          <xsl:if test="$accidattr='yes'">
            <xsl:choose>
              <xsl:when test="accidental/@editorial='yes'">
                <xsl:attribute name="acci.editorial">
                  <xsl:choose>
                    <xsl:when test="accidental = 'sharp'">s</xsl:when>
                    <xsl:when test="accidental = 'natural'">n</xsl:when>
                    <xsl:when test="accidental = 'flat'">f</xsl:when>
                    <xsl:when test="accidental = 'double-sharp'">x</xsl:when>
                    <xsl:when test="accidental = 'sharp-sharp'">ss</xsl:when>
                    <xsl:when test="accidental = 'flat-flat'">ff</xsl:when>
                    <xsl:when test="accidental = 'natural-sharp'">ns</xsl:when>
                    <xsl:when test="accidental = 'natural-flat'">nf</xsl:when>
                    <xsl:when test="accidental = 'quarter-flat'">fd</xsl:when>
                    <xsl:when test="accidental = 'quarter-sharp'">su</xsl:when>
                  </xsl:choose>
                </xsl:attribute>
                <!-- Accidental enclosure in attribute -->
                <xsl:choose>
                  <xsl:when test="accidental/@parentheses='yes'">
                    <xsl:attribute name="enclose.acci">paren</xsl:attribute>
                  </xsl:when>
                  <xsl:when test="accidental/@bracket='yes'">
                    <xsl:attribute name="enclose.acci">brack</xsl:attribute>
                  </xsl:when>
                </xsl:choose>
              </xsl:when>
              <xsl:when test="accidental">
                <xsl:attribute name="acci">
                  <xsl:choose>
                    <xsl:when test="accidental = 'sharp'">s</xsl:when>
                    <xsl:when test="accidental = 'natural'">n</xsl:when>
                    <xsl:when test="accidental = 'flat'">f</xsl:when>
                    <xsl:when test="accidental = 'double-sharp'">x</xsl:when>
                    <xsl:when test="accidental = 'sharp-sharp'">ss</xsl:when>
                    <xsl:when test="accidental = 'flat-flat'">ff</xsl:when>
                    <xsl:when test="accidental = 'natural-sharp'">ns</xsl:when>
                    <xsl:when test="accidental = 'natural-flat'">nf</xsl:when>
                    <xsl:when test="accidental = 'quarter-flat'">f