RefEntry XSLT Stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<!-- RefEntry XSLT Stylesheet                                      -->
<!-- Stylesheet for single RefEntry document for WebMan project    -->
<!--                                                               -->
<!-- Copyright 2002-2003, Mark Craig                               -->
<!--                                                               -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" encoding="UTF-8"/>
  <xsl:param name="ServletURL" select="'refentry'"/>
  <!--                                                               -->
  <!-- Section names used for the RefEntry pages.                    -->
  <!--                                                               -->
  <xsl:variable name="manvolnum">
    <xsl:value-of select="/refentry/refmeta/manvolnum"/>
  </xsl:variable>
  <xsl:variable name="section">
    <xsl:choose>
      <xsl:when test="$manvolnum = '1'">User Commands</xsl:when>
      <xsl:when test="$manvolnum = '1M'">Admin Commands</xsl:when>
      <xsl:when test="$manvolnum = '2'">System Calls</xsl:when>
      <xsl:when test="$manvolnum = '3'">Library Functions</xsl:when>
      <xsl:when test="$manvolnum = '4'">Special Files</xsl:when>
      <xsl:when test="$manvolnum = '5'">File Formats</xsl:when>
      <xsl:when test="$manvolnum = '6'">Games</xsl:when>
      <xsl:when test="$manvolnum = '7'">Conventions and Standards</xsl:when>
      <xsl:when test="$manvolnum = '8'">Admin Commands</xsl:when>
      <xsl:otherwise>Section unknown (see refentry.xslt)</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <!--                                                               -->
  <!-- Output for the RefEntry is a division that belongs together.  -->
  <!--                                                               -->
  <xsl:template match="/">
    <div>
      <xsl:apply-templates select="refentry/*"/>
    </div>
  </xsl:template>
  <!--                                                               -->
  <!-- Output a table with the name of the page at the top.          -->
  <!--                                                               -->
  <xsl:variable name="refentrytitle">
    <xsl:value-of select="/refentry/refmeta/refentrytitle"/>
  </xsl:variable>
  <xsl:template match="refmeta">
    <table border="0" width="100%">
      <tr>
        <td align="left">
          <xsl:value-of select="$refentrytitle"/>(<xsl:value-of 
	   select="$manvolnum"/>)</td>
        <td align="center">
          <xsl:value-of select="$section"/>
        </td>
        <td align="right">
          <xsl:value-of select="$refentrytitle"/>(<xsl:value-of
	  select="$manvolnum"/>)</td>
      </tr>
    </table>
  </xsl:template>
  <!--                                                               -->
  <!-- Handle the RefNameDiv of the document.                        -->
  <!--                                                               -->
  <xsl:template match="refnamediv">
    <h1>NAME</h1>
    <blockquote>
      <xsl:apply-templates select="refname">
        <xsl:sort/>
      </xsl:apply-templates>
      <xsl:text> - </xsl:text>
      <xsl:value-of select="refpurpose"/>
    </blockquote>
  </xsl:template>
  <xsl:template match="refname">
    <tt>
      <xsl:value-of select="."/>
    </tt>
    <xsl:if test="not(position()=last())">, </xsl:if>
  </xsl:template>
  <!--                                                               -->
  <!-- Handle the RefSynopsisDiv of the document.                    -->
  <!--                                                               -->
  <xsl:template match="refsynopsisdiv">
    <xsl:if test="@id">
      <a name="{@id}"/>
    </xsl:if>
    <h1>
      <xsl:value-of select="title"/>
    </h1>
    <blockquote>
      <p>
        <tt>
          <xsl:apply-templates select="classsynopsis | cmdsynopsis | 
	   constructorsynopsis | destructorsynopsis | fieldsynopsis |
	   funcsynopsis | methodsynopsis | synopsis"/>
        </tt>
      </p>
    </blockquote>
  </xsl:template>
  <xsl:template match="classsynopsis">
    <xsl:apply-templates select="ooclass | ooexception | oointerface"/>
    <xsl:apply-templates select="classsynopsisinfo | constructorsynopsis |
     destructorsynopsis | fieldsynopsis | methodsynopsis"/>
  </xsl:template>
  <xsl:template match="ooclass | ooexception | oointerface |
   classsynopsisinfo | funcsynopsisinfo">
    <xsl:apply-templates/>
    <br />
  </xsl:template>
  <xsl:template match="modifier| command | type">
    <xsl:value-of select="concat(., ' ')"/>
  </xsl:template>
  <xsl:template match="classname | exceptionname | interfacename |
   command | methodname | varname | function">
    <xsl:if test="classname">
      <xsl:text>class </xsl:text>
    </xsl:if>
    <xsl:if test="exceptionname">
      <xsl:text>exception </xsl:text>
    </xsl:if>
    <xsl:if test="interfacename">
      <xsl:text>interface </xsl:text>
    </xsl:if>
    <b>
      <xsl:value-of select="concat(., ' ')"/>
    </b>
  </xsl:template>
  <xsl:template match="cmdsynopsis | funcsynopsis | funcdef | synopsis">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="arg | group">
    <xsl:choose>
      <xsl:when test="@choice = 'opt'">[ <xsl:apply-templates/> ]</xsl:when>
      <xsl:when test="@choice = 'required'">{ <xsl:apply-templates
       /> }</xsl:when>
      <xsl:when test="@rep = 'repeat'">
        <xsl:apply-templates/> ...</xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
        <xsl:text> </xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="methodparam">
    <xsl:choose>
      <xsl:when test="not(position()=last())">
        <xsl:apply-templates/>
        <xsl:if test="@rep = 'repeat'">
          <xsl:text>...</xsl:text>
        </xsl:if>
        <xsl:text>, </xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
        <xsl:if test="@rep = 'repeat'">
          <xsl:text>...</xsl:text>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="replaceable|parameter|citetitle|emphasis">
    <i>
      <xsl:value-of select="."/>
    </i>
  </xsl:template>
  <xsl:template match="sbr">
    <br />
  </xsl:template>
  <xsl:template match="constructorsynopsis | destructorsynopsis">
    <xsl:apply-templates select="modifier"/>
    <xsl:if test="destructorsynopsis">~</xsl:if>
    <xsl:choose>
      <xsl:when test="methodname">
        <b>
          <xsl:value-of select="methodname"/>
        </b>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$refentrytitle"/>
      </xsl:otherwise>
    </xsl:choose>(<xsl:apply-templates select="methodparam"/>
    <xsl:apply-templates select="void"/>)<xsl:if test="exceptionname">
      <xsl:text> throws </xsl:text><xsl:apply-templates
      select="exceptionname"/></xsl:if>;<br />
  </xsl:template>
  <xsl:template match="void">void</xsl:template>
  <xsl:template match="fieldsynopsis">
    <xsl:apply-templates/>;<br />
  </xsl:template>
  <xsl:template match="initializer">
    <xsl:value-of select="concat(' = ', .)"/>
  </xsl:template>
  <xsl:template match="funcprototype">
    <xsl:apply-templates select="funcdef"/>(<xsl:apply-templates
     select="varargs | void | paramdef"/>);<br />
  </xsl:template>
  <xsl:template match="varargs">
    <xsl:text>...</xsl:text>
  </xsl:template>
  <xsl:template match="paramdef">
    <xsl:apply-templates/>
    <xsl:if test="not(position()=last())">, </xsl:if>
  </xsl:template>
  <xsl:template match="methodsynopsis">
    <xsl:apply-templates select="modifier"/>
    <xsl:choose>
      <xsl:when test="type">
        <xsl:value-of select="concat(type, ' ')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="void"/>
        <xsl:text> </xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:apply-templates select="methodname"/>(<xsl:apply-templates
     select="methodparam"/><xsl:apply-templates
     select="void"/>)<xsl:if test="exceptionname"><xsl:text> 
     throws </xsl:text><xsl:apply-templates
     select="exceptionname"/></xsl:if>;<br />
  </xsl:template>
  <!--                                                               -->
  <!-- Handle a RefSect1.                                            -->
  <!--                                                               -->
  <xsl:template match="refsect1">
    <xsl:if test="@id">
      <a name="{@id}"/>
    </xsl:if>
    <h1>
      <xsl:value-of select="title"/>
    </h1>
    <blockquote>
      <xsl:apply-templates select="para | blockquote | example |
       itemizedlist | msgset | orderedlist | programlisting | screen |
       table | variablelist | refsect2"/>
    </blockquote>
  </xsl:template>
  <xsl:template match="para">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="citerefentry">
    <a>
      <xsl:attribute name="href"><xsl:value-of
       select="$ServletURL"/>?RefEntry=<xsl:value-of
       select="refentrytitle"/>-<xsl:value-of
       select="manvolnum"/></xsl:attribute>
      <tt>
        <xsl:value-of select="refentrytitle"/>
      </tt>(<xsl:value-of select="manvolnum"/>)
    </a>
  </xsl:template>
  <xsl:template match="errorcode | errorname | errortype | literal">
    <tt>
      <xsl:value-of select="."/>
    </tt>
  </xsl:template>
  <xsl:template match="quote">
    <xsl:text>"</xsl:text>
      <xsl:value-of select="."/>
    <xsl:text>"</xsl:text>
  </xsl:template>
  <xsl:template match="trademark">
    <xsl:value-of select="."/>
    <xsl:choose>
      <xsl:when test="@class = 'copyright'">
        <xsl:text>[C]</xsl:text>
      </xsl:when>
      <xsl:when test="@class = 'registered'">
        <xsl:text>[R]</xsl:text>
      </xsl:when>
      <xsl:when test="@class = 'service'">
        <xsl:text>[sm]</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>[tm]</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="ulink">
    <a href="{@url}">
      <xsl:value-of select="."/>
    </a>
  </xsl:template>
  <xsl:template match="xref">
    <a href="#{@linkend}">
      <xsl:value-of select="id(@linkend)/title"/>
    </a>
  </xsl:template>
  <xsl:template match="blockquote">
    <blockquote>
      <table border="0">
        <tr>
          <td>
            <xsl:apply-templates select="para"/>
          </td>
        </tr>
        <tr>
          <td align="right">
            <xsl:text>- </xsl:text>
            <xsl:value-of select="attribution"/>
          </td>
        </tr>
      </table>
    </blockquote>
  </xsl:template>
  <!--                                                               -->
  <!-- Handle an Example.                                            -->
  <!--                                                               -->
  <xsl:template match="example">
    <xsl:if test="@id">
      <a name="{@id}"/>
    </xsl:if>
    <h2>
      <xsl:value-of select="title"/>
    </h2>
    <xsl:apply-templates select="para | programlisting | screen"/>
  </xsl:template>
  <xsl:template match="programlisting">
    <hr noshade="noshade" />
    <pre>
      <xsl:apply-templates/>
    </pre>
    <hr noshade="noshade" />
  </xsl:template>
  <xsl:template match="screen">
    <table border="1" width="90%">
      <tr>
        <td>
          <pre>
            <xsl:apply-templates/>
          </pre>
        </td>
      </tr>
    </table>
  </xsl:template>
  <xsl:template match="computeroutput">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="userinput">
    <b>
      <xsl:apply-templates/>
    </b>
  </xsl:template>
  <!--                                                               -->
  <!-- Handle lists and tables.                                      -->
  <!--                                                               -->
  <xsl:template match="itemizedlist">
    <ul>
      <xsl:apply-templates select="listitem"/>
    </ul>
  </xsl:template>
  <xsl:template match="listitem">
    <xsl:choose>
      <xsl:when test="parent::varlistentry">
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
        <li>
          <xsl:apply-templates select="para | itemizedlist |
	   orderedlist | variablelist"/>
        </li>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="msgset">
    <div>
      <xsl:apply-templates select="msgentry"/>
    </div>
  </xsl:template>
  <xsl:template match="msgentry">
    <xsl:apply-templates select="msg | msginfo | msgexplan"/>
  </xsl:template>
  <xsl:template match="msg">
    <xsl:apply-templates select="msgmain | msgrel | msgsub"/>
  </xsl:template>
  <xsl:template match="msgmain | msgrel | msgsub">
    <xsl:apply-templates select="msgtext"/>
  </xsl:template>
  <xsl:template match="msgtext | msgexplan">
    <xsl:apply-templates select="para | blockquote | itemizedlist |
     orderedlist | programlisting | screen | variablelist"/>
  </xsl:template>
  <xsl:template match="msginfo">
    <xsl:apply-templates select="msglevel | msgorig"/>
  </xsl:template>
  <xsl:template match="msglevel">
    <p>
      <b>Level</b>
      <xsl:value-of select="concat(' ',.)"/>
    </p>
  </xsl:template>
  <xsl:template match="msgorig">
    <p>
      <b>Origin</b>
      <xsl:value-of select="concat(' ',.)"/>
    </p>
  </xsl:template>
  <xsl:template match="orderedlist">
    <ol>
      <xsl:apply-templates select="listitem"/>
    </ol>
  </xsl:template>
  <xsl:template match="table">
    <h2>
      <xsl:value-of select="title"/>
    </h2>
    <table>
      <xsl:choose>
        <xsl:when test="@colsep = 1">
          <xsl:attribute
	   name="rules"><xsl:text>cols</xsl:text></xsl:attribute>
        </xsl:when>
        <xsl:when test="@frame">
          <xsl:choose>
            <xsl:when test="@frame = 'bottom'">
              <xsl:attribute 
	       name="frame"><xsl:text>below</xsl:text></xsl:attribute>
            </xsl:when>
            <xsl:when test="@frame = 'none'">
              <xsl:attribute
	       name="frame"><xsl:text>void</xsl:text></xsl:attribute>
            </xsl:when>
            <xsl:when test="@frame = 'sides'">
              <xsl:attribute
	       name="frame"><xsl:text>box</xsl:text></xsl:attribute>
            </xsl:when>
            <xsl:when test="@frame = 'top'">
              <xsl:attribute
	       name="frame"><xsl:text>above</xsl:text></xsl:attribute>
            </xsl:when>
            <xsl:when test="@frame = 'topbot'">
              <xsl:attribute
	       name="frame"><xsl:text>hsides</xsl:text></xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
              <xsl:attribute
	       name="border"><xsl:text>1</xsl:text></xsl:attribute>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:when test="@rowsep = 1">
          <xsl:attribute
	   name="rules"><xsl:text>rows</xsl:text></xsl:attribute>
        </xsl:when>
      </xsl:choose>
      <xsl:attribute name="width"><xsl:text>90%</xsl:text></xsl:attribute>
      <xsl:apply-templates select="tgroup"/>
    </table>
  </xsl:template>
  <xsl:template match="tgroup">
    <xsl:apply-templates select="thead | tbody"/>
  </xsl:template>
  <xsl:template match="thead">
    <thead>
      <xsl:if test="@valign">
        <xsl:attribute name="valign"><xsl:choose><xsl:when
	 test="@valign = 'bottom'">bottom</xsl:when><xsl:when
	 test="@valign = 'top'">top</xsl:when><xsl:otherwise
	 >middle</xsl:otherwise></xsl:choose></xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="row"/>
    </thead>
  </xsl:template>
  <xsl:template match="tbody">
    <tbody>
      <xsl:if test="@valign">
        <xsl:attribute name="valign"><xsl:choose><xsl:when
	 test="@valign = 'bottom'">bottom</xsl:when><xsl:when
	 test="@valign = 'top'">top</xsl:when><xsl:otherwise
	 >middle</xsl:otherwise></xsl:choose></xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="row"/>
    </tbody>
  </xsl:template>
  <xsl:template match="row">
    <tr>
      <xsl:if test="@valign">
        <xsl:attribute name="valign"><xsl:choose><xsl:when
	 test="@valign = 'bottom'">bottom</xsl:when><xsl:when
	 test="@valign = 'top'">top</xsl:when><xsl:otherwise
	 >middle</xsl:otherwise></xsl:choose></xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="entry"/>
    </tr>
  </xsl:template>
  <xsl:template match="entry">
    <td>
      <xsl:if test="@align">
        <xsl:attribute name="align"><xsl:choose><xsl:when
	 test="@align = 'left'">left</xsl:when><xsl:when
	 test="@align = 'right'">right</xsl:when><xsl:otherwise
	 >center</xsl:otherwise></xsl:choose></xsl:attribute>
      </xsl:if>
      <xsl:if test="@morerows">
        <xsl:attribute name="rowspan">
	  <xsl:value-of select="number(@morerows + 1)"/>
	</xsl:attribute>
      </xsl:if>
      <xsl:if test="@valign">
        <xsl:attribute name="valign"><xsl:choose><xsl:when
	 test="@valign = 'bottom'">bottom</xsl:when><xsl:when
	 test="@valign = 'top'">top</xsl:when><xsl:otherwise
	 >middle</xsl:otherwise></xsl:choose></xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="para"/>
    </td>
  </xsl:template>
  <xsl:template match="variablelist">
    <blockquote>
      <dl>
        <xsl:apply-templates select="varlistentry"/>
      </dl>
    </blockquote>
  </xsl:template>
  <xsl:template match="varlistentry">
    <xsl:apply-templates select="term"/>
    <dd>
      <xsl:apply-templates select="listitem"/>
    </dd>
  </xsl:template>
  <xsl:template match="term">
    <dt>
      <tt>
        <xsl:value-of select="."/>
      </tt>
    </dt>
  </xsl:template>
  <!--                                                               -->
  <!-- Handle a RefSect2.                                            -->
  <!--                                                               -->
  <xsl:template match="refsect2">
    <xsl:if test="@id">
      <a name="{@id}"/>
    </xsl:if>
    <h2>
      <xsl:value-of select="title"/>
    </h2>
    <blockquote>
      <xsl:apply-templates select="para | blockquote | itemizedlist |
       orderedlist | programlisting | screen | variablelist"/>
    </blockquote>
  </xsl:template>
</xsl:stylesheet>