<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:fo="http://www.w3.org/1999/XSL/Format" 
  version="1.0">
  
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
 
  <xsl:key name="footnotes" match="f" use="text()"/>
 
  <xsl:template match="content">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="all-pages"
          page-width="210mm" page-height="297mm">
          <fo:region-body region-name="xsl-region-body"
            margin-top="20mm" margin-bottom="20mm"
            margin-left="20mm" margin-right="20mm" />
        </fo:simple-page-master>
      </fo:layout-master-set>

      <fo:page-sequence master-reference="all-pages">
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates />
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
  
  <xsl:template match="p">
    <fo:block><xsl:apply-templates /></fo:block>
  </xsl:template>

  <xsl:template match="f">
    <xsl:variable name="the-first-footnote" select="key('footnotes', text())[1]"/>
    <xsl:variable name="footnote-id">
      <xsl:call-template name="count-unique-footnotes">
        <xsl:with-param name="fns" select="$the-first-footnote/preceding::f"/>
      </xsl:call-template>
    </xsl:variable>
    <!-- the anchor only -->
    <fo:footnote>
      <fo:inline font-size="80%" vertical-align="super"><xsl:value-of select="$footnote-id" /></fo:inline>
      <fo:footnote-body><fo:block/></fo:footnote-body>
    </fo:footnote>
 
    <!-- fake footnotes with invisible anchors -->
    <xsl:apply-templates select=". | following::f" mode="fake-footnotes">
      <xsl:sort select="count(key('footnotes',text())[1]/preceding::f)" data-type="number"/>
    </xsl:apply-templates>
 
 </xsl:template>

  <xsl:template name="count-unique-footnotes">
    <xsl:param name="count" select="1"/>
    <xsl:param name="fns"/>
    <xsl:choose>
      <xsl:when test="$fns">
        <xsl:call-template name="count-unique-footnotes">
          <xsl:with-param name="fns" select="$fns[position() &gt; 1]"/>
          <xsl:with-param name="count" select="$count + number(generate-id($fns[1])=generate-id(key('footnotes',$fns[1]/text()))) "/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise><xsl:value-of select="$count"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="f" mode="fake-footnotes">
    <xsl:variable name="the-first-footnote" select="key('footnotes', text())[1]"/>
    <xsl:variable name="footnote-id">
      <xsl:call-template name="count-unique-footnotes">
        <xsl:with-param name="fns" select="$the-first-footnote/preceding::f"/>
      </xsl:call-template>
    </xsl:variable>
    <fo:footnote>
      <fo:inline/>
      <fo:footnote-body id="{$footnote-id}">
        <fo:block>
          <xsl:value-of select="$footnote-id" />. <xsl:apply-templates />
       </fo:block>
      </fo:footnote-body>
    </fo:footnote>
  </xsl:template>
  
</xsl:stylesheet>


