<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  exclude-result-prefixes="rdf">

  <!--
    Generische Darstellung für RDF/XML-Dateien.
    Das Stylesheet enthält keine projektspezifischen Klassen oder Properties.
  -->
  <xsl:output method="html" version="5" encoding="UTF-8" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <html lang="de">
      <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <title>RDF-Daten</title>
        <style>
          /* body { max-width: 1000px; margin: 2rem auto; padding: 0 1rem; font-family: sans-serif; line-height: 1.45; } */
          body { margin: 2rem auto; padding: 0 1rem; font-family: sans-serif; line-height: 1.45; } 
          section { margin-bottom: 2rem; }
          table { width: 100%; border-collapse: collapse; }
          th, td { padding: .4rem; border: 1px solid; text-align: left; vertical-align: top; }
          th { width: 25%; }
          a, code { overflow-wrap: anywhere; }
          .nested { margin: .75rem 0 .25rem 1rem; }
          .meta { margin: .25rem 0 1rem; }
        </style>
      </head>
      <body>
        <h1>RDF-Daten</h1>
        <xsl:choose>
          <xsl:when test="rdf:RDF/*">
            <p><xsl:value-of select="count(rdf:RDF/*)"/> Ressource(n)</p>
            <xsl:apply-templates select="rdf:RDF/*" mode="resource">
              <xsl:with-param name="level" select="2"/>
            </xsl:apply-templates>
          </xsl:when>
          <xsl:when test="/*">
            <xsl:apply-templates select="/*" mode="resource">
              <xsl:with-param name="level" select="2"/>
            </xsl:apply-templates>
          </xsl:when>
          <xsl:otherwise>
            <p>Keine RDF-Ressource gefunden.</p>
          </xsl:otherwise>
        </xsl:choose>
      </body>
    </html>
  </xsl:template>

  <!-- Eine RDF-Ressource: typisiertes Element oder rdf:Description -->
  <xsl:template match="*" mode="resource">
    <xsl:param name="level" select="3"/>
    <section>
      <xsl:choose>
        <xsl:when test="$level = 2">
          <h2><xsl:call-template name="resource-title"/></h2>
        </xsl:when>
        <xsl:otherwise>
          <h3><xsl:call-template name="resource-title"/></h3>
        </xsl:otherwise>
      </xsl:choose>

      <p class="meta">
        <strong>Element:</strong>
        <xsl:text> </xsl:text>
        <code><xsl:value-of select="name()"/></code>
        <xsl:if test="@rdf:about">
          <br/>
          <strong>URI:</strong>
          <xsl:text> </xsl:text>
          <xsl:call-template name="render-uri">
            <xsl:with-param name="uri" select="@rdf:about"/>
          </xsl:call-template>
        </xsl:if>
        <xsl:if test="@rdf:ID">
          <br/><strong>ID:</strong><xsl:text> </xsl:text><code><xsl:value-of select="@rdf:ID"/></code>
        </xsl:if>
        <xsl:if test="@rdf:nodeID">
          <br/><strong>Blank Node:</strong><xsl:text> </xsl:text><code><xsl:value-of select="@rdf:nodeID"/></code>
        </xsl:if>
      </p>

      <xsl:if test="* or @*[namespace-uri() != 'http://www.w3.org/1999/02/22-rdf-syntax-ns#']">
        <table>
          <tbody>
            <!-- RDF/XML erlaubt Properties auch als Attribute des Knotenelements. -->
            <xsl:for-each select="@*[namespace-uri() != 'http://www.w3.org/1999/02/22-rdf-syntax-ns#']">
              <tr>
                <th scope="row"><code><xsl:value-of select="name()"/></code></th>
                <td><xsl:value-of select="."/></td>
              </tr>
            </xsl:for-each>
            <xsl:apply-templates select="*" mode="property"/>
          </tbody>
        </table>
      </xsl:if>
    </section>
  </xsl:template>

  <!-- Eine RDF-Property und ihr Wert -->
  <xsl:template match="*" mode="property">
    <tr>
      <th scope="row">
        <code><xsl:value-of select="name()"/></code>
      </th>
      <td>
        <xsl:choose>
          <xsl:when test="@rdf:resource">
            <xsl:call-template name="render-uri">
              <xsl:with-param name="uri" select="@rdf:resource"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:when test="@rdf:nodeID">
            <span>Blank Node: </span><code><xsl:value-of select="@rdf:nodeID"/></code>
          </xsl:when>
          <xsl:when test="@rdf:parseType = 'Resource'">
            <div class="nested">
              <table>
                <tbody><xsl:apply-templates select="*" mode="property"/></tbody>
              </table>
            </div>
          </xsl:when>
          <xsl:when test="*">
            <div class="nested">
              <xsl:apply-templates select="*" mode="resource">
                <xsl:with-param name="level" select="3"/>
              </xsl:apply-templates>
            </div>
          </xsl:when>
          <xsl:when test="starts-with(normalize-space(.), 'http://') or starts-with(normalize-space(.), 'https://')">
            <xsl:call-template name="render-uri">
              <xsl:with-param name="uri" select="normalize-space(.)"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:when test="normalize-space(.)">
            <xsl:value-of select="."/>
          </xsl:when>
          <xsl:otherwise>—</xsl:otherwise>
        </xsl:choose>

        <xsl:if test="@xml:lang">
          <xsl:text> [Sprache: </xsl:text><xsl:value-of select="@xml:lang"/><xsl:text>]</xsl:text>
        </xsl:if>
        <xsl:if test="@rdf:datatype">
          <xsl:text> [Datentyp: </xsl:text><xsl:value-of select="@rdf:datatype"/><xsl:text>]</xsl:text>
        </xsl:if>
        <xsl:if test="@rdf:ID">
          <xsl:text> [Aussagen-ID: </xsl:text><xsl:value-of select="@rdf:ID"/><xsl:text>]</xsl:text>
        </xsl:if>
      </td>
    </tr>
  </xsl:template>

  <!--
    Nur geprüfte Resolver werden als aktive Links ausgegeben.
    Alle anderen URIs bleiben sichtbar, besitzen aber kein href.
  -->
  <xsl:template name="render-uri">
    <xsl:param name="uri"/>
    <xsl:variable name="value" select="normalize-space($uri)"/>

    <xsl:choose>
      <!-- TextGrid-URI ohne oder mit Revision: Repository-Browser verwenden. -->
      <xsl:when test="starts-with($value, 'http://textgridrep.de/')">
        <a href="{concat('https://textgridrep.org/browse/', substring-after($value, 'http://textgridrep.de/'))}"
           target="_blank" rel="noopener noreferrer"><xsl:value-of select="$value"/></a>
      </xsl:when>
      <xsl:when test="starts-with($value, 'https://textgridrep.de/')">
        <a href="{concat('https://textgridrep.org/browse/', substring-after($value, 'https://textgridrep.de/'))}"
           target="_blank" rel="noopener noreferrer"><xsl:value-of select="$value"/></a>
      </xsl:when>
      <xsl:when test="starts-with($value, 'textgrid:')">
        <a href="{concat('https://textgridrep.org/browse/', substring-after($value, 'textgrid:'))}"
           target="_blank" rel="noopener noreferrer"><xsl:value-of select="$value"/></a>
      </xsl:when>
      <xsl:when test="starts-with($value, 'https://textgridrep.org/browse/')">
        <a href="{$value}" target="_blank" rel="noopener noreferrer"><xsl:value-of select="$value"/></a>
      </xsl:when>

      <!-- Persistente TextGrid-Handles. -->
      <xsl:when test="starts-with($value, 'https://hdl.handle.net/21.11113/')">
        <a href="{$value}" target="_blank" rel="noopener noreferrer"><xsl:value-of select="$value"/></a>
      </xsl:when>

      <!-- Zotero-Verweise sind im Projekt funktionsfähig. -->
      <xsl:when test="starts-with($value, 'https://www.zotero.org/') or starts-with($value, 'https://zotero.org/')">
        <a href="{$value}" target="_blank" rel="noopener noreferrer"><xsl:value-of select="$value"/></a>
      </xsl:when>

      <!-- Unbekannte oder nicht auflösbare URIs nur anzeigen. -->
      <xsl:otherwise>
        <span title="URI nicht als Link aktiviert"><xsl:value-of select="$value"/></span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Verständlicher Titel, ohne Kenntnis eines bestimmten Schemas. -->
  <xsl:template name="resource-title">
    <xsl:choose>
      <xsl:when test="*[local-name() = 'preferredTitle' or local-name() = 'title' or local-name() = 'label' or local-name() = 'name'][normalize-space(.) and not(*)]">
        <xsl:value-of select="*[local-name() = 'preferredTitle' or local-name() = 'title' or local-name() = 'label' or local-name() = 'name'][normalize-space(.) and not(*)][1]"/>
      </xsl:when>
      <xsl:when test="*[contains(translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'title') and not(contains(translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'type'))][normalize-space(.) and not(*)]">
        <xsl:value-of select="*[contains(translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'title') and not(contains(translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'type'))][normalize-space(.) and not(*)][1]"/>
      </xsl:when>
      <xsl:when test="*[local-name() = 'identifier'][normalize-space(.) and not(*)]">
        <xsl:value-of select="*[local-name() = 'identifier'][normalize-space(.) and not(*)][1]"/>
      </xsl:when>
      <xsl:when test="local-name() != 'Description'">
        <xsl:value-of select="name()"/>
      </xsl:when>
      <xsl:when test="rdf:type/@rdf:resource">
        <xsl:value-of select="rdf:type[1]/@rdf:resource"/>
      </xsl:when>
      <xsl:otherwise>RDF-Ressource</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
