<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
                xmlns:dtm="http://syntext.com/Extensions/DocumentTypeMetadata-1.0"
                extension-element-prefixes="dtm"
                exclude-result-prefixes="doc"
                version='1.0'>

<dtm:doc dtm:idref="all.title-markup"/>
<xsl:template match="*" mode="title.markup" dtm:id="title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:param name="verbose" select="1"/>

  <xsl:choose>
    <xsl:when test="title">
      <xsl:apply-templates select="title[1]" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:when test="self::partintro">
      <!-- partintro's don't have titles, use the parent (part or reference)
           title instead. -->
      <xsl:apply-templates select="parent::*" mode="title.markup"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>???TITLE???</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="title.title-markup"/>
<xsl:template match="title" mode="title.markup" dtm:id="title.title-markup">
  <xsl:param name="allow-anchors" select="0"/>

  <xsl:choose>
    <xsl:when test="$allow-anchors != 0">
      <xsl:apply-templates/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates mode="no.anchor.mode"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="set.title-markup"/>
<xsl:template match="set" mode="title.markup" dtm:id="set.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:apply-templates select="(setinfo/title|title)[1]"
                       mode="title.markup">
    <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  </xsl:apply-templates>
</xsl:template>

<dtm:doc dtm:idref="book.title-markup"/>
<xsl:template match="book" mode="title.markup" dtm:id="book.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:apply-templates select="(bookinfo/title|title)[1]"
                       mode="title.markup">
    <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  </xsl:apply-templates>
</xsl:template>

<dtm:doc dtm:idref="part.title-markup"/>
<xsl:template match="part" mode="title.markup" dtm:id="part.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:apply-templates select="((partinfo|docinfo)/title|title)[1]"
                       mode="title.markup">
    <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  </xsl:apply-templates>
</xsl:template>

<dtm:doc dtm:idref="pca.title-markup"/>
<xsl:template match="preface|chapter|appendix" mode="title.markup" dtm:id="pca.title-markup">
  <xsl:param name="allow-anchors" select="0"/>

  <xsl:variable name="title" select="((docinfo|prefaceinfo
                                      |chapterinfo|appendixinfo)/title
                                      |title)[1]"/>
  <xsl:apply-templates select="$title" mode="title.markup">
    <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  </xsl:apply-templates>
</xsl:template>

<dtm:doc dtm:idref="dedication.title-markup"/>
<xsl:template match="dedication" mode="title.markup" dtm:id="dedication.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:choose>
    <xsl:when test="title">
      <xsl:apply-templates select="title" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'Dedication'"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="colophon.title-markup"/>
<xsl:template match="colophon" mode="title.markup" dtm:id="colophon.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:choose>
    <xsl:when test="title">
      <xsl:apply-templates select="title" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'Colophon'"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="article.title-markup"/>
<xsl:template match="article" mode="title.markup" dtm:id="article.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:variable name="title" select="((artheader|articleinfo)/title
                                      |title)[1]"/>

  <xsl:apply-templates select="$title" mode="title.markup">
    <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  </xsl:apply-templates>
</xsl:template>

<dtm:doc dtm:idref="reference.title-markup"/>
<xsl:template match="reference" mode="title.markup" dtm:id="reference.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:apply-templates select="((referenceinfo|docinfo)/title|title)[1]"
                       mode="title.markup">
    <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  </xsl:apply-templates>
</xsl:template>

<dtm:doc dtm:idref="refentry.title-markup"/>
<xsl:template match="refentry" mode="title.markup" dtm:id="refentry.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:variable name="refmeta" select=".//refmeta"/>
  <xsl:variable name="refentrytitle" select="$refmeta//refentrytitle"/>
  <xsl:variable name="refnamediv" select=".//refnamediv"/>
  <xsl:variable name="refname" select="$refnamediv//refname"/>

  <xsl:variable name="title">
    <xsl:choose>
      <xsl:when test="$refentrytitle">
        <xsl:apply-templates select="$refentrytitle[1]" mode="title.markup"/>
      </xsl:when>
      <xsl:when test="$refname">
        <xsl:apply-templates select="$refname[1]" mode="title.markup"/>
      </xsl:when>
      <xsl:otherwise>REFENTRY WITHOUT TITLE???</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:copy-of select="$title"/>
</xsl:template>

<dtm:doc dtm:idref="rr.title-markup"/>
<xsl:template match="refentrytitle|refname" mode="title.markup" dtm:id="rr.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:choose>
    <xsl:when test="$allow-anchors != 0">
      <xsl:apply-templates/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates mode="no.anchor.mode"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="sections.title-markup"/>
