<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:name="http://namesforlife.com/ns/name"
  xmlns:taxon="http://namesforlife.com/ns/taxon"
  xmlns:exemplar="http://namesforlife.com/ns/exemplar"
  xmlns:reference="http://namesforlife.com/ns/reference"
  xmlns:common="http://namesforlife.com/ns/common"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  exclude-result-prefixes="name taxon exemplar reference common"
  >

  <xsl:include href="common.xsl" />

  <xsl:output
    method="html"
    standalone="yes"
    encoding="iso-8859-1"
    omit-xml-declaration="yes"
    cdata-section-elements="pre script style"
    indent="yes"
    media-type="text/html"
  />

  <!-- Main Entry Point -->
  <xsl:template match="/taxon:taxon">
    <xsl:apply-templates select="." mode="anchor" />
  </xsl:template>

  <xsl:template match="taxon:ancestor" mode="table-row">
    <tr>
      <td>
        <xsl:attribute name="class"><xsl:value-of select="@rank" /></xsl:attribute>
        <xsl:apply-templates select="." mode="taxon-rank-link" />
      </td>
      <td><xsl:apply-templates select="." mode="complete-link" /></td>
    </tr>
    <xsl:apply-templates select="taxon:ancestor" mode="table-row" />
  </xsl:template>

  <xsl:template match="exemplar:exemplars" mode="table-row">
    <tr>
      <td>
        <xsl:apply-templates select="exemplar:exemplar" mode="exemplar-link" />
      </td>
      <td></td>
    </tr>
  </xsl:template>

  <xsl:template match="exemplar:exemplar" mode="exemplar-link">
    <a>
      <xsl:attribute name="title">NamesforLife Exemplar</xsl:attribute>
      <xsl:attribute name="href"><xsl:text>https://doi.org/</xsl:text><xsl:value-of select="@doi" /></xsl:attribute>
      <xsl:attribute name="class">doi</xsl:attribute>
      <xsl:value-of select="@doi" />
    </a>
  </xsl:template>

  <!-- Anchor -->
  <xsl:template match="taxon:taxon" mode="anchor">
    <xsl:apply-templates select="." mode="monograph" />
  </xsl:template>

  <!-- Monograph -->
  <xsl:template match="taxon:taxon" mode="monograph">
    <table class="anchor">
      <caption></caption>
      <thead></thead>
      <tbody>
        <tr>
          <td>
            <table class="monograph">
              <caption></caption>
              <thead></thead>
              <tbody>
                <xsl:apply-templates select="." mode="table-row-doi" />
                <xsl:apply-templates select="." mode="table-row-name" />
                <xsl:apply-templates select="." mode="table-row-rank" />
                <xsl:apply-templates select="." mode="table-row-methodology" />
<!--
                <xsl:apply-templates select="." mode="table-row-parent-name" />
                <xsl:apply-templates select="." mode="table-row-parent-taxon" />
