<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:local="http://www.pantor.com/ns/local" xmlns:edate="http://exslt.org/dates-and-times" xmlns:teix="http://www.tei-c.org/ns/Examples" xmlns:tei="http://www.tei-c.org/ns/1.0" xmlns:html="http://www.w3.org/1999/xhtml" xmlns="http://www.tei-c.org/ns/1.0" xmlns:estr="http://exslt.org/strings" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xd="http://www.pnp-software.com/XSLTdoc" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" xmlns:rng="http://relaxng.org/ns/structure/1.0" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl estr edate" exclude-result-prefixes="html xd exsl estr edate a fo local rng tei teix" version="1.0">
  
  <xd:doc type="stylesheet">
    
<xd:short>Converts the MTE specifications into a teiLite encoding.</xd:short>
    
<xd:author>Tomaž Erjavec, <A href="http://nl.ijs.si/et/">http://nl.ijs.si/et/</A></xd:author>
    
<xd:date>2008-10-29</xd:date>
    
<xd:detail> Converts the TEI P5 MULTEXT-East type morphosyntactic specifications into a teiLite
      encoded version, ready for "printing" with standard TEI P5 XSLT stylesheets. 
</xd:detail>
    
<xd:copyright> This library is free software; you can redistribute it and/or modify it under the
      terms of the GNU Lesser General Public License as published by the Free Software Foundation;
      either version 2.1 of the License, or (at your option) any later version. This library is
      distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
      General Public License for more details. You should have received a copy of the GNU Lesser
      General Public License along with this library; if not, write to the Free Software Foundation,
      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
</xd:copyright>
  
</xd:doc>


  
  <xd:doc type="string">Output localisations for the features.</xd:doc>

  
<xsl:param name="lang-out">en</xsl:param>

  
  <xd:doc type="string">Patch to include TEI header in HTML, converted separately.</xd:doc>

  
<xsl:variable name="teiHeader">
    
<ref>
      
<xsl:attribute name="target">
        
<xsl:value-of select="concat('josMSD-hdr-',$lang-out,'.html')" />
      
</xsl:attribute>
      
<xsl:choose>
        
<xsl:when test="$lang-out = 'sl'">
          
<xsl:text>Kolofon TEI</xsl:text>
        
</xsl:when>
        
<xsl:when test="$lang-out = 'en'">
          
<xsl:text>TEI Header</xsl:text>
        
</xsl:when>
      
</xsl:choose>
    
</ref>
  
</xsl:variable>
  
<xsl:variable name="i18n">
    
<i18n>
      
<codes>
        
<code xml:lang="sl">
          
<desc>Slovenian</desc>
          
<name>Tomaz Erjavec</name>
          
<email>tomaz.erjavec@ijs.si</email>
        
</code>
      
</codes>
      
<entry>
          
<key>Category</key>
          
<text xml:lang="en">Category</text>
          
<text xml:lang="sl">kategorija</text>
      
</entry>
       
<entry>
      
<key>Attribute</key>
        
<text xml:lang="en">Attribute</text>
        
<text xml:lang="sl">atribut</text>
      
</entry>
      
<entry>
        
<key>Value</key>
        
<text xml:lang="en">Value</text>
        
<text xml:lang="sl">vrednost</text>
      
</entry>
      
<entry>
        
<key>Name</key>
        
<text xml:lang="en">Name</text>
        
<text xml:lang="sl">ime</text>
      
</entry>
      
<entry>
        
<key>Code</key>
        
<text xml:lang="en">Code</text>
        
<text xml:lang="sl">koda</text>
      
</entry>
      
<entry>
          
<key>Position</key>
          
<text xml:lang="en">Position</text>
          
<text xml:lang="sl">mesto</text>
        
</entry>
        
<entry>
          
<key>Lexicon</key>
        
<text xml:lang="en">Lexicon</text>
        
<text xml:lang="sl">Leksikon</text>
      
</entry>
      
<entry>
        
<key>MSDs</key>
        
<text xml:lang="en">MSDs</text>
        
<text xml:lang="sl">Oblikoslovne oznake</text>
      
</entry>
    
</i18n>
  
</xsl:variable>
  
  
<xsl:template name="i18n">
    
<xsl:param name="key" />
    
<xsl:value-of select="exsl:node-set($i18n)//tei:entry[tei:key = normalize-space($key)]/tei:text[@xml:lang=$lang-out]" />
  
