<?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="/exemplar:exemplar">
    <xsl:apply-templates select="." mode="anchor" />
  </xsl:template>

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

  <!-- Monograph -->
  <xsl:template match="exemplar:exemplar" 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:call-template name="table-row-empty" />
                <xsl:apply-templates select="exemplar:identifiers" mode="monograph-entry" />
                <xsl:call-template name="table-row-empty" />
                <xsl:apply-templates select="common:nomenclature" mode="table-row-name" />
              </tbody>
              <tfoot></tfoot>
            </table>
          </td>
        </tr>
      </tbody>
      <tfoot></tfoot>
    </table>
  </xsl:template>

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

  <!-- Culture Collection Accession Number -->
  <xsl:template match="exemplar:identifiers" mode="monograph-entry">
    <tr class="first">
      <td>
        <div>Strain Identifiers</div>
      </td>
      <td><xsl:apply-templates select="." mode="strain-equivalencies" /></td>
    </tr>
    <xsl:for-each select="exemplar:identifier">
      <!-- sort by lab-id flag (doesn't exist yet) -->
      <xsl:apply-templates select="." mode="monograph-entry">
        <xsl:with-param name="class">
          <xsl:choose>
            <xsl:when test="position()=last()">last</xsl:when>
            <xsl:otherwise>normal</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:for-each>
  </xsl:template>

  <!-- Culture Collection Accession Number -->
  <xsl:template match="exemplar:identifier" mode="monograph-entry">
    <xsl:param name="class" />
    <tr>
      <xsl:attribute name="class"><xsl:value-of select="$class" /></xsl:attribute>
      <td>
        <div></div>
        <div><xsl:apply-templates select="." mode="organization" /></div>
      </td>
      <td><xsl:apply-templates select="." mode="format" /></td>
    </tr>
  </xsl:template>

  <!-- TODO: Move this into business logic in the XML export. Need a database first though. -->
  <xsl:template match="exemplar:identifier/exemplar:organization">
<!--
    <xsl:choose>
      <xsl:when test="starts-with(@id, 'ATCC')"><xsl:text>American Type Culture Collection</xsl:text><xsl:call-template name="atcc-footnote" /></xsl:when>
      <xsl:when test="starts-with(@id, 'DSM')"><dfn title="German Collection of Microorganisms and Cell Cultures">Deutsche Sammlung von Mikroorganismen und Zellkulturen</dfn></xsl:when>
      <xsl:when test="starts-with(@id, 'NCTC')">National Collection of Type Cultures</xsl:when>
      <xsl:when test="starts-with(@id, 'JCM')">Japan Collection of Microorganisms</xsl:when>
      <xsl:otherwise></xsl:otherwise>
    </xsl:choose>
-->
  </xsl:template>

  <xsl:template match="exemplar:identifier" mode="format">
    <xsl:value-of select="@label" />
  </xsl:template>

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

  <xsl:template name="atcc-footnote">
    <sup>&#8224;</sup>
  </xsl:template>

</xsl:stylesheet>
