<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
  xmlns:tei="http://www.tei-c.org/ns/1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common"
  extension-element-prefixes="exsl"
  exclude-result-prefixes="tei exsl" 
  version="1.0">
<xsl:import href="/TEI/web/Stylesheets/teiodds.xsl"/>
<xsl:key name="ATT" match="attribute" use="@en"/>
<xsl:output indent="yes"/>
<xsl:param name="ODDROOT">/TEI/P5/</xsl:param>
<xsl:param name="verbose">true</xsl:param>

<xsl:template match="/">
  <TEI xmlns="http://www.tei-c.org/ns/1.0"
       xmlns:rng="http://relaxng.org/ns/structure/1.0">
    <teiHeader>
      <fileDesc>
	<titleStmt>
	  <title>Spanish TEI</title>
	  <author>Alejandro Bia</author>
	</titleStmt>
	<publicationStmt>
	  <p></p>
	</publicationStmt>
	<sourceDesc>
	  <p>derived from TEIMap 1.1</p>
	</sourceDesc>
      </fileDesc>
    </teiHeader>
    <text>
      <body>
	
	<xsl:variable name="E">
	  <xsl:apply-templates/>
	</xsl:variable>
	
	<xsl:variable name="E2">
	  <xsl:for-each select="exsl:node-set($E)/element">
	    <xsl:sort select="@module"/>
	    <xsl:copy-of select="."/>
	  </xsl:for-each>
	</xsl:variable>
	<p>
	  <xsl:for-each select="exsl:node-set($E2)/element">
	    <xsl:variable name="m" select="@module"/>
	    <xsl:if test="not(preceding-sibling::element/@module=$m)">
	      <module ident="{@module}" mode="change">
		<elementSpec xmlns="http://www.tei-c.org/ns/1.0" ident="{@ident}" mode="change">
		  <xsl:copy-of select="tei:*"/>
		</elementSpec>
		<xsl:for-each
		 select="following-sibling::element[@module=$m]">
		  <elementSpec ident="{@ident}" mode="change" xmlns="http://www.tei-c.org/ns/1.0" >
		    <xsl:copy-of select="tei:*"/>
		  </elementSpec>
		</xsl:for-each>
	      </module>
	    </xsl:if>
	  </xsl:for-each>
	</p>
	<p>
	  <schema ident="tei-es">
	    <xsl:for-each select="exsl:node-set($E2)/element">
	      <xsl:variable name="m" select="@module"/>
	      <xsl:if test="not(preceding-sibling::element/@module=$m)">
		<moduleRef key="{@module}"/>
	      </xsl:if>
	    </xsl:for-each>
	    <moduleRef key="general"/>
	  </schema>
	</p>
      </body>
    </text>
  </TEI>
</xsl:template>

<xsl:template match="i18n">
  <xsl:variable name="TOP" select="/"/>
  <xsl:for-each select="element">
      <xsl:variable name="this" select="@en"/>
      <element ident="{@en}">
	<xsl:attribute name="module">
	  <xsl:for-each
	   select="document(concat($ODDROOT,'Tools/tags.xml'))/Table">
	    <xsl:value-of select="key('TAGIDENTS',$this)/Tagset"/>
	  </xsl:for-each>
	</xsl:attribute>
	<xsl:choose>
	  <xsl:when test="not(starts-with(@es,'_'))">
	    <altIdent xmlns="http://www.tei-c.org/ns/1.0" ><xsl:value-of select="@es"/></altIdent>
	  </xsl:when>
	  <xsl:otherwise>
	    <altIdent xmlns="http://www.tei-c.org/ns/1.0" ><xsl:value-of select="@en"/></altIdent>
	  </xsl:otherwise>
	</xsl:choose>
	  <xsl:for-each
	   select="document(concat($ODDROOT,'Tools/tags.xml'))/Table">
	    <xsl:for-each select="key('TAGIDENTS',$this)">
	      <xsl:if test="Attributes">
		<attList xmlns="http://www.tei-c.org/ns/1.0" >
		  <xsl:for-each select="Attributes/Att">
		    <xsl:if test="not(@n='xmlns')">
		      <xsl:variable name="n" select="@n"/>
		      <xsl:variable name="name">
			<xsl:for-each select="$TOP">
			  <xsl:value-of select="key('ATT',$n)/@es"/>
			</xsl:for-each>
		      </xsl:variable>
		      <xsl:if test="not(starts-with($name,'_')) and not($name='')">
			<attDef ident="{@n}" xmlns="http://www.tei-c.org/ns/1.0" >
			  <altIdent
			   xmlns="http://www.tei-c.org/ns/1.0" >
			    <xsl:value-of select="$name"/>
			  </altIdent>
			</attDef>
		      </xsl:if>
		    </xsl:if>
		  </xsl:for-each>
		</attList>
	      </xsl:if>
	    </xsl:for-each>
	  </xsl:for-each>
      </element>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
