<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    xmlns="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="xs"
    version="2.0" xpath-default-namespace="http://sub.uni-goettingen.de/edxml#">
    
    <xsl:template match="@xml:lang">
        <xsl:attribute name="data-lang"><xsl:value-of select="data(.)"/></xsl:attribute>
    </xsl:template>
    
    <xsl:template match="@n">
        <xsl:attribute name="data-n"><xsl:value-of select="data(.)"/></xsl:attribute>
        <!--<xsl:attribute name="data-roman-n"><xsl:number value="data(.)" format="I"/></xsl:attribute>-->
    </xsl:template>
    
    <xsl:template match="philology">
        <div class="philology section" 
            id="{if(@xml:id) then (@xml:id) else ('facs_'||generate-id(.))}">
            <xsl:apply-templates />
        </div>
    </xsl:template>
    
    <xsl:template match="philology/units">
        <div class="units">
            <xsl:apply-templates />
            <!--<table>
                <xsl:apply-templates mode="table"/>
            </table>-->
        </div>
    </xsl:template>
    
    <xsl:template match="philology//unit">
        <xsl:variable name="id" select="if(@xml:id) then (@xml:id) else ('facs_'||generate-id(.))"/>
        <xsl:variable name="corresp" select="if(@corresp) then (tokenize(@corresp, ' ')) else (false())"/>
        <div class="unit" id="{$id}" data-corresp="{$corresp}">
            <div class="unit-container">
                <!--<div class="anchor-container">
                    <a class="anchor" href="#{$id}">
                        <span class="unit-label">Kolon</span>
                        <span class="unit-nr">
                            <xsl:apply-templates select="@n"/>
                            <xsl:attribute name="data-count-preceding"><xsl:value-of select="count(preceding::unit)+1"/></xsl:attribute>
                            <xsl:attribute name="data-count-preceding-sibling"><xsl:value-of select="count(preceding-sibling::unit)+1"/></xsl:attribute>
                        </span>
                        <span class="unit-count">
                            <xsl:value-of select="count(preceding::unit)+1"/>
                        </span>
                    </a>
                </div>-->
                <xsl:apply-templates />
                <xsl:if test="not(child::notes)">
                    <div class="notes section-tab"/>
                </xsl:if>
            </div>
        </div>
    </xsl:template>
    
    <xsl:template match="philology//unit" mode="table">
        <xsl:variable name="id" select="if(@xml:id) then (@xml:id) else ('facs_'||generate-id(.))"/>
        <xsl:variable name="corresp" select="if(@corresp) then (tokenize(@corresp, ' ')) else (false())"/>
        <tr class="unit" id="{$id}" data-corresp="{$corresp}">
            <td class="head hide">
                <a class="anchor" href="#{$id}">
                    <span class="unit-label">Kolon</span>
                    <span class="unit-nr"><xsl:apply-templates select="@n"/></span>
                </a>
            </td>
            <xsl:apply-templates mode="table" select="transcription"/>
            <xsl:apply-templates mode="table" select="translation"/>
        </tr>
    </xsl:template>
    
    
    
    
    <xsl:template match="philology//transcription">
        <div class="transcription section-tab">
            <xsl:apply-templates select="@*|node()"/>
        </div>
    </xsl:template>
    
    <xsl:template match="transcription/@type">
        <xsl:attribute name="data-type"><xsl:value-of select="data(.)"/></xsl:attribute>
    </xsl:template>
    
    <xsl:template match="philology//transcription" mode="table">
        <td class="transcription section-tab">
            <xsl:apply-templates select="@*"/>
            <table>
                <tr>
                <xsl:for-each select="node()[not(normalize-space(.)='')]">
                    <xsl:apply-templates select="." mode="table-items"/>
                </xsl:for-each>
                </tr>
            </table>
        </td>
    </xsl:template>
    
    
    
    
    
    
    
    <xsl:template match="philology//translation">
        <div class="translation section-tab">
            <xsl:apply-templates select="@*|node()"/>
        </div>
    </xsl:template>
    
    <xsl:template match="philology//translation" mode="table">
        <td class="translation section-tab">
            <xsl:apply-templates select="@*|node()"/>
        </td>
    </xsl:template>
    
    <xsl:template match="translation/element()|transcription/element()">
        <span class="{local-name()} item">
            <span class="conti">
                <xsl:apply-templates/>
            </span>
        </span>
    </xsl:template>
    
    <xsl:template match="translation/text()[not(normalize-space(.)='')]|transcription/text()[not(normalize-space(.)='')]">
        <span class="text item">
            <xsl:value-of select="."/>
        </span>
    </xsl:template>
    
    
    <xsl:template match="philology//lb[@n]">
        <span class="lb item">
            <xsl:value-of select="data(@n)"/>
        </span>
    </xsl:template>
    
    
    <xsl:template match="philology//phr">
        <xsl:variable name="id" select="if(@xml:id) then (@xml:id) else ('facs_'||generate-id(.))"/>
        <xsl:variable name="corresp" select="if(@corresp) then (tokenize(@corresp, ' ')) else (false())"/>
        <span class="phr" id="{$id}">
            <xsl:apply-templates/>
        </span>
    </xsl:template>
    
    
    <xsl:template match="philology//w">
        <xsl:variable name="id" select="if(@xml:id) then (@xml:id) else ('facs_'||generate-id(.))"/>
        <xsl:variable name="corresp" select="if(@corresp) then (tokenize(@corresp, ' ')) else (false())"/>
        <span class="w item" tabindex="1" id="{$id}">
            <span class="body">
                <xsl:apply-templates/>
            </span>
            <span class="annotation">
                <span class="ana">
                    <xsl:choose>
                        <xsl:when test="@ana">
                            <xsl:value-of select="@ana"/>
                        </xsl:when>
                        <xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
                    </xsl:choose>
                </span>
                <span class="lemma">
                    <xsl:choose>
                        <xsl:when test="@lemma">
                            <xsl:value-of select="@lemma"/>
                        </xsl:when>
                        <xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
                    </xsl:choose>
                </span>
            </span>
        </span>
    </xsl:template>
    
    <xsl:template match="philology//notes">
        <div class="notes section-tab">
            <xsl:apply-templates/>
        </div>
    </xsl:template>
    
    <xsl:template match="philology//note">
        <span class="note">📌
            <span class="notetext">
                <xsl:apply-templates/>
            </span>
        </span>        
    </xsl:template>
    
    
    <!-- TABLE ITEMS -->
    <xsl:template match="node()" mode="table-items">
        <td class="{local-name()}">
            <xsl:apply-templates/>
        </td>
    </xsl:template>
    
    <xsl:template match="philology//lb" mode="table-items">
        <td class="lb">
            <xsl:apply-templates select="@*"/>
        </td>
    </xsl:template>
   
    <xsl:template match="philology//phr" mode="table-items">
        <xsl:variable name="id" select="if(@xml:id) then (@xml:id) else ('facs_'||generate-id(.))"/>
        <xsl:variable name="corresp" select="if(@corresp) then (tokenize(@corresp, ' ')) else (false())"/>
        <td class="phr" id="{$id}">
            <table>
                <tr>
                    <xsl:apply-templates mode="table-items"/>
                </tr>
            </table>
        </td>
    </xsl:template>
    
    <xsl:template match="philology//w" mode="table-items">
        <xsl:variable name="id" select="if(@xml:id) then (@xml:id) else ('facs_'||generate-id(.))"/>
        <xsl:variable name="corresp" select="if(@corresp) then (tokenize(@corresp, ' ')) else (false())"/>
        <td class="w item" tabindex="1" id="{$id}">
            <span class="ana">
                <xsl:choose>
                    <xsl:when test="@ana">
                        <xsl:value-of select="@ana"/>
                    </xsl:when>
                    <xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
                </xsl:choose>
            </span>
            <span class="lemma">
                <xsl:choose>
                    <xsl:when test="@lemma">
                        <xsl:value-of select="@lemma"/>
                    </xsl:when>
                    <xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
                </xsl:choose>
            </span>
            <span class="value">
                <xsl:apply-templates/>
            </span>
        </td>
    </xsl:template>
    
    
    
</xsl:stylesheet>