<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1"
  xmlns:fo="http://www.w3.org/1999/XSL/Format">
  
  <xsl:output method="xml"/>
  
  <xsl:variable name="topiclist">
    <topiclist>
      <xsl:for-each select="/topicmap/topics/topic[desc]">
        <topic id="{@TID}" name="{translate(name,'abcdefghijklmnopqrstuvwxyz./-+','ABCDEFGHIJKLMNOPQRSTUVWXYZ')}"/>
      </xsl:for-each>
    </topiclist>
  </xsl:variable>
  <xsl:variable name="sortedtopiclist">
    <sortedtopiclist>
      <xsl:for-each select="$topiclist/topiclist/topic">
        <xsl:sort select="@name"/>
        <topic id="{@id}" name="{@name}"/>
      </xsl:for-each>
    </sortedtopiclist>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:variable name="document" select="current()"/>
    <xsl:document href="glossary.fo">
      
      <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        
        <fo:layout-master-set>
          <fo:simple-page-master master-name="simple"
            page-height="29.7cm" 
            page-width="21cm"
            margin-top="2cm" 
            margin-bottom="1.5cm" 
            margin-left="2.5cm" 
            margin-right="2.5cm">
            <fo:region-body margin-top="1.5cm" margin-bottom="1.5cm"/>
            <fo:region-before extent="1.5cm"/>
            <fo:region-after extent="1cm"/>
          </fo:simple-page-master>
        </fo:layout-master-set>
        
        <fo:page-sequence master-reference="simple">
          
          <fo:static-content flow-name="xsl-region-before">
            <fo:block font-size="12pt" 
              font-family="Times Roman"
              line-height="14pt"
              text-align="end">
              <fo:retrieve-marker retrieve-class-name="topic"
                retrieve-boundary="page"
                retrieve-position="first-starting-within-page"/> - 
              <fo:retrieve-marker retrieve-class-name="topic"
                retrieve-boundary="page"
                retrieve-position="last-ending-within-page"/>
            </fo:block>
            <fo:block line-height="1pt">
              <fo:leader leader-alignment="reference-area" leader-pattern="rule" leader-length="100%"/>
            </fo:block>
          </fo:static-content>
          
          <fo:static-content flow-name="xsl-region-after">
            <fo:block font-size="14pt" 
              font-family="Times Roman"
              line-height="14pt"
              text-align="end">
              <fo:page-number/>
            </fo:block>
          </fo:static-content>
          
          <fo:flow flow-name="xsl-region-body">
            
            <!-- Title -->
            <fo:block font-size="18pt" 
              font-family="Times Roman"
              font-weight="bold"
              line-height="24pt"
              space-after.optimum="12pt"
              text-align="center"
              padding-top="3pt">
              Topiclist
            </fo:block>
            
            <!-- Author -->
            <fo:block font-size="14pt" 
              font-family="Times Roman"
              line-height="24pt"
              space-after.optimum="16pt"
              text-align="center"
              padding-top="3pt">
              Joe Average
            </fo:block>
            
            <xsl:for-each select="$sortedtopiclist/sortedtopiclist/topic">
              <xsl:apply-templates select="$document/topicmap/topics/topic[@TID=current()/@id]"/>
            </xsl:for-each>
            
          </fo:flow>
        </fo:page-sequence>
      </fo:root>
      
    </xsl:document>
  </xsl:template>

  <xsl:template match="topic">
	<fo:block font-size="14pt" 
		font-family="Times Roman" 
    	line-height="15pt"
		space-before.optimum="10pt"
		space-after.optimum="5pt"
		text-align="justify" id="{@TID}">
		<xsl:value-of select="name"/>
		<xsl:if test="alias"> - <xsl:value-of select="alias"/></xsl:if>
		<fo:block>
			<fo:marker marker-class-name="topic">
				<xsl:value-of select="name"/>
			</fo:marker>
		</fo:block>
	</fo:block>
	<xsl:apply-templates select="desc"/>
  </xsl:template>

  <xsl:template match="desc">
    <fo:block font-size="12pt" 
      font-family="Times Roman" 
      line-height="15pt"
      space-after.optimum="10pt"
      text-align="justify"
      language="en"
      country="US"
      hyphenate="true"
      hyphenation-push-character-count="2"
      hyphenation-remain-character-count="2"> 
    <xsl:apply-templates/>
  </fo:block>

  </xsl:template>

  <xsl:template match="topicref">
    <xsl:variable name="topic" select="/topicmap/topics/topic[@TID=current()/@TID]"/>
    <xsl:choose>
      <xsl:when test="$topiclist/topiclist/topic[@id=current()/@TID]">
        <fo:basic-link color="blue" internal-destination="{@TID}"><xsl:value-of select="$topic/name"/></fo:basic-link>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$topic/name"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
</xsl:stylesheet>