</xsl:template>
  
  
  <xd:doc type="string">Heading of generated index of categories.</xd:doc>

  
<xsl:variable name="head-categories">
    
<xsl:choose>
      
<xsl:when test="$lang-out='sl'">
        
<head xml:lang="sl">Tabela besednih vrst</head>
      
</xsl:when>
      
<xsl:otherwise>
        
<head xml:lang="en">Category Table</head>
        
<p xml:lang="en">A category (Part-of-Speech) is formally a feature type. A MSD always starts with a category
        code, which should be capitalised.
</p>
        
<p xml:lang="en">The defined categories are, in the following, listed in order of occurence. The second column
        gives the name of the category followed by its code and number of attributes.
</p>
      
</xsl:otherwise>
    
</xsl:choose>
  
</xsl:variable>

  
  <xd:doc type="string">Heading of generated index of attributes.</xd:doc>

  
<xsl:variable name="head-attributes">
    
<xsl:choose>
      
<xsl:when test="$lang-out='sl'">
        
<head xml:lang="sl">Tabela lastnosti</head>
      
</xsl:when>
      
<xsl:otherwise>
        
<head xml:lang="en">Attribute Table</head>
        
<p xml:lang="en">The attributes defined in the tables are, in the following, listed in alphabetical order.
        The first column gives the name of the attribute, the second the name of the category it is
        defined for, and the third the position of the attribute for the category in the MSD.
</p>
      
</xsl:otherwise>
    
</xsl:choose>
  
</xsl:variable>

  
  <xd:doc type="string">Heading of generated index of values.</xd:doc>

  
<xsl:variable name="head-values">
    
<xsl:choose>
      
<xsl:when test="$lang-out='sl'">
        
<head xml:lang="sl">Tabela vrednosti</head>
      
</xsl:when>
      
<xsl:otherwise>
        
<head xml:lang="en">Value Table</head>
      
<p xml:lang="en">The values defined in the tables are, in the following, listed in alphabetical order. The
        first column gives the name of the value, the second column its code and the third lists
        attributes for which the value is appropriate.
</p>
      
</xsl:otherwise>
    
</xsl:choose>
  
</xsl:variable>

  
<xsl:output indent="no" encoding="utf-8" doctype-system="teiP5/teilite.dtd" />
  
<xsl:strip-space elements="*" />

  
<!--xsl:template match="/">
    <body>
      <xsl:apply-templates select="//table[@n='mte']"/>
    </body>
    </xsl:template
-->

  
  <xd:doc>
    
<xd:short>Process PoS table</xd:short>
  
</xd:doc>

  
<xsl:template match="tei:table[@n='msd.cat']">
    
<table rend="frame">
      
<xsl:apply-templates select="@*" />
      
<xsl:apply-templates select="tei:head" />
      
<!-- space separated (and terminated!) list of language localisations to display; '? ' means implicit language -->
      
<xsl:variable name="langs">
        
<xsl:call-template name="cat-langs" />
      
</xsl:variable>
      
<row role="label">
        
<cell role="position">P</cell>
        
<xsl:call-template name="ABCD">
          
<xsl:with-param name="langs" select="$langs" />
          
<xsl:with-param name="A">Attribute</xsl:with-param>
          
<xsl:with-param name="B">Value</xsl:with-param>
          
<xsl:with-param name="C">Code</xsl:with-param>
        
</xsl:call-template>
        
<xsl:call-template name="allLangRow" />
      
</row>
      
<row role="data">
        
<cell role="position">
          
<xsl:value-of select="tei:row[@role='type']/tei:cell[@role = 'position']" />
        
</cell>
        
<xsl:call-template name="ABCD">
          
<xsl:with-param name="langs" select="$langs" />
          
<xsl:with-param name="A" select="tei:row[@role='type']/tei:cell[@role = 'name']" />
          
<xsl:with-param name="B">0</xsl:with-param>
          
<xsl:with-param name="C" select="tei:row[@role='type']/tei:cell[@role = 'code']" />
        
</xsl:call-template>
        
<xsl:call-template name="LangRow">
          
<xsl:with-param name="langs" select="tei:row[@role='type']/tei:cell[@role='lang']" />
        
</xsl:call-template>
      
</row>
      
<xsl:for-each select="tei:row[@role='attribute']">
        
