<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns="http://www.gribuser.ru/xml/fictionbook/2.0"
  version="1.0">
  <xsl:output method="xml" encoding="windows-1251"/>

  <!-- copy elements by default, forcing them to Fb2.0 namespace -->
  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:for-each select="@*">
	<xsl:attribute name="{local-name()}">
	  <xsl:value-of select="."/>
	</xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <!-- copy the root element to force namespaces on the root element -->
  <xsl:template match="/FictionBook">
    <FictionBook>
      <xsl:apply-templates/>
    </FictionBook>
  </xsl:template>

  <!-- override specific items -->
  <xsl:template match="translators">
    <xsl:for-each select="./author">
      <translator>
	<xsl:apply-templates/>
      </translator>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="document-info">
   <document-info>
    <xsl:apply-templates/>
    <version>1.0</version>
   </document-info>
  </xsl:template>

  <xsl:template match="section">
    <section>
      <xsl:if test="@title and not(title) and string-length(@title)">
	<title><p><xsl:value-of select="@title"/></p></title>
      </xsl:if>
      <xsl:apply-templates select="./*"/>
    </section>
  </xsl:template>

  <xsl:template match="binary">
    <xsl:element name="binary">
      <xsl:attribute name="id"><xsl:value-of select="@name"/></xsl:attribute>
      <xsl:choose>
	<xsl:when test="contains(@name,'png')">
	  <xsl:attribute name="content-type">image/png</xsl:attribute>
	</xsl:when>
	<xsl:when test="contains(@name,'jpg') or contains(@name,'jpeg')">
	  <xsl:attribute name="content-type">image/jpeg</xsl:attribute>
	</xsl:when>
	<xsl:otherwise>
	</xsl:otherwise>
      </xsl:choose>
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="image">
    <image>
      <xsl:attribute name="xlink:href">#<xsl:value-of select="@name"/></xsl:attribute>
    </image>
  </xsl:template>

  <xsl:template match="poem">
    <poem>
      <xsl:if test="@title and not(title) and string-length(@title)">
	<title><p><xsl:value-of select="@title"/></p></title>
      </xsl:if>
      <xsl:choose>
	<xsl:when test="./v">
	  <stanza>
	    <xsl:apply-templates select="./*"/>
	  </stanza>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:apply-templates select="./*"/>
	</xsl:otherwise>
      </xsl:choose>
    </poem>
  </xsl:template>

  <xsl:template match="note">
    <xsl:element name="a">
      <xsl:attribute name="xlink:href">#N<xsl:value-of select="@name"/></xsl:attribute>
      <xsl:attribute name="type">note</xsl:attribute>
      <xsl:value-of select="@name"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="body">
    <body>
      <!-- insert coverpage, authors and book name here -->
      <xsl:if test="/FictionBook/description/title-info/coverpage/image">
	<image>
	  <xsl:attribute name="xlink:href">
	    <xsl:text>#</xsl:text>
	    <xsl:value-of select="/FictionBook/description/title-info/coverpage/image/@name"/>
	  </xsl:attribute>
	</image>
      </xsl:if>
      <title>
	<xsl:for-each select="/FictionBook/description/title-info/author">
	  <p>
	    <xsl:value-of select="./first-name"/>
	    <xsl:text> </xsl:text>
	    <xsl:value-of select="./middle-name"/>
	    <xsl:text> </xsl:text>
	    <xsl:value-of select="./last-name"/>
	  </p>
	</xsl:for-each>
	<xsl:if test="/FictionBook/description/title-info/book-title">
	  <p><xsl:value-of select="/FictionBook/description/title-info/book-title"/></p>
	</xsl:if>
      </title>
      <xsl:apply-templates/>
    </body>

    <xsl:if test="//note">
      <body name="footnotes">
	<section>
	  <xsl:apply-templates select="//note" mode="copynotes"/>
	</section>
      </body>
    </xsl:if>
  </xsl:template>

  <xsl:template match="*" mode="copynotes">
    <xsl:element name="{local-name()}">
      <xsl:for-each select="@*">
	<xsl:attribute name="{local-name()}">
	  <xsl:value-of select="."/>
	</xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="note" mode="copynotes">
    <xsl:element name="subtitle">
      <xsl:attribute name="id">N<xsl:value-of select="@name"/></xsl:attribute>
      <xsl:value-of select="@name"/>
    </xsl:element>
    <xsl:apply-templates select="./*" mode="copynotes"/>
    <empty-line/>
  </xsl:template>

</xsl:stylesheet>

