<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Author: Lauri Carlson, 2002 Aug 15 -->
<xsl:transform
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ag="http://www.ldc.upenn.edu/atlas/ag/"
  exclude-result-prefixes="ag"
  version="1.0"
  >
  
  <xsl:strip-space elements="*"/>
  
  <!--<xsl:variable name="debug" select="true()"/>-->
  <xsl:variable name="debug" select="false()"/>
  
  <xsl:output method="xml"
    encoding="ISO-8859-1"
    doctype-system="ag.dtd"
    cdata-section-elements="MetadataElement"
    indent="yes" />


<xsl:template match="ag:Metadata">

 <xsl:variable name="xmax">
    <xsl:value-of select="ag:MetadataElement[@id='xmax']"/>
 </xsl:variable>

 <xsl:variable name="size">
    <xsl:value-of select="ag:MetadataElement[@id='size']"/>
 </xsl:variable>

<xsl:copy>
<xsl:element name="MetadataElement">
<xsl:attribute name="name"><xsl:value-of select='generate-id()'/></xsl:attribute>
File type = "ooTextFile"
Object class = "TextGrid"
      
xmin = 0
xmax = <xsl:value-of select='$xmax'/>
tiers? &lt;exists&gt;
size = <xsl:value-of select='count(//ag:AG)'/>
item []:

</xsl:element>
</xsl:copy>
</xsl:template>
  
<xsl:template match="ag:AG">

<xsl:variable name="xmax">
<xsl:value-of select="//ag:MetadataElement[@id='xmax']"/>
</xsl:variable>

<xsl:variable name="size">
<xsl:value-of select="count(ag:Annotation)+
count(ag:Annotation[@end != following-sibling::ag:Annotation[position() = 1]/@start])+
count(ag:Annotation[not(preceding-sibling::ag:Annotation) and ./@start != 'A0'])"/>
</xsl:variable>

<xsl:variable name="tier">
  <xsl:value-of select='position()-1'/>
</xsl:variable>

<xsl:copy>

<xsl:element name="Metadata">
<xsl:element name="MetadataElement">
<xsl:attribute name="name"><xsl:value-of select='generate-id()'/></xsl:attribute> 
    item [<xsl:value-of select='$tier'/>]:
        class = "IntervalTier"
        name = "<xsl:value-of select='@id'/>"
	xmin = 0
	xmax = <xsl:value-of select='$xmax'/>
	intervals: size = <xsl:value-of select='$size'/>
<xsl:text>
</xsl:text>
</xsl:element>
</xsl:element>
	<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
  
<xsl:template match="ag:Anchor">
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="ag:Annotation">

<xsl:if test='not(preceding-sibling::ag:Annotation) and ./@start != "A0"'>
    <xsl:element name="Annotation">
        <xsl:for-each select="@*">
	 <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
        </xsl:for-each>
	<xsl:attribute name="start">A0</xsl:attribute>
	<xsl:attribute name="end"><xsl:value-of select="@start"/></xsl:attribute>
	<xsl:element name="Feature"><xsl:attribute name="name">text</xsl:attribute>""</xsl:element>
    </xsl:element>
</xsl:if>

<xsl:copy-of select='.'>
</xsl:copy-of>

<xsl:if test='self::node()/@end != following-sibling::ag:Annotation[position() = 1]/@start '>
    <xsl:element name="Annotation">
        <xsl:for-each select="@*">
	 <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
        </xsl:for-each>
	<xsl:attribute name="start"><xsl:value-of select='./@end'/></xsl:attribute>
	<xsl:attribute name="end"><xsl:value-of select='following-sibling::ag:Annotation[position() = 1]/@start '/></xsl:attribute>
        <xsl:for-each select="ag:Feature[@name != 'text']">
	 <xsl:copy-of select="."/>
        </xsl:for-each>
	<xsl:element name="Feature"><xsl:attribute name="name">text</xsl:attribute>""</xsl:element>
    </xsl:element>

</xsl:if>


</xsl:template>

<xsl:template match="ag:AGSet">
<xsl:copy>
      <xsl:apply-templates/>
</xsl:copy>
</xsl:template>

  <xsl:template match="*">
    
      <xsl:apply-templates/>
    
  </xsl:template>
  
  <xsl:template match="comment()|processing-instruction()">
    <xsl:copy />
  </xsl:template>
  
</xsl:transform>






