<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output
   method="xml"
   standalone="yes"
   indent="yes"
   doctype-public="-//W3C//DTD XHTML 1.1//EN"
   doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>

  <xsl:template match="para">
    <p><xsl:apply-templates/></p>
  </xsl:template>

  <xsl:template match="programlisting">
    <pre class="listing">
      <xsl:apply-templates/>
    </pre>
  </xsl:template>

  <!-- Simpler template using {} replacement -->
  <!--
  <xsl:template match="mediaobject">
    <img src="{imageobject/imagedata/@fileref}" alt="{imageobject/objectinfo/title}"/>
  </xsl:template>
  -->

  <!-- More complex template using <element> and <value-of> -->
  <xsl:template match="mediaobject">
    <xsl:element name="img">
      <xsl:attribute name="src">
        <xsl:value-of select="imageobject/imagedata/@fileref"/>
      </xsl:attribute>
      <xsl:if test="count(imageobject/objectinfo) > 0">
        <xsl:attribute name="alt">
          <xsl:value-of select="imageobject/objectinfo/title"/>
        </xsl:attribute>
      </xsl:if>
    </xsl:element>
  </xsl:template>

  <!-- Base table template used in both forms -->
  <xsl:template match="table">
    <table>
      <xsl:if test="count(title) > 0">
        <caption><xsl:value-of select="title"/></caption>
      </xsl:if>
      <xsl:apply-templates select="child::*[name() != 'title']"/>
    </table>
  </xsl:template>

  <!-- Verbose table templates -->
  <!--
  <xsl:template match="thead">
    <thead><xsl:apply-templates mode="header"/></thead>
  </xsl:apply-templates>

  <xsl:template match="tbody">
    <thead><xsl:apply-templates/></thead>
  </xsl:apply-templates>

  <xsl:template match="row">
    <tr><xsl:apply-templates/></tr>
  </xsl:template>

  <xsl:template match="row" mode="headed">
    <tr><xsl:apply-templates mode="headed"/></tr>
  </xsl:template>

  <xsl:template match="entry">
    <td><xsl:apply-templates/></td>
  </xsl:template>

  <xsl:template match="entry" mode="headed">
    <th>xsl:apply-templates/></th>
  </xsl:template>
  -->

  <!-- Parametric table templates -->
   <xsl:template match="thead|tbody|tfoot">
     <xsl:apply-templates>
       <xsl:with-param name="tag">
         <xsl:choose>
           <xsl:when test="name() = 'thead'">
             <xsl:text>th</xsl:text> 
           </xsl:when>
           <xsl:otherwise>
             <xsl:text>td</xsl:text>
           </xsl:otherwise>
         </xsl:choose>
       </xsl:with-param>
     </xsl:apply-templates>
   </xsl:template>

   <xsl:template match="row">
     <xsl:param name="tag"/>
     <tr>
       <xsl:for-each select="entry">
         <xsl:element name="{$tag}">
           <xsl:apply-templates/>
         </xsl:element>
       </xsl:for-each>
     </tr>
   </xsl:template>

   <xsl:template match="itemizedlist">
     <ul><xsl:apply-templates/></ul>
   </xsl:template>

   <xsl:template match="listitem">
     <li><xsl:apply-templates/></li>
   </xsl:template>

  <xsl:template match="section">
    <a name="{@id}"/>
    <xsl:element name="div">
      <xsl:attribute name="class">
        <xsl:text>section-</xsl:text>
        <xsl:value-of select="count(ancestor::*)"/>
      </xsl:attribute>
      <xsl:call-template name="section-name"/>
    </xsl:element>
    <xsl:apply-templates select="child::*[name() != 'title']"/>
  </xsl:template>

  <xsl:template name="section-name" match="*" mode="name">
    <xsl:text>Section </xsl:text>
    <xsl:number level="multiple" format="1.1."/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="title"/>
  </xsl:template>

  <xsl:template match="article">
    <html>
      <head>
        <title><xsl:value-of select="title"/></title>
        <link rel="stylesheet" type="text/css" href="document.css"/>
      </head>
      <body>
        <h1><xsl:value-of select="title"/></h1>
        <hr style="width: 45%;"/>
        <xsl:call-template name="toc-outline"/>
        <hr style="width: 45%;"/>
        <xsl:for-each select="section">
          <xsl:apply-templates select="."/>
          <xsl:if test="count(following-sibling::section) > 0">
            <hr style="width: 25%;"/>
          </xsl:if>
        </xsl:for-each>
        <xsl:if test="count(//footnote) > 0">
          <hr style="width: 85%;"/>
          <xsl:apply-templates select="//footnote" mode="expand"/>
        </xsl:if>        
        <xsl:if test="count(articleinfo/legalnotice) > 0">
          <hr style="width: 85%;"/>
          <xsl:apply-templates select="articleinfo/legalnotice"/>
        </xsl:if>
      </body>
    </html>
  </xsl:template>

  <xsl:template name="toc-outline">
    <xsl:if test="count(section) > 0">
      <ul>
        <xsl:apply-templates select="section" mode="outline"/>
      </ul>
    </xsl:if>
  </xsl:template>

  <xsl:template match="section" mode="outline">
    <li>
      <xsl:element name="a">
        <xsl:attribute name="href">
          <xsl:text>#</xsl:text><xsl:value-of select="@id"/>
        </xsl:attribute>
        <xsl:call-template name="section-name"/>
      </xsl:element>
      <xsl:call-template name="toc-outline"/>
    </li>
  </xsl:template>

  <!-- Other templates needed for correct processing -->

  <xsl:template match="ulink">
    <a href="{@url}"><xsl:apply-templates/></a>
  </xsl:template>

  <xsl:template match="email">
    <xsl:element name="a">
      <xsl:attribute name="href">mailto:<xsl:apply-templates/></xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="note">
    <div class="note">
      <span class="note-title"><xsl:apply-templates select="title"/>: </span>
      <span class="note-body">
        <xsl:apply-templates select="child::*[name() != 'title']"/>
      </span>
    </div>
  </xsl:template>

  <xsl:template match="command">
    <code><xsl:apply-templates/></code>
  </xsl:template>

  <xsl:template match="footnote">
    <sup>
      <xsl:text>[</xsl:text>
      <a href="#{@id}"><xsl:number level="any" format="1"/></a>
      <xsl:text>]</xsl:text>
    </sup>
  </xsl:template>

  <xsl:template match="footnoteref">
    <xsl:variable name="linkend">
      <xsl:value-of select="@linkend"/>
    </xsl:variable>
    <xsl:apply-templates select="//footnote[@id = $linkend]"/>
  </xsl:template>

  <xsl:template match="footnote" mode="expand">
    <a name="{@id}"/>
    <p><xsl:number level="any" format="1"/>: <xsl:apply-templates/></p>
  </xsl:template>

  <xsl:template match="xref">
    <xsl:variable name="linkend">
      <xsl:value-of select="@linkend"/>
    </xsl:variable>
    <a href="#{$linkend}">
      <xsl:apply-templates select="//*[@id = $linkend]" mode="name"/>
    </a>
  </xsl:template>
</xsl:stylesheet>