<xsl:for-each select="tei:cell/tei:table/tei:row[@role='value']">
          
<xsl:element name="{local-name()}">
            
<xsl:choose>
              
<xsl:when test="position()=1">
                
<xsl:apply-templates select="ancestor::tei:row[@role='attribute']/tei:cell[@role='position']" />
                
<xsl:call-template name="ABCD">
                  
<xsl:with-param name="langs" select="$langs" />
                  
<xsl:with-param name="A" select="ancestor::tei:row[@role='attribute']/tei:cell[@role='name']" />
                  
<xsl:with-param name="B" select="tei:cell[@role='name']" />
                  
<xsl:with-param name="C" select="tei:cell[@role='code']" />
                
</xsl:call-template>
              
</xsl:when>
              
<xsl:otherwise>
                
<cell />
                
<xsl:call-template name="ABCD">
                  
<xsl:with-param name="langs" select="$langs" />
                  
<xsl:with-param name="B" select="tei:cell[@role = 'name']" />
                  
<xsl:with-param name="C" select="tei:cell[@role = 'code']" />
                
</xsl:call-template>
              
</xsl:otherwise>
            
</xsl:choose>
            
<xsl:call-template name="LangRow">
              
<xsl:with-param name="langs" select="tei:cell[@role='lang']" />
            
</xsl:call-template>
          
</xsl:element>
        
</xsl:for-each>
      
</xsl:for-each>
    
</table>
  
</xsl:template>

  
<xsl:template name="div-atts">
    
<xsl:attribute name="xml:id">
      
<xsl:value-of select="@type" />
      
<xsl:if test="@select">
        
<xsl:text>-</xsl:text>
        
<xsl:value-of select="@select" />
      
</xsl:if>
    
</xsl:attribute>
  
</xsl:template>

  
  <xd:doc>
    
<xd:short>Make link to teiHeader</xd:short>
  
</xd:doc>

  
<xsl:template match="tei:divGen[@type='teiHeader']">
    
<emph rend="bold">
      
<xsl:copy-of select="$teiHeader" />
    
</emph>
    
</xsl:template>

  
  <xd:doc>
    
<xd:short>Generate MULTEXT Category (PoS) table</xd:short>
  
</xd:doc>

  
<xsl:template match="tei:divGen[@type='msd.cats']">
    
<xsl:variable name="langs">
      
<xsl:call-template name="cat-langs" />
    
</xsl:variable>
    
<div type="section">
      
<xsl:copy-of select="$head-categories" />
      
<table rend="frame">
        
<!--head>
          Category table
        </head
-->
        
<row role="label">
          
<xsl:call-template name="ABCD">
            
<xsl:with-param name="langs" select="$langs" />
            
<xsl:with-param name="A">Name</xsl:with-param>
            
<xsl:with-param name="B">Code</xsl:with-param>
          
</xsl:call-template>
          
<xsl:choose>
            
<xsl:when test="$lang-out='sl'">
              
<cell>št. atributov</cell>
            
</xsl:when>
            
<xsl:otherwise>
              
<cell>No. of attributes</cell>
            
</xsl:otherwise>
          
</xsl:choose>
          
<xsl:call-template name="allLangRow" />
        
</row>
        
<xsl:for-each select="//tei:table[@n='msd.cat']">
          
<row role="data">
            
<!--xsl:message>
              <xsl:text>Languages: </xsl:text>
              <xsl:value-of select="$langs"/>
            </xsl:message
-->
            
<xsl:call-template name="ABCD">
              
<xsl:with-param name="langs" select="$langs" />
              
<xsl:with-param name="A" select="tei:row[@role='type']/tei:cell[@role = 'name']" />
              
<xsl:with-param name="B" select="tei:row[@role='type']/tei:cell[@role = 'code']" />
            
</xsl:call-template>
            
<cell role="attno">
              
<xsl:value-of select="count(tei:row[@role='attribute'])" />
            
</cell>
            
<xsl:call-template name="LangRow">
              
<xsl:with-param name="langs" select="tei:row[@role='type']/tei:cell[@role='lang']" />
            
</xsl:call-template>
          
</row>
        
</xsl:for-each>
      
</table>
    
</div>
  
</xsl:template>

  
  <xd:doc>
    
<xd:short>Generate MULTEXT attribute table</xd:short>
  
