<?xml version="1.0"?>

<!--
Stylesheet for conversion upCast 4.0.0 -> DocBook 4.1.2

** PROVIDED AS-IS, NO WARRANTIES, NO SUPPORT! **

Important setting up of UpCast:
_______________________________
Filter:                          XML (upCast DTD)

Configuration of filter:
________________________
Table model:                     CALS (XML-EM/OASIS)

Author:  Pavel Zampach, zampach@volny.cz

Updated for upCast 4.0.0:  Christian Roth, cr@infinity-loop.de, 2003-06-23

-->

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:html="http://www.w3.org/HTML/1998/html4"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    exclude-result-prefixes="#default xlink html" >

<xsl:output
  method="xml"
  doctype-public="-//OASIS//DTD DocBook XML V4.1.2//EN"
  doctype-system="http://www.oasis-open.org/docbook/xml/4.0/docbookx.dtd"
  indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:template match="document">
  <article>
     <xsl:apply-templates/>
  </article>
</xsl:template>

<xsl:template match="documentinfo">
  <!-- nop -->
</xsl:template>

<xsl:template match="part">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="section">
  <xsl:variable name="content" select="." />
  <xsl:if test="$content != ''">
    <section>
      <title>
        <xsl:value-of select="heading"/>
      </title>
      <xsl:apply-templates/>
    </section>
  </xsl:if>
</xsl:template>

<xsl:template match="heading">
  <!-- nop -->
</xsl:template>

<xsl:template match="par[not (image)]">
  <para>
    <xsl:apply-templates/>
  </para>
</xsl:template>

<xsl:template match="inline">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="plain">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="hidden">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="list">
  <xsl:variable name="type" select="@style"/>
  <xsl:choose>
    <xsl:when test="$type='disc' or $type=''">
      <itemizedlist>
        <xsl:apply-templates/>
      </itemizedlist>
    </xsl:when>
    <xsl:otherwise>
      <orderedlist>
        <xsl:attribute name="numeration">
          <xsl:choose>
            <xsl:when test="contains( $type, 'upper-roman')">upperroman</xsl:when>
            <xsl:when test="$type='lower-roman'">lowerroman</xsl:when>
            <xsl:when test="$type='upper-alpha'">upperalpha</xsl:when>
            <xsl:when test="$type='lower-alpha'">loweralpha</xsl:when>
            <xsl:otherwise>arabic</xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>

        <xsl:apply-templates/>
      </orderedlist>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="item">
  <listitem>
    <xsl:apply-templates/>
  </listitem>
</xsl:template>

<xsl:template match="table">
  <informaltable>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </informaltable>
</xsl:template>

<xsl:template match="tgroup">
  <tgroup>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </tgroup>
</xsl:template>

<xsl:template match="colspec">
  <colspec>
    <xsl:copy-of select="@*"/>
  </colspec>
</xsl:template>

<xsl:template match="thead">
  <thead>
    <xsl:apply-templates/>
  </thead>
</xsl:template>

<xsl:template match="tbody">
  <tbody>
    <xsl:apply-templates/>
  </tbody>
</xsl:template>

<xsl:template match="row">
  <row>
    <xsl:apply-templates/>
  </row>
</xsl:template>

<xsl:template match="entry">

<xsl:variable name="align" select="par[1]/@style"/>

  <entry>
    <xsl:copy-of select="@*"/>
    <xsl:if test="$align != ''">
      <xsl:attribute name="align">
        <xsl:choose>
          <xsl:when test="contains( $align, 'text-align: justify')">justify</xsl:when>
          <xsl:when test="contains( $align, 'text-align: right')">right</xsl:when>
          <xsl:when test="contains( $align, 'text-align: left')">left</xsl:when>
          <xsl:when test="contains( $align, 'text-align: center')">center</xsl:when>
          <xsl:otherwise>left</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates/>
  </entry>
</xsl:template>

<xsl:template match="image | par/image">
  <mediaobject>
    <imageobject>
      <imagedata>
        <xsl:attribute name="fileref">
          <xsl:value-of select="@xlink:href"/>
        </xsl:attribute>
      </imagedata>
    </imageobject>
  </mediaobject>
</xsl:template>

<xsl:template match="subdoc">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="reference">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="footnote">
  <footnote>
    <xsl:apply-templates/>
  </footnote>
</xsl:template>

<xsl:template match="textbox">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="annotation">
  <xsl:comment>
    <xsl:apply-templates/>
  </xsl:comment>
</xsl:template>

<xsl:template match="gentext">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="target">
  <anchor>
    <xsl:attribute name="id">
      <xsl:value-of select="@id"/>
    </xsl:attribute>
  </anchor>
</xsl:template>

<xsl:template match="link">
  <ulink>
    <xsl:attribute name="url">
      <xsl:value-of select="@xlink:href" />
    </xsl:attribute>
    <xsl:apply-templates/>
  </ulink>
</xsl:template>

<xsl:template match="linebreak">
  <!-- nop -->
</xsl:template>

<xsl:template match="pagebreak">
  <!-- nop -->
</xsl:template>

<xsl:template match="columnbreak">
  <!-- nop -->
</xsl:template>

<xsl:template match="indextarget">
  <indexterm>
    <xsl:choose>
      <xsl:when test="@entryLevel1">
        <primary>
          <xsl:value-of select="@entryLevel1"/>
        </primary>
      </xsl:when>
      <xsl:when test="@entryLevel2">
        <secondary>
          <xsl:value-of select="@entryLevel2"/>
        </secondary>
      </xsl:when>
      <xsl:when test="@entryLevel3">
        <tertiary>
          <xsl:value-of select="@entryLevel3"/>
        </tertiary>
      </xsl:when>
      <xsl:otherwise>
        <!-- nop -->
      </xsl:otherwise>
    </xsl:choose>

    <xsl:apply-templates/>
  </indexterm>
</xsl:template>

<xsl:template match="pageheader">
  <!-- nop -->
</xsl:template>

<xsl:template match="pagefooter">
  <!-- nop -->
</xsl:template>

<xsl:template match="toc">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="formtext">
  <!-- nop -->
</xsl:template>

<xsl:template match="formcheckbox">
  <!-- nop -->
</xsl:template>

<xsl:template match="formdropdown">
  <!-- nop -->
</xsl:template>

</xsl:stylesheet>
