<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="application/xml" href="docbook-html.xsl"?>
<!DOCTYPE article
  PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<!--
   Copyright 2004-2009 Dag-Erling Smørgrav

   Distributed under Creative Commons Attribution-Share Alike 3.0
   http://creativecommons.org/licenses/by-sa/3.0/

   $Id: index.xml 26 2009-05-26 13:34:23Z des $
 -->
<article>
  <articleinfo>
    <title>On-The-Fly Stylesheets for DocBook XML</title>
    <author>
      <firstname>Dag-Erling</firstname>
      <surname>Smørgrav</surname>
    </author>
  </articleinfo>

  <section>
    <title>Overview</title>

    <para>
      These stylesheets are intented for quick previews of <ulink url="http://www.docbook.org/">DocBook XML</ulink> documents in CSS- and XSLT-capable web browsers.
      The main advantage is that you can preview a document that you are working on with no further ado than pressing "reload" in your browser.
      They are also useful for situations such as browsing a Subversion repository using mod_dav_svn or a similar web interface; with the proper magic, when viewing a DocBook document, the user's browser will immediately load the stylesheet, and the user will see a readable document instead of a sea of XML tags.
    </para>

    <para>
      The XSLT version generates valid and easily stylable <ulink url="http://www.w3.org/TR/xhtml1/">XHTML 1.0 Strict</ulink> documents.
      It is accompanied by a simple but fairly pleasant (to the author's eye) CSS stylesheet, but it is also designed to produce output suitable for viewing with browsers that do not support CSS, or with CSS disabled.
      This is achieved by adhering to the following principles:
    </para>

    <orderedlist>
      <listitem>
	<para>
          Use standard HTML elements to approximate the result one would normally expect in the absence of a CSS stylesheet.
          For instance, <literal>&lt;emphasis&gt;</literal> is translated to <literal>&lt;em&gt;</literal>, <literal>&lt;filename&gt;</literal> is translated to <literal>&lt;tt&gt;</literal> and <literal>&lt;screen&gt;</literal> is translated to <literal>&lt;pre&gt;</literal>.
	</para>
      </listitem>
      <listitem>
	<!-- XXX rewrite this one -->
	<para>
          Keep the HTML code simple.
          The XSLT stylesheet does not use any pre-CSS HTML tricks, such as using tables to control positioning, or <literal>&lt;b&gt;</literal> and <literal>&lt;big&gt;</literal> to control the appearance of a subtitle.
	</para>
      </listitem>
      <listitem>
	<para>
          Preserve structural information.
          For instance, sections, chapters etc. are wrapped in <literal>&lt;div&gt;</literal> elements to preserve the structure of the original document.
	</para>
      </listitem>
      <listitem>
	<para>
          Preserve semantical information.
          This means that for most DocBook elements, the corresponding HTML element has a <literal>class</literal> attribute with the name of the original DocBook element.
          For instance, although <literal>&lt;programlisting&gt;</literal> and <literal>&lt;screen&gt;</literal> are both translated to <literal>&lt;pre&gt;</literal>, they have different <literal>class</literal> attributes, so a CSS stylesheet can distinguish between them and render them differently.
	</para>
      </listitem>
    </orderedlist>

    <para>
      The first two principles ensure that the resulting HTML document is viewable in non-CSS-capable browsers, including text-only browsers such as <ulink url="http://lynx.isc.org/">Lynx</ulink> and <ulink url="http://w3m.sourceforge.net/">w3m</ulink>.
      The last two ensure that the HTML document contains as much information as possible about the original DocBook document, to allow more effective use of CSS to control the final layout.
    </para>

    <para>
      The CSS version simply provides information on how to render the raw XML.
      The result is not particularly good, since CSS doesn't allow you to manipulate the order in which elements are displayed or transform the content in any significant manner, but it is good enough for proof-reading.
    </para>
  </section>

  <section>
    <title>Usage</title>

    <section>
      <title>Using the XSLT Version</title>

      <para>
	To use the XSLT version, add the following line at the top of your DocBook document, immediately after the XML declaration and before the document type declaration:
      </para>

      <programlisting>
&lt;?xml-stylesheet type="application/xml" href="docbook-html.xsl"?&gt;
      </programlisting>

      <para>
	<emphasis>IMPORTANT NOTE:</emphasis> the value of the type attribute must match the <literal>Content-Type</literal> your web server specifies for the stylesheet.
	By default, Apache 2 uses <literal>application/xml</literal> for <filename>.xsl</filename> files and <literal>application/xslt+xml</literal> for <filename>.xslt</filename> files.
	The former is the correct MIME type according to the <ulink url="http://www.w3.org/TR/1999/REC-xslt-19991116">XSLT 1.0</ulink> specification.
	The latter is correct according to the <ulink url="http://www.w3.org/TR/xslt20/">XSLT 2.0</ulink> draft specification, but has not yet been registered with IANA.
	Currently, the safest option seems to be to stick with <literal>application/xml</literal>.
      </para>

      <para>
	This assumes that the stylesheet is located in the same directory as the document; otherwise, adjust the <literal>href</literal> attribute to point at the correct URL.
      </para>
    </section>

    <section>
      <title>Using the CSS Version</title>

      <para>
	To use the CSS version, add the following line at the top of your DocBook document, immediately after the XML declaration andx before the document type declaration:
      </para>

      <programlisting>
&lt;?xml-stylesheet type="text/css" href="docbook-xml.css"?&gt;
      </programlisting>

      <para>
	This assumes that the stylesheet is located in the same directory as the document; otherwise, adjust the <literal>href</literal> attribute to point at the correct URL.
      </para>
    </section>

<!--

    <para>
      In theory, you could allow the reader to decide which stylesheet to use:
    </para>

    <programlisting>
&lt;?xml-stylesheet title="XSLT" type="application/xml" href="docbook-html.xsl"?&gt;
&lt;?xml-stylesheet title="CSS" type="text/css" href="docbook-xml.css" alternate="yes"?&gt;
    </programlisting>

    <para>
      However, this doesn't seem to work with Firefox.
      I don't know if it works with any other browsers.
    </para>
-->
  </section>

  <section>
    <title>Bugs and Limitations</title>

    <section>
      <title>Browser Issues</title>

      <para>
	Gecko-based browsers (such as Firefox and Galeon) won't load a stylesheet located on a different server than the document.
      </para>

      <para>
	When using the CSS version, ordered lists will not render properly in Gecko-based browsers, due to a bug in Gecko's implementation of counters (see <ulink url="https://bugzilla.mozilla.org/show_bug.cgi?id=4522">Mozilla bug 4522</ulink>).
      </para>

    </section>

    <section>
      <title>Common Issues</title>

      <para>
	These stylesheets implement only a fairly small subset of DocBook.
	Specifically, the stylesheets implement the bits of DocBook that I a) use frequently and b) could figure out how to implement easily in XSLT or CSS.
	In theory, the feature set of the XSLT stylesheet should be a strict superset of the feature set of the CSS stylesheet, but this may not be the case, as the development process for these stylesheets is rather haphazard.
      </para>

      <para>
	The most obviously useful parts of DocBook that I haven't implemented at all (yet) are images and other embedded media.
      </para>
    </section>

    <section>
      <title>Issues Specific to the XSLT Version</title>

      <para>
	Internal links don't work properly, except for those in the table of contents.
      </para>

      <para>
        In <literal>&lt;table&gt;</literal> and <literal>&lt;informaltable&gt;</literal>, only the first <literal>&lt;tgroup&gt;</literal> is rendered.
        Row and column spans are not implemented; expect surprising results.
      </para>

      <para>
	The XSLT stylesheet has been designed for use with UTF-8 documents (which includes documents written in pure ASCII), and has not been tested with other encodings.
      </para>
    </section>

    <section>
      <title>Issues Specific to the CSS Version</title>

      <para>
	Links, whether internal or external, don't work.
      </para>
    </section>
  </section>

  <section>
    <title>Download</title>

    <itemizedlist>
      <listitem>
	<para>
          XSLT version:
	</para>
	<itemizedlist>
          <listitem>
            <para>
              <ulink url="docbook-html.xsl"/>
            </para>
          </listitem>
          <listitem>
            <para>
              <ulink url="docbook-html.css"/>
            </para>
          </listitem>
	</itemizedlist>
      </listitem>
      <listitem>
	<para>
          CSS version:
	</para>
	<itemizedlist>
          <listitem>
            <para>
              <ulink url="docbook-xml.css"/>
            </para>
          </listitem>
	</itemizedlist>
      </listitem>
    </itemizedlist>
  </section>

  <section>
    <title>Copyright and license</title>

    <para>
      Copyright © 2004-2009 Dag-Erling Smørgrav.
    </para>

    <para>
      Distributed under the <ulink url="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</ulink> license.
    </para>
  </section>
</article>

<!--
Footnotes, indices, bibliography etc. are not implemented at all;
footnotes will appear inline, bibliography and references will
appear unformatted in the position in which they are in the source.
Cross-references are not implemented either.

The articleinfo / bookinfo elements are only partially implemented.

The screen, function, prompt etc. elements are only partially
implemented.
-->