</xd:doc>

  
<xsl:template match="tei:divGen[@type='msd.atts']">
    
<xsl:variable name="langs">
      
<xsl:call-template name="cat-langs" />
    
</xsl:variable>
    
<xsl:variable name="allvals">
      
<xsl:for-each select="//tei:table[@n='msd.cat']">
        
<xsl:for-each select="tei:row[@role='attribute']">
          
<row role="data">
            
<xsl:call-template name="ABCD">
              
<xsl:with-param name="langs" select="$langs" />
              
<xsl:with-param name="A" select="tei:cell[@role='name']" />
              
<xsl:with-param name="B" select="../tei:row[@role='type']/tei:cell[@role = 'name']" />
            
</xsl:call-template>
            
<xsl:apply-templates select="tei:cell[@role='position']" />
            
<xsl:call-template name="LangRow">
              
<xsl:with-param name="langs" select=".//tei:cell[@role='lang']" />
            
</xsl:call-template>
          
</row>
        
</xsl:for-each>
      
</xsl:for-each>
    
</xsl:variable>
    
<div type="section">
      
<xsl:copy-of select="$head-attributes" />
      
<table rend="frame">
        
<row role="label">
          
<xsl:call-template name="ABCD">
            
<xsl:with-param name="langs" select="$langs" />
            
<xsl:with-param name="A">Attribute</xsl:with-param>
            
<xsl:with-param name="B">Category</xsl:with-param>
          
</xsl:call-template>
          
<cell role="code">
            
<xsl:call-template name="i18n">
              
<xsl:with-param name="key">Position</xsl:with-param>
            
</xsl:call-template>
          
</cell>
          
<xsl:call-template name="allLangRow" />
        
</row>
        
<xsl:apply-templates select="exsl:node-set($allvals)/tei:row" mode="atts">
          
<xsl:sort select="tei:cell[1]" />
          
<xsl:sort select="tei:cell[2]" />
        
</xsl:apply-templates>
      
</table>
    
</div>
  
</xsl:template>
  
<xsl:template match="tei:row" mode="atts">
    
<xsl:apply-templates select="." />
  
</xsl:template>

  
  <xd:doc>
    
<xd:short>Generate MULTEXT value table</xd:short>
  
</xd:doc>

  
<xsl:template match="tei:divGen[@type='msd.vals']">
    
<xsl:variable name="langs">
      
<xsl:call-template name="cat-langs" />
    
</xsl:variable>
    
<xsl:variable name="allvals">
      
<xsl:for-each select="//tei:table[@n='msd.cat']">
        
<xsl:for-each select="tei:row[@role='attribute']">
          
<xsl:variable name="attposition" select="tei:cell[@role='position']" />
          
<xsl:variable name="attname" select="tei:cell[@role='name']" />
          
<xsl:for-each select="tei:cell/tei:table/tei:row[@role='value']">
            
<row>
              
<xsl:call-template name="ABCD">
                
<xsl:with-param name="langs" select="$langs" />
                
<xsl:with-param name="A" select="tei:cell[@role='name']" />
                
<xsl:with-param name="B" select="tei:cell[@role='code']" />
                
<xsl:with-param name="C" select="ancestor::tei:row[@role='attribute']/tei:cell[@role='name']" />
                
<xsl:with-param name="D" select="ancestor::tei:table/tei:row[@role='type']/tei:cell[@role = 'name']" />
              
</xsl:call-template>
              
<xsl:call-template name="LangRow">
                
<xsl:with-param name="langs" select="tei:cell[@role='lang']" />
              
</xsl:call-template>
            
</row>
          
</xsl:for-each>
        
</xsl:for-each>
      
</xsl:for-each>
    
</xsl:variable>
    
<div type="section">
      
<xsl:copy-of select="$head-values" />
      
<table rend="frame">
        
<row role="label">
          
<xsl:call-template name="ABCD">
            
<xsl:with-param name="langs" select="$langs" />
            
<xsl:with-param name="A">Value</xsl:with-param>
            
<xsl:with-param name="B">Code</xsl:with-param>
            
<xsl:with-param name="C">Attribute</xsl:with-param>
            
<xsl:with-param name="D">Category</xsl:with-param>
          
</xsl:call-template>
          
<xsl:call-template name="allLangRow" />
        
</row>
        
<xsl:apply-templates select="exsl:node-set($allvals)/tei:row" mode="vals">
          
