<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tei="http://www.tei-c.org/ns/1.0" exclude-result-prefixes="tei"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0">
    
    <xsl:import href="common.xsl"/>
    
    <!-- Ausgabe des Kommentars (tei-comment.xml), erstellt von Ulrike Henny -->
    
    <xsl:output method="html" encoding="UTF-8" doctype-public="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
    <!-- warum war das hier? <xsl:strip-space elements="tei:cit"/> -->
    
    <xsl:template match="/">
        <html>
            <head>
                <title>Engelbert Kaempfer: Amoenitates Exoticae</title>
                <xsl:copy-of select="$meta"/>
                <xsl:copy-of select="$CSS"/>
                <xsl:copy-of select="$JavaScript" />
            </head>
            <body>
                <div class="comment content">
                    <xsl:apply-templates />
                </div>
                <div class="source">
                    <p>XML-Source: <a href="http://diglib.hab.de/edoc/ed000081/tei-comment.xml">http://diglib.hab.de/edoc/ed000081/tei-comment.xml</a><br/> 
                        XSL-Stylesheet: <a href="http://diglib.hab.de/edoc/ed000081/tei-comment.xsl">http://diglib.hab.de/edoc/ed000081/tei-comment.xsl</a></p>
                    <div class="print"><a href="#" onclick="window.print(); return false"><img src="http://diglib.hab.de/edoc/ed000081/icons/printer.png" /> Seite drucken</a></div>
                </div>
            </body>
        </html>
    </xsl:template>
    
    <!-- div -->
    <xsl:template match="tei:div">
        <div>
            <xsl:call-template name="set-attributes" />
            <xsl:choose>
                <xsl:when test="@xml:id='AEdTN-0'">
                    <h2>Kommentar zum Titel</h2>
                    <xsl:apply-templates />
                </xsl:when>
                <xsl:when test="@xml:id='AEdTN-5'">
                    <h2>Fazikel V - Kommentar zum Titel</h2>
                </xsl:when>
                <xsl:when test="@xml:id='AEdDedN-0'">
                    <h2>Kommentar zur Widmung</h2>
                    <xsl:apply-templates />
                </xsl:when>
                <xsl:when test="@xml:id='AEdPreN-0'">
                    <h2>Kommentar zum Vorwort</h2>
                    <xsl:apply-templates />
                </xsl:when>
                <xsl:when test="@xml:id='AEdPreN-5'">
                    <h2>Faszikel V - Kommentar zum Vorwort</h2>
                    <xsl:apply-templates />
                </xsl:when>
                <xsl:when test="@xml:id != 'AEdN-1' and starts-with(@xml:id, 'AEdN-1')">
                    <h2>Faszikel I - Kommentar zu Relatio <xsl:value-of select="substring-after(@xml:id, '.')"/></h2>
                    <xsl:apply-templates />
                </xsl:when>
                <xsl:when test="@xml:id != 'AEdN-5' and @xml:id != 'AEdN-5-para' and starts-with(@xml:id,'AEdN-5')">
                    <h2>Faszikel V - Kommentar zu Classis <xsl:value-of select="substring-after(@xml:id, '.')"/></h2>
                    <xsl:apply-templates />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates />
                </xsl:otherwise>
            </xsl:choose>     
        </div>
    </xsl:template>
    
    
    <!-- note -->
    <xsl:template match="tei:note">
        <p>
            <xsl:call-template name="set-attributes" />
            <xsl:apply-templates />
            <xsl:if test="@resp != '' and @resp != '#H'">
                 <span> [<xsl:choose>
                     <xsl:when test="contains(@resp, ' ')">
                       <xsl:variable name="parts">
                             <xsl:call-template name="tokenize">
                                 <xsl:with-param name="stringToTokenize" select="@resp" />
                                 <xsl:with-param name="delimiter"><xsl:text> </xsl:text></xsl:with-param>
                             </xsl:call-template>
                       </xsl:variable>  
                       <xsl:value-of select="$parts"/>
                     </xsl:when>
                     <xsl:otherwise>
                         <xsl:variable name="respSingle" select="substring(@resp, 2)" />
                         <xsl:value-of select="document('person.xml')//person[@xml:id = $respSingle]//roleName[@type='person_initials']" />
                     </xsl:otherwise>
                 </xsl:choose>]</span>
            </xsl:if>
        </p>
    </xsl:template>
    
    
    <!-- à la XSLT 2.0 tokenize(), speziell für @resp -->
    <xsl:template name="tokenize">
        <xsl:param name="stringToTokenize" />
        <xsl:param name="delimiter" />
        <xsl:choose>
            <xsl:when test="contains($stringToTokenize, $delimiter)">
                <xsl:variable name="part">
                    <xsl:value-of select="substring-before($stringToTokenize, $delimiter)"/>
                </xsl:variable>
                <xsl:value-of select="concat(document('person.xml')//person[@xml:id = substring($part, 2)]//roleName[@type='person_initials'], ', ')" />
                <xsl:variable name="newString">
                    <xsl:value-of select="substring-after($stringToTokenize, $delimiter)"/>
                </xsl:variable>
                <xsl:if test="$newString != ''">
                    <xsl:call-template name="tokenize">
                        <xsl:with-param name="stringToTokenize" select="$newString" />
                        <xsl:with-param name="delimiter" select="$delimiter" />
                    </xsl:call-template>
                </xsl:if>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="part">
                    <xsl:value-of select="$stringToTokenize"/>
                </xsl:variable>
                <xsl:value-of select="document('person.xml')//person[@xml:id = substring($part, 2)]//roleName[@type='person_initials']" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="tei:note/tei:term[1]">
        <xsl:variable name="id" select="parent::tei:note/@xml:id" />
        <span onclick="syncroAmEx(this, '#CL_{$id}');">
            <xsl:attribute name="class">
                <xsl:value-of select="name()" /> link
            </xsl:attribute>
            <xsl:apply-templates />
        </span>
    </xsl:template>
    
    
    <!-- ############################################## Interne Links ###################################################### -->
    
    <xsl:template match="tei:ref[@type = 'link_internal_commentary']">
        <xsl:variable name="type" select="@type" />
        <!-- bei mehreren Zielen wird das erste ausgewählt -->
        <xsl:variable name="target">
            <xsl:choose>
                <xsl:when test="contains(@target, ' ')"><xsl:value-of select="substring-before(@target, ' ')" /></xsl:when>
                <xsl:otherwise><xsl:value-of select="@target" /></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <a class="link" href="{$target}">
            <xsl:apply-templates />
        </a>
    </xsl:template>
    
    
    
    <!-- figure/graphic -->
    <xsl:template match="tei:figure">
        <p><img src="{tei:graphic/@url}"/></p>
        <xsl:apply-templates />
    </xsl:template>
</xsl:stylesheet>