<xsl:template match="section
                     |sect1|sect2|sect3|sect4|sect5
                     |refsect1|refsect2|refsect3
                     |simplesect"
              mode="title.markup" dtm:id="sections.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:variable name="title" select="((sectioninfo|sect1info
                                      |sect2info|sect3info
                                      |sect4info|sect5info
                                      |refsect1info|refsect2info
                                      |refsect3info)/title
                                      |title)[1]"/>

  <xsl:apply-templates select="$title" mode="title.markup">
    <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  </xsl:apply-templates>
</xsl:template>

<dtm:doc dtm:idref="bridgehead.title-markup"/>
<xsl:template match="bridgehead" mode="title.markup" dtm:id="bridgehead.title-markup">
  <xsl:apply-templates mode="title.markup"/>
</xsl:template>

<dtm:doc dtm:idref="refsynopsisdiv.title-markup"/>
<xsl:template match="refsynopsisdiv" mode="title.markup" dtm:id="refsynopsisdiv.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:choose>
    <xsl:when test="title">
      <xsl:apply-templates select="title" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'RefSynopsisDiv'"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="bibliography.title-markup"/>
<xsl:template match="bibliography" mode="title.markup" dtm:id="bibliography.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:variable name="title" select="(bibliographyinfo/title|title)[1]"/>
  <xsl:choose>
    <xsl:when test="$title">
      <xsl:apply-templates select="$title" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'Bibliography'"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="index.title-markup"/>
<xsl:template match="index" mode="title.markup" dtm:id="index.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:variable name="title" select="(indexinfo/title|title)[1]"/>
  <xsl:choose>
    <xsl:when test="$title">
      <xsl:apply-templates select="$title" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'Index'"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="setindex.title-markup"/>
<xsl:template match="setindex" mode="title.markup" dtm:id="setindex.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:variable name="title" select="(setindexinfo/title|title)[1]"/>
  <xsl:choose>
    <xsl:when test="$title">
      <xsl:apply-templates select="$title" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'SetIndex'"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="ftee.title-markup"/>
<xsl:template match="figure|table|example|equation" mode="title.markup" dtm:id="ftee.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:apply-templates select="title" mode="title.markup">
    <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  </xsl:apply-templates>
</xsl:template>

<dtm:doc dtm:idref="procedure.title-markup"/>
<xsl:template match="procedure" mode="title.markup" dtm:id="procedure.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:apply-templates select="title" mode="title.markup">
    <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
  </xsl:apply-templates>
</xsl:template>

<dtm:doc dtm:idref="abstract.title-markup"/>
<xsl:template match="abstract" mode="title.markup" dtm:id="abstract.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:choose>
    <xsl:when test="title">
      <xsl:apply-templates select="title" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'Abstract'"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="ctwin.title-markup"/>
<xsl:template match="caution|tip|warning|important|note" mode="title.markup" dtm:idref="ctwin.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:variable name="title" select="title[1]"/>
  <xsl:choose>
    <xsl:when test="$title">
      <xsl:apply-templates select="$title" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="gentext">
        <xsl:with-param name="key">
          <xsl:choose>
            <xsl:when test="self::note">Note</xsl:when>
            <xsl:when test="self::important">Important</xsl:when>
            <xsl:when test="self::caution">Caution</xsl:when>
            <xsl:when test="self::warning">Warning</xsl:when>
            <xsl:when test="self::tip">Tip</xsl:when>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="question.title-markup"/>
<xsl:template match="question" mode="title.markup" dtm:id="question.title-markup">
  <!-- questions don't have titles -->
  <xsl:call-template name="gentext">
    <xsl:with-param name="key" select="'question'"/>
  </xsl:call-template>
</xsl:template>

<dtm:doc dtm:idref="answer.title-markup"/>
<xsl:template match="answer" mode="title.markup" dtm:id="answer.title-markup">
  <!-- answers don't have titles -->
  <xsl:call-template name="gentext">
    <xsl:with-param name="key" select="'answer'"/>
  </xsl:call-template>
</xsl:template>

<dtm:doc dtm:idref="qandaentry.title-markup"/>
<xsl:template match="qandaentry" mode="title.markup" dtm:id="qandaentry.title-markup">
  <!-- qandaentrys are represented by the first question in them -->
  <xsl:call-template name="gentext">
    <xsl:with-param name="key" select="'question'"/>
  </xsl:call-template>
</xsl:template>

<dtm:doc dtm:idref="legalnotice.title-markup"/>
<xsl:template match="legalnotice" mode="title.markup" dtm:id="legalnotice.title-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:choose>
    <xsl:when test="title">
      <xsl:apply-templates select="title" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="gentext">
        <xsl:with-param name="key" select="'LegalNotice'"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- ============================================================ -->