<xsl:sort select="tei:cell[1]" />
          
<xsl:sort select="tei:cell[3]" />
          
<xsl:sort select="tei:cell[4]" />
        
</xsl:apply-templates>
      
</table>
    
</div>
  
</xsl:template>
  
<xsl:template match="tei:row" mode="vals">
    
<xsl:apply-templates select="." />
  
</xsl:template>

  
<xsl:template match="tei:table[@n='msd.lex']/tei:head">
    
<xsl:copy>
      
<xsl:copy-of select="./*" />
      
<xsl:text> (</xsl:text>
      
<xsl:value-of select="count(parent::tei:table/tei:row)-1" />
      
<xsl:text>)</xsl:text>
    
</xsl:copy>
  
</xsl:template>

  
  <xd:doc>
    
<xd:short>Select MSD table for a PoS</xd:short>
  
</xd:doc>

  
<xsl:template match="tei:divGen[@type='msd.lex']">
    
<xsl:variable name="cat" select="parent::tei:div//tei:table[@n='msd.cat']/tei:row[@role='type']/tei:cell[@role='name']" />
    
<div type="section">
      
<head>
        
<xsl:call-template name="i18n">
          
<xsl:with-param name="key">Lexicon</xsl:with-param>
        
</xsl:call-template>
      
</head>
      
<xsl:apply-templates select="//tei:table[@n='msd.lex']/../tei:p" />
      
<table rend="frame">
        
<head>
          
<xsl:call-template name="i18n">
            
<xsl:with-param name="key">MSDs</xsl:with-param>
          
</xsl:call-template>
          
<xsl:text> (</xsl:text>
          
<xsl:value-of select="count(//tei:table[@n='msd.lex']/tei:row[tei:cell[2] = $cat or substring-before(tei:cell[2],' ') = $cat])" />
          
<xsl:text>)</xsl:text>
        
</head>
        
<xsl:apply-templates select="//tei:table[@n='msd.lex']/tei:row[1]" />
        
<xsl:for-each select="//tei:table[@n='msd.lex']/tei:row">
          
<xsl:if test="tei:cell[2] = $cat or substring-before(tei:cell[2],' ') = $cat">
            
<xsl:apply-templates select="." />
          
</xsl:if>
        
</xsl:for-each>
      
</table>
    
</div>
  
</xsl:template>

  
  <xd:doc>
    
<xd:short>Output row of all defined languages from table[@id='msd.langs']</xd:short>
  
</xd:doc>

  
<xsl:template name="allLangRow">
    
<xsl:if test="ancestor::tei:div[@xml:id='msd.common']">
      
<!-- dont do it for language particular tables-->
      
<xsl:for-each select="//tei:table[@xml:id='msd.langs']/tei:row[@role='lang']">
        
<xsl:apply-templates select="tei:cell[@role='name']" />
      
</xsl:for-each>
    
</xsl:if>
  
</xsl:template>

  
  <xd:doc>
    
<xd:short>Output row of defined languages from table[@id='msd.langs'] if they are defined in
      $langs
</xd:short>
  
</xd:doc>

  
<xsl:template name="LangRow">
    
<xsl:param name="langs" select="tei:cell[@role='lang']" />
    
<!-- dont do it for language particular tables-->
    
<xsl:if test="ancestor::tei:div[@xml:id='msd.common']">
      
<xsl:for-each select="//tei:table[@xml:id='msd.langs']/tei:row[@role='lang']/tei:cell[@role='code']">
        
<cell role="lang">
          
<!--complications because of attribute languages - defined only thru values-->
          
<xsl:variable name="langcodes">
            
<xsl:apply-templates select="exsl:node-set($langs)" mode="langs">
              
<xsl:with-param name="lang" select="." />
            
</xsl:apply-templates>
          
</xsl:variable>
          
<xsl:value-of select="substring-before($langcodes,' ')" />
        
</cell>
      
</xsl:for-each>
    
</xsl:if>
  
</xsl:template>
  
  <xd:doc>
    
<xd:short>Output label lang only if it matches with selected language</xd:short>
  
</xd:doc>

  
<xsl:template match="*" mode="langs">
    
<xsl:param name="lang" />
    
<xsl:choose>
      
<xsl:when test=". = $lang">
        
<xsl:value-of select="." />
        
<xsl:text> </xsl:text>
      
