<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="4.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:map="http://www.w3.org/2005/xpath-functions/map"
  xmlns:math="http://www.w3.org/2005/xpath-functions/math"
  xmlns:array="http://www.w3.org/2005/xpath-functions/array"
  xmlns:tm="http://www.datenverdrahten.de/tm"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="#all"
  expand-text="yes">

  <!-- by T. Meinike 11/22 -->

  <xsl:output method="xml" version="1.0" html-version="5" encoding="UTF-8"
    indent="yes" include-content-type="no" omit-xml-declaration="yes"/>

  <xsl:function name="tm:termine" as="xs:string*">
    <xsl:param name="jahr" as="xs:integer"/>

    <xsl:variable name="monate" select="'01', '02', '03', '04',  '05',  '06',  '07',  '08',  '09',  '10', '11', '12'"/>

    <xsl:sequence>
      <xsl:for-each select="$monate">
        <xsl:variable name="i" select="fn:position()"/>

        <xsl:variable name="datum" select="xs:date(fn:concat($jahr, '-', $monate[$i], '-01'))"/>
        <xsl:variable name="tag" select="fn:format-date($datum, '[F]')"/>

        <xsl:variable name="mi1" as="xs:integer">
          <xsl:choose>
            <xsl:when test="$tag = 'Mittwoch'" select="1"/>
            <xsl:when test="$tag = 'Dienstag'" select="2"/>
            <xsl:when test="$tag = 'Montag'" select="3"/>
            <xsl:when test="$tag = 'Sonntag'" select="4"/>
            <xsl:when test="$tag = 'Samstag'" select="5"/>
            <xsl:when test="$tag = 'Freitag'" select="6"/>
            <xsl:when test="$tag = 'Donnerstag'" select="7"/>
            <!-- ggf. <xsl:otherwise select="..."/> -->
          </xsl:choose>
        </xsl:variable>

        <xsl:variable name="mi3" select="$mi1 + 14"/>
        <xsl:value-of select="$mi3 || '.' || $monate[$i] || '.' || $jahr"/>
      </xsl:for-each>
    </xsl:sequence>
  </xsl:function>

  <xsl:template name="xsl:initial-template">

    <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE html>&#xA;</xsl:text>
    <html lang="de">
      <head>
        <meta charset="UTF-8"/>
        <title>Termine</title>
        <style>* {{ font-family: sans-serif; }} p,li {{ font-size: 1.5em; }} ol {{ padding-left: 2.5em; }} ol li::marker {{ color: #999; }} li {{ font-family: monospace; font-weight: bold; }}</style>
      </head>
      <body>

        <h1>Termine 2022</h1>
        <p>3. Mittwoch im Monat:</p>

        <ol>
          <xsl:for-each select="tm:termine(2022)">
            <li>{.}</li>
          </xsl:for-each>
        </ol>

      </body>
    </html>

  </xsl:template>

</xsl:stylesheet>