-->

                <xsl:call-template name="table-row-empty" />

                <xsl:apply-templates select="." mode="table-row-parent" />

                <xsl:if test="taxon:child or not (@rank='species' or @rank='subspecies')">
                  <xsl:apply-templates select="." mode="table-row-members" />
                </xsl:if>
                <xsl:if test="@rank='species' or @rank='subspecies'">
                  <xsl:apply-templates select="exemplar:exemplars" mode="table-row-type-exemplar" />
                </xsl:if>
              </tbody>
              <tfoot></tfoot>
            </table>
          </td>
        </tr>
      </tbody>
      <tfoot></tfoot>
    </table>
  </xsl:template>

  <!-- Taxon DOI -->
  <xsl:template match="taxon:taxon" mode="table-row-doi">
    <tr class="doi">
      <td><div>Taxon DOI</div></td>
      <td><xsl:value-of select="@doi" /></td>
    </tr>
  </xsl:template>

  <!-- Name -->
  <xsl:template match="taxon:taxon" mode="table-row-name">
    <tr>
      <td>
        <div>
          <xsl:text>Name</xsl:text>
          <xsl:if test="count(common:nomenclature/common:name-status-group) &gt; 1">s</xsl:if>
        </div>
      </td>
      <td itemscope="itemscope" itemtype="http://schema.org/about">
        <xsl:for-each select="common:nomenclature/common:name-status-group">
          <xsl:if test="position() != 1">, </xsl:if>
          <xsl:apply-templates select="." mode="complete-link" />
        </xsl:for-each>
      </td>
    </tr>
  </xsl:template>

  <!-- Rank -->
  <xsl:template match="taxon:taxon" mode="table-row-rank">
    <tr>
      <td><div>Taxonomic Rank</div></td>
      <td><xsl:apply-templates select="@rank" /></td>
    </tr>
  </xsl:template>

  <!-- Methodology -->
  <xsl:template match="taxon:taxon" mode="table-row-methodology">
    <tr>
      <td><div>Methodology</div></td>
      <td><div>Nomenclatural Taxonomy [1980-2022]</div><div><i>Incertae sedis</i> taxa are placed using <a target="_blank" href="https://www.google.com/patents/US8036997">SOSCC</a> (Garrity and Lilburn 2008)</div></td>
    </tr>
  </xsl:template>

  <!-- Parent Name -->
  <xsl:template match="taxon:taxon" mode="table-row-parent-name">
    <tr>
      <td><div>Parent Name</div></td>
      <td><xsl:apply-templates select="taxon:parent" mode="complete-link" /></td>
    </tr>
  </xsl:template>

  <!-- Parent Taxon -->
  <xsl:template match="taxon:taxon" mode="table-row-parent-taxon">
    <tr>
      <td><div>Parent Taxon</div></td>
      <td><xsl:apply-templates select="taxon:parent" mode="taxon-doi-link" /></td>
    </tr>
  </xsl:template>

  <!-- Parent -->
  <xsl:template match="taxon:taxon" mode="table-row-parent">
    <xsl:if test="@rank!='domain'">
      <tr>
        <td><div>Parent</div></td>
        <td>
          <table>
            <thead>
              <tr>
                <th>Taxon Record</th>
                <th>Name Record</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td><xsl:apply-templates select="taxon:parent" mode="taxon-rank-link" /></td>
                <td style="padding-left: 3em;"><xsl:apply-templates select="taxon:parent" mode="complete-link" /></td>
              </tr>
            </tbody>
            <tfoot></tfoot>
          </table>
        </td>
      </tr>
    </xsl:if>
  </xsl:template>

  <!-- Members -->
  <xsl:template match="taxon:taxon" mode="table-row-members">
    <tr>
      <td><div>Members</div></td>
      <td>
        <table>
          <thead>
            <xsl:if test="@rank='domain'">
              <tr>
                <th>Taxon</th>
                <th>Name</th>
              </tr>
            </xsl:if>
          </thead>
          <tbody>
            <xsl:apply-templates select="taxon:child" mode="table-row-members">
              <xsl:sort select="contains(common:nomenclature/common:name-status-group/common:identifier, 'sp.')" order="ascending" />
              <xsl:sort select="@doi=../common:nomenclatural-type/common:name-status-group/common:identifier/@taxon-doi" order="descending" />
              <xsl:sort select="common:nomenclature/common:name-status-group/common:identifier" order="ascending" />
            </xsl:apply-templates>
          </tbody>
          <tfoot></tfoot>
        </table>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="taxon:child" mode="table-row-members">
    <tr>
      <td>
        <xsl:apply-templates select="." mode="taxon-rank-link" />
      </td>
      <td style="padding-left: 3em;">
        <xsl:apply-templates select="." mode="complete-link" />
      </td>
    </tr>
  </xsl:template>

  <!-- Type Exemplar -->
  <xsl:template match="exemplar:exemplars" mode="table-row-type-exemplar">
    <tr itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb">
      <td><div itemprop="title">Type Strain</div><div>representative organism</div></td>
      <td><xsl:apply-templates select="exemplar:exemplar[@doi=../../common:nomenclatural-type/@nomenclatural-type-exemplar]/exemplar:identifiers" mode="strain-equivalence-links" /></td>
    </tr>
  </xsl:template>

</xsl:stylesheet>