</xsl:when>
    
</xsl:choose>
  
</xsl:template>

  
<xsl:template name="ABCD">
    
<xsl:param name="langs" />
    
<xsl:param name="A" />
    
<xsl:param name="B" />
    
<xsl:param name="C" />
    
<xsl:param name="D" />
    
<xsl:variable name="lang" select="substring-before($langs,' ')" />
    
<xsl:if test="normalize-space($lang)">
      
<cell>
        
<xsl:call-template name="lang-cell">
          
<xsl:with-param name="lang" select="$lang" />
          
<xsl:with-param name="X" select="$A" />
        
</xsl:call-template>
      
</cell>
      
<xsl:if test="normalize-space($B)">
        
<cell>
          
<xsl:call-template name="lang-cell">
            
<xsl:with-param name="lang" select="$lang" />
            
<xsl:with-param name="X" select="$B" />
          
</xsl:call-template>
        
</cell>
      
</xsl:if>
      
<xsl:if test="normalize-space($C)">
        
<cell>
          
<xsl:call-template name="lang-cell">
            
<xsl:with-param name="lang" select="$lang" />
            
<xsl:with-param name="X" select="$C" />
          
</xsl:call-template>
        
</cell>
      
</xsl:if>
      
<xsl:if test="normalize-space($D)">
        
<cell>
          
<xsl:call-template name="lang-cell">
            
<xsl:with-param name="lang" select="$lang" />
            
<xsl:with-param name="X" select="$D" />
          
</xsl:call-template>
        
</cell>
      
</xsl:if>
      
<xsl:call-template name="ABCD">
        
<xsl:with-param name="langs" select="substring-after($langs,' ')" />
        
<xsl:with-param name="A" select="$A" />
        
<xsl:with-param name="B" select="$B" />
        
<xsl:with-param name="C" select="$C" />
        
<xsl:with-param name="D" select="$D" />
      
</xsl:call-template>
    
</xsl:if>
  
</xsl:template>

  
<xsl:template name="lang-cell">
    
<xsl:param name="lang" />
    
<xsl:param name="X" />
    
<!--xsl:message>
      <xsl:text>[</xsl:text>
      <xsl:value-of select="$lang"/>
       <xsl:text>/</xsl:text>
      <xsl:value-of select="$X"/>
      <xsl:text>]</xsl:text>
    </xsl:message
-->
    
<xsl:choose>
      
<xsl:when test="$X = '0'" />
      
<xsl:when test="exsl:node-set($X)[@xml:lang=$lang]">
        
<xsl:choose>
          
<xsl:when test="$lang = '?'">
            
<xsl:value-of select="exsl:node-set($X)" />
          
</xsl:when>
          
<xsl:otherwise>
            
<xsl:value-of select="exsl:node-set($X)[@xml:lang=$lang]" />
          
</xsl:otherwise>
        
</xsl:choose>
      
</xsl:when>
      
<xsl:when test="exsl:node-set($i18n)//tei:entry[tei:key = normalize-space($X)]">
        
<xsl:call-template name="i18n">
          
<xsl:with-param name="key" select="$X" />
        
</xsl:call-template>
        
<!--xsl:value-of select="exsl:node-set($i18n)//tei:entry[tei:key = $X]/tei:text[@xml:lang=$lang-out]"-->
      
</xsl:when>
            
<xsl:when test="$X">
              
<xsl:value-of select="$X" />
        
<xsl:if test="$lang != '?'">
          
<xsl:text> (</xsl:text>
          
<xsl:value-of select="$lang" />
          
<xsl:text>)</xsl:text>
        
</xsl:if>
      
</xsl:when>
    
</xsl:choose>
  
</xsl:template>

  
<!-- returns the langauges that a particular cat table has localisations for; in common table, returns '? '-->
  
<xsl:template name="cat-langs">
    
<xsl:variable name="langs">
      
<xsl:choose>
        
<xsl:when test="ancestor::tei:div[@xml:id='msd.common']">
          
<xsl:text></xsl:text>
          
<!-- must be followed by space -->
        
</xsl:when>
        
<xsl:when test="self::tei:table">
          
<xsl:message>
            
<xsl:text>Checking table </xsl:text>
            
<xsl:value-of select="tei:row[@role='type']//tei:cell[@role='name']" />
            
<xsl:text> for languages. </xsl:text>
          
