<?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:xhtml="https://www.w3.org/1999/xhtml/"
                exclude-result-prefixes="xs tei xhtml"
                version="2.0">

    <xsl:output encoding="utf-8" method="xhtml" indent="yes"/>

    <!-- Dein bestehendes Template für teiHeader -->
    <xsl:template match="tei:teiHeader">
        <details>
            <summary>Metadaten aus <pre style="display: inline;">teiHeader</pre></summary>
            <ul>
                <details>
                    <summary>Text-ID:</summary>
                    <ul>
                        <xsl:apply-templates select="//tei:text/@xml:id"/>
                    </ul>
                </details>
                <details>
                    <summary>Typ-Index: <pre style="display: inline;"><a href="http://www.folklore.uni-jena.de/?content=attribut_a" target="_blank">(Link zum Schema)</a></pre></summary>
                    <ul>
                        <xsl:apply-templates select="//tei:keywords[@scheme = 'http://www.folklore.uni-jena.de/?content=attribut_a']/tei:list/tei:item"/>
                    </ul>
                </details>
                <details>
                    <summary>Motiv-Index: <pre style="display: inline;"><a href="http://www.folklore.uni-jena.de/?content=attribut_c" target="_blank">(Link zum Schema)</a></pre></summary>
                    <ul>
                        <xsl:apply-templates select="//tei:keywords[@scheme = 'http://www.folklore.uni-jena.de/?content=attribut_c']/tei:list/tei:item/tei:ref"/>
                    </ul>
                </details>
                <details>
                    <summary>⚠ Vorurteile im Märchen: <pre style="display: inline;"><a href="http://www.folklore.uni-jena.de/?content=vorurteile" target="_blank">(Link zum Schema)</a></pre></summary>
                    <ul>
                        <xsl:apply-templates select="//tei:keywords[@scheme = 'http://www.folklore.uni-jena.de/?content=vorurteile']/tei:list/tei:item"/>
                    </ul>
                </details>
                <details>
                    <summary>Digitale Quelle:</summary>
                    <ul>
                        <xsl:apply-templates select="//tei:fileDesc/tei:sourceDesc"/>
                    </ul>
                </details>
            </ul>
        </details>
    </xsl:template>

    <!-- Bestehende Templates für Quellen, Keywords, IDs, Absätze -->
    <xsl:template match="tei:sourceDesc/tei:bibl[@type = 'digitalSource']/tei:ref">
        <li>
            <xsl:choose>
                <xsl:when test="@target">
                    <a href="{@target}" target="_blank">
                        <xsl:value-of select="."/>
                    </a>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </li>
    </xsl:template>

    <xsl:template match="tei:textClass/tei:keywords[@scheme = 'http://www.folklore.uni-jena.de/?content=attribut_a']/tei:list/tei:item">
        <li><xsl:value-of select="."/></li>
    </xsl:template>

    <xsl:template match="tei:textClass/tei:keywords[@scheme = 'http://www.folklore.uni-jena.de/?content=attribut_c']/tei:list/tei:item/tei:ref">
        <li>
            <xsl:choose>
                <xsl:when test="@target">
                    <a href="{@target}" target="_blank">
                        <xsl:value-of select="."/>
                    </a>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </li>
    </xsl:template>

    <xsl:template match="tei:textClass/tei:keywords[@scheme = 'http://www.folklore.uni-jena.de/?content=vorurteile']/tei:list/tei:item">
        <li><xsl:value-of select="."/></li>
    </xsl:template>

    <xsl:template match="tei:text/@xml:id">
        <li><xsl:value-of select="."/></li>
    </xsl:template>

    <xsl:template match="tei:text//tei:p">
        <p><xsl:value-of select="."/></p>
    </xsl:template>

    <!-- NEU: Endnoten-Verweise im Text -->
    <xsl:template match="tei:note[@place='end']">
        <sup>
            <a href="#note{count(preceding::tei:note[@place='end'])+1}">
                <xsl:value-of select="count(preceding::tei:note[@place='end'])+1"/>
            </a>
        </sup>
    </xsl:template>

    <!-- Hauptstruktur -->
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title>
                    <xsl:value-of select="/tei:TEI/tei:teiHeader[1]/tei:fileDesc[1]/tei:titleStmt[1]/tei:title[1]"/>
                </title>
            </head>
            <body>
                <xsl:call-template name="title"/>
                <xsl:apply-templates/>

                <!-- NEU: Endnoten-Sammlung -->
                <xsl:if test="//tei:note[@place='end']">
                    <hr/>
                    <div id="endnotes">
                        <h2>Endnoten</h2>
                        <ol>
                            <xsl:for-each select="//tei:note[@place='end']">
                                <li id="note{position()}">
                                    <xsl:apply-templates/>
                                </li>
                            </xsl:for-each>
                        </ol>
                    </div>
                </xsl:if>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="title">
        <h1><xsl:value-of select="//tei:titleStmt[1]/tei:title[1]"/></h1>
    </xsl:template>

    <xsl:template match="node() | @*">
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>
