<?xml version="1.0" encoding="UTF-8"?>
<!-- http://www.language-archives.org/tools/metadata/metadata.xsl
     A stylesheet for transforming free-standing OLAC metadata
       into a web page following the OLAC Display Format.
     Gary Simons, SIL International (last updated 7 June 2003)

Copyright (c) 2003 Gary Simons (SIL International). This material may be
distributed only subject to the terms and conditions set forth in the
Open Publication License, v1.0 or later (the latest version is presently
available at http://www.opencontent.org/openpub/).
 -->
 <!-- Modified by Marisa Ferrara, The LINGUIST List, 26-JUN-03 to display 
 the metadata as a database upload file in HTML.
-->
 <!--This header calls up the namespaces needed to identify the elements -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:olac="http://www.language-archives.org/OLAC/1.0/">
<xsl:output method="text" />
  <!--This template establishes the TITLE variable and then the HTML display.  TITLE is called upon first as the header, and then the elements will show within the APPLY TEMPLATES area. -->
<xsl:template match="/olac:olac" priority="1">
	<html>
		<head>
			<title>
				<xsl:text></xsl:text>
			</title>
		</head>
		<body>
			<table width="475" align="center">
				<tr>
					<td>
						<xsl:apply-templates/>
					</td>
				</tr>
			</table>
		</body>
	</html>
</xsl:template>
    <!--This template establishes the variables and parameters needed for display -->
<xsl:template match="*">
    <!-- This variable parses the tag name from its namespace heading-->
	<xsl:variable name="tag">
		<xsl:choose>
			<xsl:when test="contains(name(),':')">
				<xsl:value-of select="substring-after(name(), ':')"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="name()"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
       <!-- This variable establishes the value of TAG (the parsed out tag name) under the name LABEL-->
	<xsl:variable name="label">
		<xsl:call-template name="olac-display-format">
			<xsl:with-param name="label">
				<xsl:value-of select="$tag"/>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:variable>
       <!--This variable parses out the _ from the olac:code element extension -->
	<xsl:variable name="code">
		<xsl:value-of select="translate(@olac:code ,'_', ' ')"/>
	</xsl:variable>
       <!-- What follows is the formatting of the display.  This parses out the element content and extensions, and gives them parameter names (PRIMARY CODE, SECONDARY CODE, TERTIARY CODE, FOURTH CODE) to be used in the next template for display -->
		<xsl:choose>
			<xsl:when test="@xsi:type='olac:language'">
                  	<xsl:call-template name="olac-display-format">
                    	<xsl:with-param name="tertiaryCode">
                    		<xsl:value-of select="document('LanguageCodes.xsd')//xs:enumeration[@value=$code]/@label"/>
                    	</xsl:with-param>
                  	</xsl:call-template>
               	</xsl:when>
               	<xsl:when test="@xsi:type='linguistic-field'">
                  	<xsl:call-template name="olac-display-format">
                     	<xsl:with-param name="primaryCode">
                        		<xsl:call-template name="capitalize">
                           		<xsl:with-param name="label">
                            			<xsl:value-of select="@olac:code"/>
                         			</xsl:with-param>
                        		</xsl:call-template>
                    	</xsl:with-param>
                  	</xsl:call-template>
              	</xsl:when>
               	<xsl:when test="@xsi:type='olac:access'">
                  	<xsl:call-template name="olac-display-format">
                     	<xsl:with-param name="primaryCode">
                        		<xsl:call-template name="capitalize">
                           		<xsl:with-param name="label">
                             			<xsl:value-of select="@olac:code"/>
                           		</xsl:with-param>
                       		</xsl:call-template>
                     	</xsl:with-param>
                  	</xsl:call-template>
               	</xsl:when>
               	<xsl:when test="@xsi:type='olac:linguistic-type'">
               		<xsl:call-template name="olac-display-format">
                     		<xsl:with-param name="fourthCode">
                        			<xsl:call-template name="capitalize">
                           			<xsl:with-param name="label">
                             				<xsl:value-of select="@olac:code"/>
                           			</xsl:with-param>
                       			</xsl:call-template>
                     		</xsl:with-param>
                  		</xsl:call-template>
               	</xsl:when>
              	<xsl:when test="@xsi:type='olac:role'">
                  	<xsl:call-template name="olac-display-format">
                    	<xsl:with-param name="secondaryCode">
                        		<xsl:value-of select="@olac:code"/>
                     	</xsl:with-param>
                  	</xsl:call-template>
               	</xsl:when>
               	<xsl:otherwise>
                  		<xsl:call-template name="element-content"/>
               	</xsl:otherwise>
            </xsl:choose>
	</xsl:template>
    <!-- This template capitalizes the first letter of all labels-->	
	<xsl:template name="capitalize">
      		<xsl:param name="label"/>
      			<xsl:value-of select="concat( translate(substring($label,1,1),  'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring($label, 2, string-length($label)-1))"/>
  	</xsl:template>
     <!-- This template establishes the order of the contents to be displayed.  It also marks the last element called with a ||.--> 	
	<xsl:template name="olac-display-format" priority="0">
		<xsl:param name="label"/>
		<xsl:param name="primaryCode"/>
		<xsl:param name="secondaryCode"/>
		<xsl:param name="fourthCode"/>
		<xsl:choose>
			<xsl:when test="$secondaryCode">				
				<xsl:value-of select="$secondaryCode"/>				
				<xsl:text> , </xsl:text>
				<xsl:call-template name="element-content"/>
			</xsl:when>
			<xsl:when test="$primaryCode">
				<xsl:value-of select="$primaryCode"/>
				<xsl:text>,</xsl:text>
				<xsl:call-template name="element-content"/>
			</xsl:when>
			<xsl:when test="$fourthCode">
				<xsl:value-of select="$fourthCode"/>
				<xsl:text>,</xsl:text>
				<xsl:call-template name="element-content"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="element-content"/>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:if test="position() = last()">
			<xsl:value-of select="fourthCode"></xsl:value-of>
			<xsl:text>||</xsl:text>		
		</xsl:if>
	</xsl:template>
	<!--This template establishes the display of all elements and their extensions, followed by the * -->
	<xsl:template name="element-content">
		<xsl:param name="secondaryCode"></xsl:param>
		<xsl:param name="primaryCode"></xsl:param>
		<xsl:param name="fourthCode"></xsl:param>
		<xsl:if test="$secondaryCode">
			<xsl:value-of select="."></xsl:value-of>
		</xsl:if>
      		<xsl:choose>
        		<xsl:when test="$secondaryCode">
         			<xsl:value-of select="."></xsl:value-of>
         		</xsl:when>
         		<xsl:when test="$fourthCode">
         			<xsl:value-of select="."></xsl:value-of>
         		</xsl:when>
         		<xsl:when test="$primaryCode">
         			<xsl:value-of select="$primaryCode"></xsl:value-of>
         			<xsl:text disable-output-escaping="yes">&#x09;</xsl:text>
         		</xsl:when>
         		<xsl:otherwise>
            			<xsl:value-of select="."/>
            			<xsl:text disable-output-escaping="yes">&#x09;</xsl:text>
         		</xsl:otherwise>
      		</xsl:choose>
  	</xsl:template>
</xsl:stylesheet>