<dtm:doc dtm:idref="all.titleabbrev-markup"/>
<xsl:template match="*" mode="titleabbrev.markup" dtm:id="all.titleabbrev-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:param name="verbose" select="1"/>

  <xsl:choose>
    <xsl:when test="titleabbrev">
      <xsl:apply-templates select="titleabbrev[1]" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="." mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
        <xsl:with-param name="verbose" select="$verbose"/>
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="pca.titleabbrev-markup"/>
<xsl:template match="preface|chapter|appendix" mode="titleabbrev.markup" dtm:id="pca.titleabbrev-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:param name="verbose" select="1"/>

  <xsl:variable name="titleabbrev" select="((docinfo|prefaceinfo
                                           |chapterinfo
                                           |appendixinfo)/titleabbrev
                                           |titleabbrev)[1]"/>

  <xsl:choose>
    <xsl:when test="$titleabbrev">
      <xsl:apply-templates select="$titleabbrev" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="." mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
        <xsl:with-param name="verbose" select="$verbose"/>
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="article.titleabbrev-markup"/>
<xsl:template match="article" mode="titleabbrev.markup" dtm:id="article.titleabbrev-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:param name="verbose" select="1"/>

  <xsl:variable name="titleabbrev" select="((artheader|articleinfo)/titleabbrev
                                           |titleabbrev)[1]"/>

  <xsl:choose>
    <xsl:when test="$titleabbrev">
      <xsl:apply-templates select="$titleabbrev" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="." mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
        <xsl:with-param name="verbose" select="$verbose"/>
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="sections.titleabbrev-markup"/>
<xsl:template match="section
                     |sect1|sect2|sect3|sect4|sect5
                     |refsect1|refsect2|refsect3
                     |simplesect"
              mode="titleabbrev.markup" dtm:id="sections.titleabbrev-markup">
  <xsl:param name="allow-anchors" select="0"/>
  <xsl:param name="verbose" select="1"/>

  <xsl:variable name="titleabbrev" select="((sectioninfo|sect1info
                                      |sect2info|sect3info
                                      |sect4info|sect5info
                                      |refsect1info|refsect2info
                                      |refsect3info)/titleabbrev
                                      |titleabbrev)[1]"/>

  <xsl:choose>
    <xsl:when test="$titleabbrev">
      <xsl:apply-templates select="$titleabbrev" mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
      </xsl:apply-templates>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="." mode="title.markup">
        <xsl:with-param name="allow-anchors" select="$allow-anchors"/>
        <xsl:with-param name="verbose" select="$verbose"/>
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<dtm:doc dtm:idref="titleabbrev.title-markup"/>
<xsl:template match="titleabbrev" mode="title.markup" dtm:id="titleabbrev.title-markup">
  <xsl:param name="allow-anchors" select="0"/>

  <xsl:choose>
    <xsl:when test="$allow-anchors != 0">
      <xsl:apply-templates/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates mode="no.anchor.mode"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- ============================================================ -->
<dtm:doc dtm:idref="all.no-anchor-mode"/>
<xsl:template match="*" mode="no.anchor.mode" dtm:id="all.no-anchor-mode">
  <xsl:apply-templates mode="no.anchor.mode"/>
</xsl:template>

<dtm:doc dtm:idref="footnote.no-anchor-mode"/>
<xsl:template match="footnote" mode="no.anchor.mode" dtm:id="footnote.no-anchor-mode">
  <!-- nop, suppressed -->
</xsl:template>

<dtm:doc dtm:idref="anchor.no-anchor-mode"/>
<xsl:template match="anchor" mode="no.anchor.mode" dtm:id="anchor.no-anchor-mode">
  <!-- nop, suppressed -->
</xsl:template>

<dtm:doc dtm:idref="ulink.no-anchor-mode"/>
<xsl:template match="ulink" mode="no.anchor.mode" dtm:id="ulink.no-anchor-mode">
  <xsl:apply-templates/>
</xsl:template>

<dtm:doc dtm:idref="link.no-anchor-mode"/>
<xsl:template match="link" mode="no.anchor.mode" dtm:id="link.no-anchor-mode">
  <xsl:apply-templates/>
</xsl:template>

<dtm:doc dtm:idref="olink.no-anchor-mode"/>
<xsl:template match="olink" mode="no.anchor.mode" dtm:id="olink.no-anchor-mode">
  <xsl:apply-templates/>
</xsl:template>

<dtm:doc dtm:idref="indexterm.no-anchor-mode"/>
<xsl:template match="indexterm" mode="no.anchor.mode" dtm:id="indexterm.no-anchor-mode">
  <!-- nop, suppressed -->
</xsl:template>

<dtm:doc dtm:idref="xref.no-anchor-mode"/>
<xsl:template match="xref" mode="no.anchor.mode" dtm:id="xref.no-anchor-mode">
  <!-- FIXME: this should generate the text without the link... -->
</xsl:template>

</xsl:stylesheet>

