<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/pool/xslt_ht.xslt" type="application/xml"?>
<xsl:stylesheet
  xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
  xmlns:d = "http://herbaer.de/xmlns/20051201/doc"
  xmlns:is = "http://herbaer.de/xmlns/20080705/imgshow"
  xmlns:ht = "http://www.w3.org/1999/xhtml"
  exclude-result-prefixes = "d ht"
  version = "1.0"
  xml:lang = "de"
>

<xsl:import href="/kal/s/kal.xslt"/>

<d:info xmlns="http://herbaer.de/xmlns/20051201/doc">
  <title>kal.xslt</title>
  <subtitle>Bildgeschichte als Kalender</subtitle>
  <date>2016-02-03</date>
  <author>
    <personname>
      <firstname>Herbert</firstname>
      <surname>Schiemann</surname>
    </personname>
    <email>h.schiemann@herbaer.de</email>
  </author>
</d:info>

<d:para role="stylesheet">
Diese Datei definiert die Auswahl der Bilddateien für die Kalenderansicht.
Die XHTML-Darstellung (für Drucker und Bildschirm)
wird durch die eingebundene Datei aus dem &#x201e;Kalender&#x201d;-Bereich definiert.
Die Datei <d:filename>kal.stub</d:filename>
verknüpft eine Bildergeschichte mit dieser Bildkalender-Ansicht.
</d:para>

<d:para>Verweis zur Startseite</d:para>
<xsl:variable name="g_home" select="'../'"/>

<d:para>Wurzelelement</d:para>
<xsl:template match="is:document">
  <xsl:variable name="f">
    <xsl:choose>
      <xsl:when test="    count (//is:jpg [contains ('pq', @fmt)])         &gt; count
      (//is:jpg [contains
      ('hk', @fmt)])">pq</xsl:when>
      <xsl:otherwise>hk</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="c" select="count (is:section [.//is:jpg [contains ($f, @fmt)]])"/>
  <xsl:variable name="s">
    <xsl:choose>
      <xsl:when test="$c &lt; 14">1</xsl:when>
      <xsl:when test="$c mod 7 = 0">11</xsl:when>
      <xsl:otherwise>7</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:call-template name="kalender">
    <xsl:with-param name="data">
      <xsl:apply-templates select="is:title"/>
      <xsl:text>||</xsl:text>
      <xsl:apply-templates select="(is:section//is:jpg [contains($f, @fmt)])[1]">
        <xsl:with-param name="m" select="1"/>
        <xsl:with-param name="s" select="$s"/>
        <xsl:with-param name="c" select="$c"/>
        <xsl:with-param name="f" select="$f"/>
      </xsl:apply-templates>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<d:para>
Bild als Monatsbild auswählen
</d:para>
<xsl:template match="is:jpg">
  <!-- Nummer des Monats -->
  <xsl:param name="m" select="1"/>
  <!--
      Im Fall $s = 1
      die Position des Bildes innerhalb des Abschnitts,
      sonst
      die Position des Abschnitts unter den Bildabschnitten
  -->
  <xsl:param name="p" select="1"/>
  <!-- Abschnitt-Schrittweite -->
  <xsl:param name="s" select="1"/>
  <!-- Die Anzahl der Bild-Abschnitte im Falle $s > 1 -->
  <xsl:param name="c"/>
  <!-- Bildformat: hoch (hk) oder quer (pq) -->
  <xsl:param name="f" select="'hk'"/>
  <xsl:if test="contains ($f, @fmt)">
    <xsl:text>#s</xsl:text>
    <xsl:value-of select="count (preceding::is:section) + 1"/>
    <xsl:value-of select="concat ('_', @src, '_')"/>
    <xsl:value-of select="count (preceding::is:jpg) - count (ancestor::is:section[1]/preceding::is:jpg)"
    />
    <xsl:value-of select="concat ('|', @src, '||')"/>
  </xsl:if>
  <xsl:if test="$m &lt; 12">
    <xsl:choose>
      <xsl:when test="$s &gt; 1">
        <xsl:variable name="np" select="($p + $s - 1) mod $c + 1"/>
        <xsl:apply-templates
          select = "
            (/is:document/is:section [.//is:jpg [contains ($f, @fmt)]][$np]
            //is:jpg[contains ($f, @fmt)])[1]
          "
        >
          <xsl:with-param name="s" select="$s"/>
          <xsl:with-param name="p" select="$np"/>
          <xsl:with-param name="m" select="$m + 1"/>
          <xsl:with-param name="c" select="$c"/>
          <xsl:with-param name="f" select="$f"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:when test="following::is:section//is:jpg [contains ($f, @fmt)][position() = $p]"
      >
        <xsl:apply-templates select="(following::is:section//is:jpg [contains ($f, @fmt)][position() = $p])[1]"
        >
          <xsl:with-param name="m" select="$m + 1"/>
          <xsl:with-param name="p" select="$p"/>
          <xsl:with-param name="f" select="$f"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:when test="//is:section//is:jpg [contains ($f, @fmt)][position() = $p + 1]">
        <xsl:apply-templates select="(//is:section//is:jpg [contains ($f, @fmt)][position() = $p + 1])[1]"
        >
          <xsl:with-param name="m" select="$m + 1"/>
          <xsl:with-param name="p" select="$p + 1"/>
          <xsl:with-param name="f" select="$f"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="(//is:jpg [contains ($f, @fmt)]) [1]">
          <xsl:with-param name="m" select="$m + 1"/>
          <xsl:with-param name="f" select="$f"/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>
</xsl:template>

<d:para>
Der Titel
</d:para>
<xsl:template match="is:title">
  <xsl:choose>
    <xsl:when test="contains (., '(')">
      <xsl:value-of select="normalize-space (substring-before (., '('))"/>
    </xsl:when>
    <xsl:when test="contains (., '&#xff08;')">
      <xsl:value-of select="normalize-space (substring-before (., '&#xff08;'))"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
