<?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"
    version="2.0">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:param name="pathToTargetFile">itinerar_karte.kml</xsl:param>
    <xsl:variable name="diaryData" select="collection('../../ed000228?select=16*.xml;recurse=no')"/>
    
    <xsl:template match="*" mode="toc">
        <xsl:apply-templates mode="toc"/>
    </xsl:template>
    
    <xsl:template match="/">
        <xsl:result-document href="{$pathToTargetFile}">
            <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
                <folder>
                    <xsl:for-each select="$diaryData//tei:index[@indexName='place']/tei:term[substring(@ref,2)=document('listPlace_lat_lng.xml')//tei:place/@xml:id]">
                        <xsl:variable name="entry" select="ancestor::tei:div[@type='entry']/substring(@xml:id, 6)"/>
                        <Placemark>
                            <name>
                                <xsl:value-of select="translate(document('listPlace_lat_lng.xml')//tei:place[@xml:id=substring(current()/@ref,2)]/tei:placeName,',',';')"/>
                                <!--<xsl:value-of select="substring(ancestor::tei:div[@type='entry']/@xml:id, 14,2)"/>.<xsl:value-of select="substring(ancestor::tei:div[@type='entry']/@xml:id, 11,2)"/>.<xsl:value-of select="substring(ancestor::tei:div[@type='entry']/@xml:id, 6,4)"/>-->
                            </name>
                            <address>
                                <xsl:value-of select="translate(document('listPlace_lat_lng.xml')//tei:place[@xml:id=substring(current()/@ref,2)]/tei:placeName,',',';')"/>
                            </address>
                            <description>
                                <xsl:text>&lt;h2&gt;</xsl:text>
                                <xsl:apply-templates select="ancestor::tei:div[@type='entry']//tei:date" mode="toc"/>
                                <xsl:text>&lt;/h2&gt;</xsl:text>
                                <xsl:text>&lt;ul&gt;</xsl:text>
                                <xsl:for-each select="ancestor::tei:div[@type='entry']//tei:index[@indexName='regest']/tei:term">
                                    <!--<xsl:text>&lt;tr&gt;</xsl:text>-->
                                    <xsl:text>&lt;li&gt;</xsl:text>
                                    <xsl:value-of select="."/>
                                    <xsl:text>&lt;/li&gt;</xsl:text>
                                    <!--<xsl:text>&lt;/tr&gt;</xsl:text>-->
                                </xsl:for-each>
                                <xsl:text>&lt;/ul&gt;</xsl:text>
                                <xsl:text>[
                                &lt;a href="http://diglib.hab.de/content.php?dir=edoc/ed000228&amp;distype=optional&amp;metsID=edoc_ed000228_fg_
                                </xsl:text>
                                <xsl:value-of select="translate(substring($entry, 1, 7), '-', '_')"/>
                                <xsl:text>_sm&amp;xml=</xsl:text>
                                <xsl:value-of select="translate(substring($entry, 1, 7), '-', '_')"/>
                                <xsl:text>.xml&amp;xsl=tei-transcript.xsl#hd</xsl:text>
                                <xsl:value-of select="count(ancestor::tei:div[@type='entry']/preceding-sibling::tei:div[@type='entry']) + 1"/>
                                <xsl:text>"&gt;↗Zum Tageseintrag&lt;/a&gt;]</xsl:text>
                            </description>
                            <TimeStamp>
                                <when><xsl:value-of select="current()/ancestor::tei:div[@type='entry']/substring(@xml:id,6)"/></when>
                            </TimeStamp>
                            <Point>
                                <coordinates>
                                    <xsl:value-of select="substring-after(document('listPlace_lat_lng.xml')//tei:place[@xml:id=substring(current()/@ref,2)]/tei:location/tei:geo,' ')"/>
                                    <xsl:text>,</xsl:text>
                                    <xsl:value-of select="substring-before(document('listPlace_lat_lng.xml')//tei:place[@xml:id=substring(current()/@ref,2)]/tei:location/tei:geo,' ')"/>
                                </coordinates>
                            </Point>
                        </Placemark>
                    </xsl:for-each>
                </folder>
            </kml>            
            
            
        <!--<xsl:text>Address,TimeStamp,Latitude,Longitude,GettyID,TimeSpan:begin,TimeSpan:end&#xA;</xsl:text>
            <xsl:for-each select="$diaryData//tei:index[@indexName='place']/tei:term[substring(@ref,2)=document('listPlace_lat_lng.xml')//tei:place/@xml:id]">
            <xsl:value-of select="translate(document('listPlace_lat_lng.xml')//tei:place[@xml:id=substring(current()/@ref,2)]/tei:placeName,',',';')"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="current()/ancestor::tei:div[@type='entry']/substring(@xml:id,6)"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="substring-before(document('listPlace_lat_lng.xml')//tei:place[@xml:id=substring(current()/@ref,2)]/tei:location/tei:geo,' ')"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="substring-after(document('listPlace_lat_lng.xml')//tei:place[@xml:id=substring(current()/@ref,2)]/tei:location/tei:geo,' ')"/>
            <xsl:text>&#xA;</xsl:text>                        
        </xsl:for-each>-->
        </xsl:result-document>
    </xsl:template>
    
    <xsl:template match="tei:date" mode="toc">
        <xsl:variable name="month" select="substring(@when, 6, 2)"/>
        <!-- Tag -->
        <xsl:value-of select="substring(@when, 9, 2)"/>
        <xsl:text>. </xsl:text>
        <!-- Monat -->
        <xsl:choose>
            <xsl:when test="$month = '01'"><xsl:text>Januar</xsl:text></xsl:when>
            <xsl:when test="$month = '02'"><xsl:text>Februar</xsl:text></xsl:when>
            <xsl:when test="$month = '03'"><xsl:text>März</xsl:text></xsl:when>
            <xsl:when test="$month = '04'"><xsl:text>April</xsl:text></xsl:when>
            <xsl:when test="$month = '05'"><xsl:text>Mai</xsl:text></xsl:when>
            <xsl:when test="$month = '06'"><xsl:text>Juni</xsl:text></xsl:when>
            <xsl:when test="$month = '07'"><xsl:text>Juli</xsl:text></xsl:when>
            <xsl:when test="$month = '08'"><xsl:text>August</xsl:text></xsl:when>
            <xsl:when test="$month = '09'"><xsl:text>September</xsl:text></xsl:when>
            <xsl:when test="$month = '10'"><xsl:text>Oktober</xsl:text></xsl:when>
            <xsl:when test="$month = '11'"><xsl:text>November</xsl:text></xsl:when>
            <xsl:when test="$month = '12'"><xsl:text>Dezember</xsl:text></xsl:when>
        </xsl:choose>
        <xsl:text> </xsl:text>
        <!-- Jahr -->
        <xsl:value-of select="substring(@when, 1, 4)"/>
    </xsl:template>
    
</xsl:stylesheet>