<?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"
    indent="yes" />

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

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

<xsl:variable name="target">
  <xsl:value-of select="//ag:Annotation[@type='div']"/>
</xsl:variable>

<xsl:variable name="xmax">
  <xsl:value-of select="string-length($target)"/>
</xsl:variable>

<xsl:variable name="size">
  <xsl:value-of select="count(//ag:Annotation[@type='div']/ag:Feature[@name='text'])"/>
</xsl:variable>

    <xsl:element name="Metadata">
	<xsl:element name="MetadataElement">
	<xsl:attribute name="id">size</xsl:attribute>
	  <xsl:value-of select="$size"/>
        </xsl:element>
	<xsl:element name="MetadataElement">
	<xsl:attribute name="id">xmax</xsl:attribute>
	  <xsl:value-of select="$xmax"/>
        </xsl:element>
    </xsl:element>
  
    <xsl:element name="AG">
      <xsl:attribute name="id">phrase</xsl:attribute>
      <xsl:for-each select="ag:Anchor">
	<xsl:copy-of select="."/> 
      </xsl:for-each>	  
      <xsl:for-each select="ag:Annotation[@type='phrase']">
	<xsl:copy-of select="."/> 
      </xsl:for-each>	  
    </xsl:element>
    
    <xsl:element name="AG">
      <xsl:attribute name="id">K</xsl:attribute>
      <xsl:attribute name="type">turn</xsl:attribute>
      <xsl:for-each select="ag:Annotation[@type='turn'][ag:Feature[@name='who']/text()='K']">
	<xsl:copy-of select="."/> 
      </xsl:for-each>	  
    </xsl:element>
    
    <xsl:element name="AG">
      <xsl:attribute name="id">V</xsl:attribute>
      <xsl:attribute name="type">turn</xsl:attribute>
      <xsl:for-each select="ag:Annotation[@type='turn'][ag:Feature[@name='who']/text()='V']">
	<xsl:copy-of select="."/> 
      </xsl:for-each>	  
    </xsl:element>
    
  </xsl:template>
  
  <xsl:template match="*">
    
    <xsl:if test="$debug">
      Match at Tag <xsl:copy/> Position <xsl:number value="position()"/> <!--containing "<xsl:value-of select="."/>":--> 
    </xsl:if>
    
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
    
  </xsl:template>
  
  <xsl:template match="comment()|processing-instruction()">
    <xsl:copy />
  </xsl:template>
  
</xsl:transform>


