<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format"
		xmlns:html="http://www.w3.org/1999/xhtml"
                version='1.0'>

  <xsl:import href="http://www.w3.org/2001/tag/webarch/html2fo.xsl"/>

  <xsl:template match="html:h1|html:h2|html:h3|html:h4|html:h5">
    <fo:block keep-with-next="always">
      <xsl:apply-imports/>
    </fo:block>

    <xsl:variable name="id" select="html:a[@id][1]/@id"/>
    <xsl:variable name="xref" select="//html:div[@class='section'
				      and not(html:h2[@class='notoc'])
				      and .//html:a[@href=concat('#',$id)]]"/>
    <xsl:if test="$xref">
      <fo:block keep-with-next="always" margin-left="1.5em" font-size="90%">
	<xsl:text>Referenced from: </xsl:text>
	<xsl:for-each select="$xref">
	  <xsl:if test="position() &gt; 1">, </xsl:if>
	  <fo:basic-link internal-destination="{.//html:a[@id][1]/@id}"
			 font-style="italic" keep-together.within-line="always">
	    <xsl:value-of select="(html:h1|html:h2|html:h3|html:h4|html:h5)[1]"/>
	  </fo:basic-link>
	</xsl:for-each>
      </fo:block>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>
