<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Author: Lauri Carlson, 2002 May 24 -->
<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"
            indent="yes" />

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

    <xsl:apply-templates/>
    <xsl:element name="Anchor">
      <xsl:attribute name="id">A<xsl:value-of select="count(descendant::ag:Feature[@name='text'])"/></xsl:attribute>
    </xsl:element>
 
 </xsl:template>

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

  <xsl:apply-templates/>

    <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">A<xsl:value-of select="count(preceding::ag:Feature[@name='text'])"/></xsl:attribute>
       <xsl:attribute name="end">A<xsl:value-of select="count(preceding::ag:Feature[@name='text'])+count(descendant::ag:Feature[@name='text'])"/></xsl:attribute>
<!--       <xsl:copy-of select="ag:Feature[not(@text)]" /> -->
       <xsl:apply-templates select="ag:Feature" mode="copy"/>
    </xsl:element>
  
 </xsl:template>

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

 <xsl:if test="@name='text'">
  <xsl:element name="Anchor">
    <xsl:attribute name="id">A<xsl:value-of select="count(preceding::ag:Feature[@name='text'])"/></xsl:attribute>
  </xsl:element>
 </xsl:if>

 </xsl:template>

 <xsl:template match="ag:Feature" mode="copy">

  <xsl:element name="Feature">
    <xsl:for-each select="@*">
	<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
    </xsl:for-each>
    <xsl:if test="@name='text'">
	<xsl:attribute name="id">A<xsl:value-of select="count(preceding::ag:Feature[@name='text'])"/></xsl:attribute>
       	<xsl:attribute name="start">A<xsl:value-of select="count(preceding::ag:Feature[@name='text'])"/></xsl:attribute>
        <xsl:attribute name="end">A<xsl:value-of select="count(preceding::ag:Feature[@name='text'])+1"/></xsl:attribute>
    </xsl:if>
	<xsl:value-of select="."/>
  </xsl:element>

 </xsl:template>

 <xsl:template match="*">

  <xsl:if test="$debug">
   Match at Tag <xsl:copy/> Id <xsl:value-of select="generate-id(.)"/> Position <xsl:number value="position()"/> containing "<xsl:value-of select="."/>": 
   Count of preceding text nodes is <xsl:value-of select="count(preceding::text())"/>;
   Count of inside text nodes is <xsl:value-of select="count(descendant::text())"/>;
  </xsl:if>
 
   <xsl:copy>
   <xsl:apply-templates/>
   </xsl:copy>
 
 </xsl:template>

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

</xsl:transform>


