<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://www.tei-c.org/ns/1.0" version="2.0">
    <xsl:template match="TEI"> 
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title><xsl:value-of select="teiHeader/fileDesc/titleStmt/title"/></title>
<style type="text/css">
      p {font-family:  'Times New Roman'}
                </style>
            </head>
            <body> 
                <xsl:apply-templates select="text/body"/>
           
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="listBibl">
        <xsl:for-each select="biblStruct">
       <!--     <xsl:sort select="descendant::surname[1]"/>    -->      
            <p style="margin-bottom:01em; margin-top:1em">
                    <xsl:choose>
             <xsl:when test="descendant::title[@level='j']">
                <xsl:text>TY - JOUR</xsl:text><br/>  
            </xsl:when>    
                        <xsl:when test="descendant::title[@level='m'] and descendant::title[@level='a']">
                            <xsl:text>TY - CHAP</xsl:text><br/>  
                        </xsl:when>    
            <xsl:otherwise>   
                <xsl:text>TY - BOOK</xsl:text><br/> 
            </xsl:otherwise>    
                    </xsl:choose>                
                <xsl:apply-templates/><xsl:text>ER - </xsl:text></p>
            <br/>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template match="author">
        <xsl:for-each select="surname"><xsl:text>AU - </xsl:text><xsl:apply-templates/>, </xsl:for-each><xsl:for-each select="forename"><xsl:apply-templates/></xsl:for-each><xsl:text> </xsl:text><xsl:for-each select="gloss"><xsl:apply-templates/></xsl:for-each><br/>
    </xsl:template>
    
    <xsl:template match="editor">
        <xsl:for-each select="surname"><xsl:text>A3 - </xsl:text><xsl:apply-templates/>, </xsl:for-each><xsl:for-each select="forename"><xsl:apply-templates/></xsl:for-each><xsl:text> </xsl:text><xsl:for-each select="gloss"><xsl:apply-templates/></xsl:for-each><br/>
    </xsl:template>
   
   <xsl:template match="title">
       <xsl:choose>
           <xsl:when test="@level='a'"><xsl:text>TI - </xsl:text><xsl:apply-templates/><br/>
           </xsl:when>
           <xsl:when test="@level='j'"><xsl:text>T2 - </xsl:text><xsl:apply-templates/><br/></xsl:when>
                    
           <xsl:when test="@level='m' and ancestor::biblStruct[1]/analytic/title[@level='a']">
                   <xsl:choose>
                       <xsl:when test="parent::title[@level='a']">
                           <xsl:apply-templates/>
                       </xsl:when>
                       <xsl:otherwise>     <xsl:text>T2 - </xsl:text><xsl:apply-templates/><br/></xsl:otherwise>
                   </xsl:choose>
          
                   
           </xsl:when>
           <xsl:otherwise>
               <xsl:text>TI - </xsl:text><xsl:apply-templates/><br/>
           </xsl:otherwise>
           
       </xsl:choose>
       
   </xsl:template>
    
    <xsl:template match="pubPlace">
        <xsl:text>CY - </xsl:text><xsl:apply-templates/><br/>
    </xsl:template>
    
    <xsl:template match="publisher">
        <xsl:text>PB - </xsl:text><xsl:apply-templates/><br/>
    </xsl:template>
    
    <xsl:template match="date">
        <xsl:text>PY - </xsl:text><xsl:apply-templates/><br/>
        <xsl:text>DA - </xsl:text><xsl:apply-templates/><xsl:text>///</xsl:text><br/>
    </xsl:template>
    
    <!--<xsl:variable name="pagespan"><xsl:value-of select="//biblScope[@unit='pp']"/></xsl:variable>-->
      
    <xsl:template match="biblScope">
        <xsl:choose>
            <xsl:when test="@unit='vol' or @unit='volume'"><xsl:text>VL - </xsl:text><xsl:apply-templates/><br/>
            </xsl:when>
            <xsl:when test="@unit='issue'"><xsl:text>IS - </xsl:text><xsl:apply-templates/><br/></xsl:when>
            <!--<xsl:when test="@unit='pp'">  
                <xsl:if test="contains($pagespan, '–')  or contains($pagespan, '-')">
     <xsl:apply-templates/><br/>
        </xsl:if></xsl:when>-->
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="biblScope[@unit='pp']">
        <xsl:variable name="pText" select="."/>
        <xsl:if test="contains($pText, '–')">
        <xsl:text>SP - </xsl:text><xsl:value-of select="substring-before($pText, '–')"/><br/>
        <xsl:text>EP - </xsl:text><xsl:value-of select="substring-after($pText, '–')"/><br/>
        </xsl:if>
        <xsl:if test="contains($pText, '-')">
            <xsl:text>SP - </xsl:text><xsl:value-of select="substring-before($pText, '-')"/><br/>
            <xsl:text>EP - </xsl:text><xsl:value-of select="substring-after($pText, '-')"/><br/>
        </xsl:if>
    </xsl:template>  
    
    <xsl:template match="note">
        <xsl:text>N1 - </xsl:text><!--&lt;p&gt;--><xsl:apply-templates/><!--&lt;/p&gt;--><br/>
    </xsl:template>
    
    <xsl:template match="gloss">
        <xsl:text> </xsl:text><xsl:apply-templates/>
    </xsl:template>
    
    
</xsl:stylesheet>