</xsl:message>
          
<xsl:for-each select="tei:row[@role='type']//tei:cell[@role='name']/@xml:lang">
            
<!--xsl:message><xsl:text>found </xsl:text><xsl:value-of select="."/></xsl:message-->
            
<xsl:value-of select="." />
            
<xsl:text> </xsl:text>
          
</xsl:for-each>
        
</xsl:when>
        
<xsl:otherwise>
          
<xsl:for-each select="//tei:table[@n='msd.cat']//tei:cell/@xml:lang">
            
<xsl:value-of select="." />
            
<xsl:text> </xsl:text>
          
</xsl:for-each>
        
</xsl:otherwise>
      
</xsl:choose>
    
</xsl:variable>
    
<xsl:call-template name="set">
      
<xsl:with-param name="langs-in" select="$langs" />
    
</xsl:call-template>
    
<!--xsl:message>
      <xsl:text>GOT</xsl:text>
      <xsl:value-of select="$langs"/>
      <xsl:text>MADE</xsl:text>
      <xsl:call-template name="set">
        <xsl:with-param name="langs-in" select="$langs"/>
      </xsl:call-template>
      </xsl:message
-->
  
</xsl:template>
  
<xsl:template name="set">
    
<xsl:param name="langs-in" />
    
<xsl:param name="langs-out" />
    
<xsl:choose>
      
<xsl:when test="normalize-space($langs-in)">
        
<xsl:variable name="lang" select="substring-before($langs-in,' ')" />
        
<xsl:variable name="rest" select="substring-after($langs-in,' ')" />
        
<xsl:choose>
          
<xsl:when test="contains($langs-out,$lang)">
            
<xsl:call-template name="set">
              
<xsl:with-param name="langs-in" select="$rest" />
              
<xsl:with-param name="langs-out" select="$langs-out" />
            
</xsl:call-template>
          
</xsl:when>
          
<xsl:otherwise>
            
<xsl:call-template name="set">
              
<xsl:with-param name="langs-in" select="$rest" />
              
<xsl:with-param name="langs-out" select="concat($langs-out,$lang,' ')" />
            
</xsl:call-template>
          
</xsl:otherwise>
        
</xsl:choose>
      
</xsl:when>
      
<xsl:otherwise>
        
<xsl:value-of select="$langs-out" />
      
</xsl:otherwise>
    
</xsl:choose>
  
</xsl:template>

  
  <xd:doc>
    
<xd:short>By default pass through</xd:short>
  
</xd:doc>


  
<xsl:template match="tei:table">
    
<xsl:element name="{local-name()}">
      
<xsl:apply-templates select="@*" />
      
<xsl:attribute name="rend">frame</xsl:attribute>
      
<xsl:apply-templates />
    
</xsl:element>
  
</xsl:template>
  
<xsl:template match="@rows" />
  
<xsl:template match="@cols" />

  
<!-- ignore localisaton in cell, to display AVMs in all languages -->
  
<xsl:template match="tei:cell">
    
<xsl:element name="{local-name()}">
      
<xsl:apply-templates select="@*" />
      
<xsl:apply-templates />
    
</xsl:element>
  
</xsl:template>
  
  
<xsl:template match="tei:*">
    
<xsl:choose>
      
<xsl:when test="normalize-space($lang-out) and @xml:lang and @xml:lang!=$lang-out">
          
<xsl:variable name="gi" select="name()" />
        
<xsl:if test="not(preceding-sibling::*[@xml:lang=$lang-out]) and not(following-sibling::*[@xml:lang=$lang-out])">
          
<xsl:element name="{local-name()}">
            
<xsl:apply-templates select="@*" />
            
<xsl:apply-templates />
          
</xsl:element>
        
</xsl:if>
      
</xsl:when>
      
<xsl:otherwise>
        
<xsl:element name="{local-name()}">
          
<xsl:apply-templates select="@*" />
          
<xsl:apply-templates />
        
</xsl:element>
      
</xsl:otherwise>
    
</xsl:choose>
  
</xsl:template>

  
<xsl:template match="*">
    
<xsl:copy>
      
<xsl:apply-templates select="@*" />
      
<xsl:apply-templates />
    
</xsl:copy>
  
</xsl:template>

  
<xsl:template match="@*">
    
<xsl:copy />
  
</xsl:template>
  
</xsl:stylesheet>






































v