<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

<!-- XSLT stylesheet to transform a XML bibliography in DocBook -->

  <xsl:output method="xml"    
    doctype-public="-//OASIS//DTD DocBook XML V4.1.2//EN"
    doctype-system="dtd/xml/4.1.2/docbookx.dtd"/>

  <xsl:preserve-space elements="*"/>

  <xsl:template match="/">
    <xsl:comment><xsl:text>&#10;DO NOT EDIT! Automatically produced by </xsl:text> 
    <xsl:value-of select="system-property('xsl:vendor')"/><xsl:text> (</xsl:text>
    <xsl:value-of select="system-property('xsl:vendor-url')"/><xsl:text>)</xsl:text>
    <xsl:text>&#10;</xsl:text></xsl:comment>
    <xsl:text>&#10;</xsl:text>
    <xsl:text>&#10;</xsl:text>
    <xsl:apply-templates select="bibliography"/>
  </xsl:template>

  <xsl:template match="bibliography">
    <bibliography>
      <xsl:apply-templates select="*"/>
    </bibliography>
  </xsl:template>

  <xsl:template match="book|article|netresource|inproceedings">
    <xsl:variable name="id"><xsl:value-of select="@id"/></xsl:variable>
    <xsl:variable name="label"><xsl:value-of select="@id"/></xsl:variable>
    <biblioentry><xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute><xsl:attribute name="xreflabel"><xsl:value-of select="$label"/></xsl:attribute>
      <xsl:apply-templates/>
    </biblioentry>
  </xsl:template>

  <xsl:template match="rfc">
    <xsl:variable name="id"><xsl:text>RFC</xsl:text><xsl:value-of select="@num"/></xsl:variable>
    <xsl:variable name="label"><xsl:text>RFC </xsl:text><xsl:value-of select="@num"/></xsl:variable>
    <biblioentry><xsl:attribute name="id"><xsl:value-of select="$id"/></xsl:attribute><xsl:attribute name="xreflabel"><xsl:value-of select="$label"/></xsl:attribute>
      <xsl:apply-templates/>
    </biblioentry>
  </xsl:template>

  <!-- TODO: handle multiple authors -->
  <xsl:template match="author">
    <author>
      <xsl:if test="firstname">
	<firstname><xsl:value-of select="firstname"/></firstname>
      </xsl:if>
      <xsl:if test="middlename">
	<othername><xsl:value-of select="middlename"/></othername>
      </xsl:if>
      <xsl:if test="lastname">
	<surname><xsl:value-of select="lastname"/></surname>
      </xsl:if>
    </author>
  </xsl:template>
  
  <xsl:template match="title">
    <title><xsl:value-of select="text()"/></title>
  </xsl:template>

  <xsl:template match="year">
    <date><xsl:value-of select="text()"/></date>
  </xsl:template>

  <!-- TODO: do something with months. -->

  <xsl:template match="number">
    <xsl:if test="@id=isbn">
      <isbn><xsl:value-of select="text()"/></isbn>
    </xsl:if>
    <!-- TODO: what if not isbn? -->
  </xsl:template>

  <xsl:template match="publisher">
    <publisher><publishername><xsl:value-of select="text()"/></publishername></publisher>
  </xsl:template>

  <xsl:template match="journal">
    <!-- TODO: not easy to render in DocBook -->
  </xsl:template>

  <xsl:template match="url">
    <xsl:variable name="url"><xsl:value-of select="text()"/></xsl:variable>
    <abstract><para><ulink><xsl:attribute name="url"><xsl:value-of select="$url"/></xsl:attribute><xsl:value-of select="$url"/></ulink></para></abstract>
  </xsl:template>

  <xsl:template match="*">
  </xsl:template>

</xsl:stylesheet>

