<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exsl="http://exslt.org/common"
                xmlns:fo="http://www.w3.org/1999/XSL/Format"
		xmlns:html="http://www.w3.org/1999/xhtml"
                xmlns:rx="http://www.renderx.com/XSL/Extensions"
                exclude-result-prefixes="exsl html rx"
                version='1.0'>

<xsl:param name="body.font.family" select="'serif'"/>
<xsl:param name="monospace.font.family" select="'monospace'"/>
<xsl:param name="symbol.font.family" select="'Symbol,ZapfDingbats'"/>
<xsl:param name="title.font.family" select="'sans-serif'"/>

<xsl:param name="title.margin.left" select="'0pt'"/>
<xsl:param name="body.margin.bottom" select="'0.5in'"/>
<xsl:param name="body.margin.top" select="'0.5in'"/>
<xsl:param name="column.count.body" select="1"/>
<xsl:param name="column.gap.body" select="'12pt'"/>
<xsl:param name="page.margin.bottom" select="'0.5in'"/>
<xsl:param name="page.margin.inner" select="'1in'"/>
<xsl:param name="page.margin.outer" select="'1in'"/>
<xsl:param name="page.margin.top" select="'0.5in'"/>
<xsl:param name="page.orientation" select="'portrait'"/>
<xsl:param name="paper.type" select="'USletter'"/>
<xsl:param name="region.after.extent" select="'0.4in'"/>
<xsl:param name="region.before.extent" select="'0.4in'"/>
<xsl:param name="hyphenate" select="'true'"/>

<xsl:param name="xep.extensions" select="1"/>

<xsl:param name="page.height">
  <xsl:choose>
    <xsl:when test="$page.orientation = 'portrait'">
      <xsl:value-of select="$page.height.portrait"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$page.width.portrait"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:param>

<xsl:param name="page.height.portrait">
  <xsl:choose>
    <xsl:when test="$paper.type = 'A4landscape'">210mm</xsl:when>
    <xsl:when test="$paper.type = 'USletter'">11in</xsl:when>
    <xsl:when test="$paper.type = 'USlandscape'">8.5in</xsl:when>
    <xsl:when test="$paper.type = '4A0'">2378mm</xsl:when>
    <xsl:when test="$paper.type = '2A0'">1682mm</xsl:when>
    <xsl:when test="$paper.type = 'A0'">1189mm</xsl:when>
    <xsl:when test="$paper.type = 'A1'">841mm</xsl:when>
    <xsl:when test="$paper.type = 'A2'">594mm</xsl:when>
    <xsl:when test="$paper.type = 'A3'">420mm</xsl:when>
    <xsl:when test="$paper.type = 'A4'">297mm</xsl:when>
    <xsl:when test="$paper.type = 'A5'">210mm</xsl:when>
    <xsl:when test="$paper.type = 'A6'">148mm</xsl:when>
    <xsl:when test="$paper.type = 'A7'">105mm</xsl:when>
    <xsl:when test="$paper.type = 'A8'">74mm</xsl:when>
    <xsl:when test="$paper.type = 'A9'">52mm</xsl:when>
    <xsl:when test="$paper.type = 'A10'">37mm</xsl:when>
    <xsl:when test="$paper.type = 'B0'">1414mm</xsl:when>
    <xsl:when test="$paper.type = 'B1'">1000mm</xsl:when>
    <xsl:when test="$paper.type = 'B2'">707mm</xsl:when>
    <xsl:when test="$paper.type = 'B3'">500mm</xsl:when>
    <xsl:when test="$paper.type = 'B4'">353mm</xsl:when>
    <xsl:when test="$paper.type = 'B5'">250mm</xsl:when>
    <xsl:when test="$paper.type = 'B6'">176mm</xsl:when>
    <xsl:when test="$paper.type = 'B7'">125mm</xsl:when>
    <xsl:when test="$paper.type = 'B8'">88mm</xsl:when>
    <xsl:when test="$paper.type = 'B9'">62mm</xsl:when>
    <xsl:when test="$paper.type = 'B10'">44mm</xsl:when>
    <xsl:when test="$paper.type = 'C0'">1297mm</xsl:when>
    <xsl:when test="$paper.type = 'C1'">917mm</xsl:when>
    <xsl:when test="$paper.type = 'C2'">648mm</xsl:when>
    <xsl:when test="$paper.type = 'C3'">458mm</xsl:when>
    <xsl:when test="$paper.type = 'C4'">324mm</xsl:when>
    <xsl:when test="$paper.type = 'C5'">229mm</xsl:when>
    <xsl:when test="$paper.type = 'C6'">162mm</xsl:when>
    <xsl:when test="$paper.type = 'C7'">114mm</xsl:when>
    <xsl:when test="$paper.type = 'C8'">81mm</xsl:when>
    <xsl:when test="$paper.type = 'C9'">57mm</xsl:when>
    <xsl:when test="$paper.type = 'C10'">40mm</xsl:when>
    <xsl:otherwise>11in</xsl:otherwise>
  </xsl:choose>
</xsl:param>

<xsl:param name="page.width">
  <xsl:choose>
    <xsl:when test="$page.orientation = 'portrait'">
      <xsl:value-of select="$page.width.portrait"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$page.height.portrait"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:param>

<xsl:param name="page.width.portrait">
  <xsl:choose>
    <xsl:when test="$paper.type = 'USletter'">8.5in</xsl:when>
    <xsl:when test="$paper.type = '4A0'">1682mm</xsl:when>
    <xsl:when test="$paper.type = '2A0'">1189mm</xsl:when>
    <xsl:when test="$paper.type = 'A0'">841mm</xsl:when>
    <xsl:when test="$paper.type = 'A1'">594mm</xsl:when>
    <xsl:when test="$paper.type = 'A2'">420mm</xsl:when>
    <xsl:when test="$paper.type = 'A3'">297mm</xsl:when>
    <xsl:when test="$paper.type = 'A4'">210mm</xsl:when>
    <xsl:when test="$paper.type = 'A5'">148mm</xsl:when>
    <xsl:when test="$paper.type = 'A6'">105mm</xsl:when>
    <xsl:when test="$paper.type = 'A7'">74mm</xsl:when>
    <xsl:when test="$paper.type = 'A8'">52mm</xsl:when>
    <xsl:when test="$paper.type = 'A9'">37mm</xsl:when>
    <xsl:when test="$paper.type = 'A10'">26mm</xsl:when>
    <xsl:when test="$paper.type = 'B0'">1000mm</xsl:when>
    <xsl:when test="$paper.type = 'B1'">707mm</xsl:when>
    <xsl:when test="$paper.type = 'B2'">500mm</xsl:when>
    <xsl:when test="$paper.type = 'B3'">353mm</xsl:when>
    <xsl:when test="$paper.type = 'B4'">250mm</xsl:when>
    <xsl:when test="$paper.type = 'B5'">176mm</xsl:when>
    <xsl:when test="$paper.type = 'B6'">125mm</xsl:when>
    <xsl:when test="$paper.type = 'B7'">88mm</xsl:when>
    <xsl:when test="$paper.type = 'B8'">62mm</xsl:when>
    <xsl:when test="$paper.type = 'B9'">44mm</xsl:when>
    <xsl:when test="$paper.type = 'B10'">31mm</xsl:when>
    <xsl:when test="$paper.type = 'C0'">917mm</xsl:when>
    <xsl:when test="$paper.type = 'C1'">648mm</xsl:when>
    <xsl:when test="$paper.type = 'C2'">458mm</xsl:when>
    <xsl:when test="$paper.type = 'C3'">324mm</xsl:when>
    <xsl:when test="$paper.type = 'C4'">229mm</xsl:when>
    <xsl:when test="$paper.type = 'C5'">162mm</xsl:when>
    <xsl:when test="$paper.type = 'C6'">114mm</xsl:when>
    <xsl:when test="$paper.type = 'C7'">81mm</xsl:when>
    <xsl:when test="$paper.type = 'C8'">57mm</xsl:when>
    <xsl:when test="$paper.type = 'C9'">40mm</xsl:when>
    <xsl:when test="$paper.type = 'C10'">28mm</xsl:when>
    <xsl:otherwise>8.5in</xsl:otherwise>
  </xsl:choose>
</xsl:param>

<!-- ============================================================ -->

<xsl:output method="xml" indent="no"/>

<xsl:key name="id" match="*" use="@id"/>

<!-- ============================================================ -->

<xsl:template match="*">
  <xsl:message>
    <xsl:value-of select="name(.)"/>
    <xsl:text> encountered</xsl:text>
    <xsl:if test="parent::*">
      <xsl:text> in </xsl:text>
      <xsl:value-of select="name(parent::*)"/>
    </xsl:if>
    <xsl:text>, but no template matches.</xsl:text>
  </xsl:message>
  <fo:block color="red">
    <xsl:text>&lt;</xsl:text>
    <xsl:value-of select="name(.)"/>
    <xsl:text>&gt;</xsl:text>
    <xsl:apply-templates/> 
    <xsl:text>&lt;/</xsl:text>
    <xsl:value-of select="name(.)"/>
    <xsl:text>&gt;</xsl:text>
  </fo:block>
</xsl:template>

<!-- ============================================================ -->

<xsl:template match="/">
  <fo:root font-family="{$body.font.family},{$symbol.font.family}"
	   font-size="10pt"
	   text-align="start"
	   line-height="normal"
	   font-selection-strategy="character-by-character"
	   line-height-shift-adjustment="disregard-shifts"
	   language="us-en">

    <xsl:if test="$xep.extensions != 0">
      <xsl:call-template name="xep-document-information"/>
    </xsl:if>

  <fo:layout-master-set>
    <!-- blank pages -->
    <fo:simple-page-master master-name="blank"
                           page-width="{$page.width}"
                           page-height="{$page.height}"
                           margin-top="{$page.margin.top}"
                           margin-bottom="{$page.margin.bottom}"
                           margin-left="{$page.margin.outer}"
                           margin-right="{$page.margin.inner}">
      <fo:region-body display-align="center"
                      margin-bottom="{$body.margin.bottom}"
                      margin-top="{$body.margin.top}"
		      region-name="blank-body"/>
      <fo:region-before region-name="xsl-region-before-blank"
                        extent="{$region.before.extent}"
                        display-align="before"/>
      <fo:region-after region-name="xsl-region-after-blank"
                       extent="{$region.after.extent}"
                        display-align="after"/>
    </fo:simple-page-master>

    <!-- body pages -->
    <fo:simple-page-master master-name="body-first"
                           page-width="{$page.width}"
                           page-height="{$page.height}"
                           margin-top="{$page.margin.top}"
                           margin-bottom="{$page.margin.bottom}"
                           margin-left="{$page.margin.inner}"
                           margin-right="{$page.margin.outer}">
      <fo:region-body margin-bottom="{$body.margin.bottom}"
                      margin-top="{$body.margin.top}"
                      column-gap="{$column.gap.body}"
                      column-count="{$column.count.body}">
      </fo:region-body>
      <fo:region-before region-name="xsl-region-before-first"
                        extent="{$region.before.extent}"
                        display-align="before"/>
      <fo:region-after region-name="xsl-region-after-first"
                       extent="{$region.after.extent}"
                       display-align="after"/>
    </fo:simple-page-master>

    <fo:simple-page-master master-name="body-odd"
                           page-width="{$page.width}"
                           page-height="{$page.height}"
                           margin-top="{$page.margin.top}"
                           margin-bottom="{$page.margin.bottom}"
                           margin-left="{$page.margin.inner}"
                           margin-right="{$page.margin.outer}">
      <fo:region-body margin-bottom="{$body.margin.bottom}"
                      margin-top="{$body.margin.top}"
                      column-gap="{$column.gap.body}"
                      column-count="{$column.count.body}">
      </fo:region-body>
      <fo:region-before region-name="xsl-region-before-odd"
                        extent="{$region.before.extent}"
                        display-align="before"/>
      <fo:region-after region-name="xsl-region-after-odd"
                       extent="{$region.after.extent}"
                       display-align="after"/>
    </fo:simple-page-master>

    <fo:simple-page-master master-name="body-even"
                           page-width="{$page.width}"
                           page-height="{$page.height}"
                           margin-top="{$page.margin.top}"
                           margin-bottom="{$page.margin.bottom}"
                           margin-left="{$page.margin.outer}"
                           margin-right="{$page.margin.inner}">
      <fo:region-body margin-bottom="{$body.margin.bottom}"
                      margin-top="{$body.margin.top}"
                      column-gap="{$column.gap.body}"
                      column-count="{$column.count.body}">
      </fo:region-body>
      <fo:region-before region-name="xsl-region-before-even"
                        extent="{$region.before.extent}"
                        display-align="before"/>
      <fo:region-after region-name="xsl-region-after-even"
                       extent="{$region.after.extent}"
                       display-align="after"/>
    </fo:simple-page-master>

    <!-- setup for body pages -->
    <fo:page-sequence-master master-name="body">
      <fo:repeatable-page-master-alternatives>
        <fo:conditional-page-master-reference master-reference="blank"
                                              blank-or-not-blank="blank"/>
        <fo:conditional-page-master-reference master-reference="body-first"
                                              page-position="first"/>
        <fo:conditional-page-master-reference master-reference="body-odd"
                                              odd-or-even="odd"/>
        <fo:conditional-page-master-reference master-reference="body-even"
                                              odd-or-even="even"/>
      </fo:repeatable-page-master-alternatives>
    </fo:page-sequence-master>

    </fo:layout-master-set>

    <xsl:if test="$xep.extensions != 0">
      <xsl:variable name="bookmarks">
	<xsl:apply-templates mode="xep.outline"/>
      </xsl:variable>
      <xsl:if test="string($bookmarks) != ''">
	<rx:outline xmlns:rx="http://www.renderx.com/XSL/Extensions">
	  <xsl:copy-of select="$bookmarks"/>
	</rx:outline>
      </xsl:if>
    </xsl:if>

    <fo:page-sequence hyphenate="{$hyphenate}"
		      master-reference="body"
		      force-page-count="end-on-even"
		      format="1"
		      initial-page-number="1"
		      hyphenation-character="-"
		      hyphenation-push-character-count="2"
		      hyphenation-remain-character-count="2">

      <fo:static-content flow-name="xsl-region-before-first">
	<fo:block/>
      </fo:static-content>
      
      <fo:static-content flow-name="xsl-region-before-odd">
	<fo:table table-layout="fixed" width="100%"
		  padding-bottom="2pt"
		  border-bottom-width="0.5pt"
		  border-bottom-style="solid"
		  border-bottom-color="black">

	  <fo:table-column column-number="1"
			   column-width="proportional-column-width(1)"/>
	  <fo:table-column column-number="2"
			   column-width="proportional-column-width(1)"/>

	  <fo:table-body>
	    <fo:table-row height="14pt">
	      <fo:table-cell text-align="left"
			     display-align="before"
			     relative-align="baseline">
		<fo:block>
		  <xsl:value-of select="//html:h1[1]"/>
		</fo:block>
	      </fo:table-cell>
	      <fo:table-cell text-align="right"
			     display-align="before"
			     relative-align="baseline">
		<fo:block>
		  <xsl:value-of select="//html:h2[1]"/>
		</fo:block>
	      </fo:table-cell>
	    </fo:table-row>
	  </fo:table-body>
	</fo:table>
      </fo:static-content>

      <fo:static-content flow-name="xsl-region-before-even">
	<fo:table table-layout="fixed" width="100%"
		  padding-bottom="2pt"
		  border-bottom-width="0.5pt"
		  border-bottom-style="solid"
		  border-bottom-color="black">

	  <fo:table-column column-number="1"
			   column-width="proportional-column-width(1)"/>
	  <fo:table-column column-number="2"
			   column-width="proportional-column-width(1)"/>

	  <fo:table-body>
	    <fo:table-row height="14pt">
	      <fo:table-cell text-align="left"
			     display-align="before"
			     relative-align="baseline">
		<fo:block>
		  <xsl:value-of select="//html:h2[1]"/>
		</fo:block>
	      </fo:table-cell>
	      <fo:table-cell text-align="right"
			     display-align="before"
			     relative-align="baseline">
		<fo:block>
		  <xsl:value-of select="//html:h1[1]"/>
		</fo:block>
	      </fo:table-cell>
	    </fo:table-row>
	  </fo:table-body>
	</fo:table>
      </fo:static-content>

      <fo:static-content flow-name="xsl-region-before-blank">
	<fo:block/>
      </fo:static-content>

      <fo:static-content flow-name="xsl-region-after-first">
	<fo:block/>
      </fo:static-content>
      
      <fo:static-content flow-name="xsl-region-after-odd">
	<fo:table table-layout="fixed" width="100%"
		  padding-top="2pt"
		  border-top-width="0.5pt"
		  border-top-style="solid"
		  border-top-color="black">

	  <fo:table-column column-number="1"
			   column-width="proportional-column-width(5)"/>
	  <fo:table-column column-number="2"
			   column-width="proportional-column-width(1)"/>

	  <fo:table-body>
	    <fo:table-row height="14pt">
	      <fo:table-cell text-align="left"
			     display-align="before"
			     relative-align="baseline">
		<fo:block>
		  <fo:retrieve-marker retrieve-class-name="section"
				      retrieve-position="first-including-carryover"
				      retrieve-boundary="page-sequence"/>
		</fo:block>
	      </fo:table-cell>
	      <fo:table-cell text-align="right"
			     display-align="before"
			     relative-align="baseline">
		<fo:block>
		  <fo:page-number/>
		</fo:block>
	      </fo:table-cell>
	    </fo:table-row>
	  </fo:table-body>
	</fo:table>
      </fo:static-content>

      <fo:static-content flow-name="xsl-region-after-even">
	<fo:table table-layout="fixed" width="100%"
		  padding-top="2pt"
		  border-top-width="0.5pt"
		  border-top-style="solid"
		  border-top-color="black">

	  <fo:table-column column-number="1"
			   column-width="proportional-column-width(1)"/>
	  <fo:table-column column-number="2"
			   column-width="proportional-column-width(5)"/>

	  <fo:table-body>
	    <fo:table-row height="14pt">
	      <fo:table-cell text-align="left"
			     display-align="before"
			     relative-align="baseline">
		<fo:block>
		  <fo:page-number/>
		</fo:block>
	      </fo:table-cell>
	      <fo:table-cell text-align="right"
			     display-align="before"
			     relative-align="baseline">
		<fo:block>
		  <fo:retrieve-marker retrieve-class-name="section"
				      retrieve-position="first-including-carryover"
				      retrieve-boundary="page-sequence"/>
		</fo:block>
	      </fo:table-cell>
	    </fo:table-row>
	  </fo:table-body>
	</fo:table>
      </fo:static-content>

      <fo:static-content flow-name="xsl-region-after-blank">
	<fo:block/>
      </fo:static-content>

      <fo:flow flow-name="xsl-region-body">
	<xsl:apply-templates/>
      </fo:flow>
    </fo:page-sequence>
  </fo:root>
</xsl:template>

<xsl:template match="html:html">
  <xsl:apply-templates select="html:body"/>
</xsl:template>

<xsl:template match="html:body">
  <fo:block>
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:p">
  <fo:block space-before="1em">
    <xsl:if test="@id">
      <xsl:attribute name="id">
	<xsl:value-of select="@id"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:div">
  <fo:block>
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:h1">
  <fo:block space-before="0.25in"
	    color="#00599C"
	    font-size="16pt"
	    font-family="{$title.font.family}"
	    keep-with-next="always"
	    id="{generate-id()}">
    <fo:marker marker-class-name="section">
      <xsl:apply-templates/>
    </fo:marker>
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:h2">
  <fo:block space-before="0.1in"
	    color="#00599C"
	    font-size="14pt"
	    font-family="{$title.font.family}"
	    keep-with-next="always"
	    id="{generate-id()}">
    <fo:marker marker-class-name="section">
      <xsl:apply-templates/>
    </fo:marker>
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:h3">
  <fo:block space-before="0.1in"
	    color="#00599C"
	    font-size="12pt"
	    font-family="{$title.font.family}"
	    keep-with-next="always"
	    id="{generate-id()}">
    <fo:marker marker-class-name="section">
      <xsl:apply-templates/>
    </fo:marker>
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:h4">
  <fo:block space-before="0.1in"
	    color="#00599C"
	    font-weight="bold"
	    font-family="{$title.font.family}"
	    keep-with-next="always"
	    id="{generate-id()}">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:h5">
  <fo:block space-before="0.1in"
	    color="#00599C"
	    font-weight="bold"
	    font-family="{$title.font.family}"
	    keep-with-next="always"
	    id="{generate-id()}">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:cite">
  <fo:inline font-style="italic">
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="html:em">
  <fo:inline font-style="italic">
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="html:strong">
  <fo:inline font-weight="bold">
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="html:code">
  <fo:inline font-family="{$monospace.font.family}">
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="html:span">
  <fo:inline>
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="html:dfn">
  <fo:inline>
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="html:acronym">
  <fo:inline>
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="html:sup">
  <fo:inline font-size="75%" baseline-shift="super">
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="html:a[@href]" priority="100">
  <xsl:choose>
    <xsl:when test="starts-with(@href, '#')">
      <fo:basic-link internal-destination="{substring-after(@href, '#')}">
	<xsl:choose>
	  <xsl:when test="contains(., '(§')">
	    <xsl:value-of select="substring-before(., ')')"/>
	    <xsl:text>, pg. </xsl:text>
	    <fo:page-number-citation ref-id="{substring-after(@href, '#')}"/>
	    <xsl:text>)</xsl:text>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:apply-templates/>
	  </xsl:otherwise>
	</xsl:choose>
      </fo:basic-link>
    </xsl:when>
    <xsl:otherwise>
      <fo:inline>
	<xsl:apply-templates/>
      </fo:inline>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="html:a">
  <fo:inline>
    <xsl:if test="@name">
      <xsl:attribute name="id">
	<xsl:value-of select="@name"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="html:img">
  <fo:external-graphic>
    <xsl:attribute name="content-width">
      <xsl:choose>
	<xsl:when test="@width">
	  <xsl:value-of select="@width"/>
	  <xsl:text>px</xsl:text>
	</xsl:when>
	<xsl:otherwise>6.5in</xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
    <xsl:attribute name="src">
      <xsl:call-template name="fo-external-image">
	<xsl:with-param name="filename" select="@src"/>
      </xsl:call-template>
    </xsl:attribute>
  </fo:external-graphic>
</xsl:template>

<xsl:template match="html:hr">
  <fo:block border-bottom-color="black"
	    border-bottom-width="1pt"
	    border-bottom-style="solid"/>
</xsl:template>

<xsl:template match="html:dl">
  <fo:block space-before="8pt" keep-with-previous="always">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:dt">
  <fo:block font-family="{$title.font.family}"
	    keep-with-next="always">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:dt[preceding-sibling::html:dt]">
  <fo:block font-family="{$title.font.family}"
	    keep-with-next="always"
	    space-before="8pt">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:dd">
  <fo:block margin-left="0.5in">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:ul">
  <fo:list-block provisional-label-separation="0.2em"
		 provisional-distance-between-starts="1em"
		 space-before="8pt" margin-left="8pt">
    <xsl:apply-templates select="html:li"/>
  </fo:list-block>
</xsl:template>

<xsl:template match="html:ul/html:li">
  <fo:list-item>
    <xsl:if test="not(preceding-sibling::html:li)">
      <xsl:attribute name="keep-with-next">always</xsl:attribute>
    </xsl:if>
    <xsl:if test="not(following-sibling::html:li)">
      <xsl:attribute name="keep-with-previous">always</xsl:attribute>
    </xsl:if>

    <fo:list-item-label end-indent="label-end()">
      <fo:block>
	<xsl:text>•</xsl:text>
      </fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <fo:block>
	<xsl:apply-templates/>
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>

<xsl:template match="html:ol">
  <fo:list-block provisional-label-separation="0.2em"
		 provisional-distance-between-starts="1.25em"
		 space-before="8pt" margin-left="8pt">
    <xsl:apply-templates select="html:li"/>
  </fo:list-block>
</xsl:template>

<xsl:template match="html:ol/html:li">
  <fo:list-item>
    <xsl:if test="not(preceding-sibling::html:li)">
      <xsl:attribute name="keep-with-next">always</xsl:attribute>
    </xsl:if>
    <xsl:if test="not(following-sibling::html:li)">
      <xsl:attribute name="keep-with-previous">always</xsl:attribute>
    </xsl:if>

    <fo:list-item-label end-indent="label-end()">
      <fo:block>
	<xsl:number from="html:ol" format="1." count="html:li"/>
      </fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <fo:block>
	<xsl:apply-templates/>
      </fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>

<!-- ============================================================ -->
<!-- Document header -->

<xsl:template match="html:h1[ancestor::html:div[@class='head']]" priority="100">
  <fo:block space-before="0.25in"
	    color="#00599C"
	    font-family="{$title.font.family}"
	    font-size="18pt"
	    id="{generate-id()}">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:h2[ancestor::html:div[@class='head']]" priority="100">
  <fo:block space-before="8pt"
	    color="#00599C"
	    font-family="{$title.font.family}"
	    font-size="14pt">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:dl[ancestor::html:div[@class='head']]" priority="100">
  <fo:block space-before="8pt">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:dt[ancestor::html:div[@class='head']]" priority="200">
  <fo:block font-family="{$title.font.family}"
	    keep-with-next="always">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="html:p[@class='copyright' and
		            ancestor::html:div[@class='head']]" priority="100">
  <fo:block space-before="8pt"
	    space-after="8pt"
	    font-size="75%">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<!-- ============================================================ -->
<!-- Table of Contents -->

<xsl:template match="html:div[html:h2='Table of Contents']">
  <fo:block break-before="page" break-after="page">
    <xsl:apply-templates select="html:h2"/>
    <xsl:apply-templates select="html:ul[@class='toc']" mode="toc"/>
    <xsl:apply-templates select="html:div"/>
  </fo:block>
</xsl:template>

<xsl:template match="html:ul" mode="toc">
  <xsl:apply-templates select=".//html:li" mode="toc"/>
</xsl:template>

<xsl:template match="html:li[ancestor::html:li]" mode="toc" priority="100">
  <xsl:variable name="id" select="substring-after(html:a[1]/@href, '#')"/>

  <fo:block text-align-last="justify"
	    end-indent="2pc"
	    last-line-end-indent="-2pc"
	    margin-left="1em">
    <fo:inline keep-together.within-line="always">
      <fo:basic-link internal-destination="{$id}">
	<xsl:value-of select="html:a[1]"/>
      </fo:basic-link>
      <xsl:text> </xsl:text>
      <fo:leader leader-pattern="dots"
		 keep-with-next.within-line="always"/>
      <xsl:text> </xsl:text>
      <fo:basic-link internal-destination="{$id}">
	<fo:page-number-citation ref-id="{$id}"/>
      </fo:basic-link>
    </fo:inline>
  </fo:block>
</xsl:template>

<xsl:template match="html:li" mode="toc">
  <xsl:variable name="id" select="substring-after(html:a[1]/@href, '#')"/>

  <fo:block space-before="8pt"
	    text-align-last="justify"
	    end-indent="2pc"
	    last-line-end-indent="-2pc">
    <fo:inline keep-together.within-line="always">
      <fo:basic-link internal-destination="{$id}">
	<xsl:value-of select="html:a[1]"/>
      </fo:basic-link>
      <xsl:text> </xsl:text>
      <fo:leader leader-pattern="dots"
		 keep-with-next.within-line="always"/>
      <xsl:text> </xsl:text>
      <fo:basic-link internal-destination="{$id}">
	<fo:page-number-citation ref-id="{$id}"/>
      </fo:basic-link>
    </fo:inline>
  </fo:block>
</xsl:template>

<!-- ============================================================ -->
<!-- Story -->

<xsl:template match="html:div[@class='boxedtext']" priority="200">
  <fo:block border="0.25pt solid #7F7F7F" keep-together.within-column="always"
	    margin-left="8pt" margin-right="8pt" margin-top="8pt">
    <xsl:apply-templates select="html:p[1]" mode="boxtitle"/>
    <fo:block margin-left="8pt" margin-right="8pt" margin-bottom="4pt">
      <xsl:apply-templates select="html:p[1]/following-sibling::*"/>
    </fo:block>
  </fo:block>
</xsl:template>

<xsl:template match="html:p" mode="boxtitle">
  <xsl:variable name="bgcolor">
    <xsl:choose>
      <xsl:when test="html:span/@class='principlelab'">
	<xsl:text>#f7ebd7</xsl:text>
      </xsl:when>
      <xsl:when test="html:span/@class='constraintlab'">
	<xsl:text>#becece</xsl:text>
      </xsl:when>
      <xsl:when test="html:span/@class='propertylab'">
	<xsl:text>#f7ebd7</xsl:text>
      </xsl:when>
      <xsl:when test="html:span/@class='practicelab'">
	<xsl:text>#dfffff</xsl:text>
      </xsl:when>
      <xsl:when test="html:span/@class='storylab'">
	<xsl:text>#005a9c</xsl:text>
      </xsl:when>
      <xsl:otherwise>black</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="fgcolor">
    <xsl:choose>
      <xsl:when test="html:span/@class='principlelab'">
	<xsl:text>black</xsl:text>
      </xsl:when>
      <xsl:when test="html:span/@class='constraintlab'">
	<xsl:text>black</xsl:text>
      </xsl:when>
      <xsl:when test="html:span/@class='propertylab'">
	<xsl:text>black</xsl:text>
      </xsl:when>
      <xsl:when test="html:span/@class='practicelab'">
	<xsl:text>black</xsl:text>
      </xsl:when>
      <xsl:when test="html:span/@class='storylab'">
	<xsl:text>white</xsl:text>
      </xsl:when>
      <xsl:otherwise>white</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <fo:block margin-top="0.5pt" padding-top="1pt"
	    margin-left="0.5pt" padding-left="8pt"
	    margin-right="0.5pt"
	    background-color="{$bgcolor}" color="{$fgcolor}">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>


<!-- ============================================================ -->

<xsl:template name="fo-external-image">
  <xsl:param name="filename"/>
  <xsl:value-of select="concat('url(', $filename, ')')"/>
</xsl:template>

<xsl:template name="gentext">
  <xsl:param name="key"/>
  <xsl:value-of select="$key"/>
</xsl:template>

<!-- ============================================================ -->

<xsl:template name="xep-document-information">
  <rx:meta-info>
    <xsl:element name="rx:meta-field">
      <xsl:attribute name="name">title</xsl:attribute>
      <xsl:attribute name="value">
	<xsl:value-of select="/html:html/html:head/html:title"/>
      </xsl:attribute>
    </xsl:element>
  </rx:meta-info>
</xsl:template>

<!-- PDF Bookmarks -->

<xsl:template match="*" mode="xep.outline">
  <xsl:apply-templates select="*" mode="xep.outline"/>
</xsl:template>

<xsl:template match="html:h1[parent::html:div[@class='head']]"
	      mode="xep.outline">

  <xsl:variable name="id" select="generate-id()"/>
  <xsl:variable name="label">
    <xsl:apply-templates/>
  </xsl:variable>

  <rx:bookmark internal-destination="{$id}">
    <rx:bookmark-label>
      <xsl:value-of select="$label"/>
    </rx:bookmark-label>
  </rx:bookmark>
</xsl:template>

<xsl:template match="html:div[@class='section']"
	      mode="xep.outline">

  <xsl:variable name="title"
		select="(html:h1|html:h2|html:h3|html:h4|html:h5)[1]"/>
  <xsl:variable name="id" select="generate-id($title)"/>
  <xsl:variable name="label">
    <xsl:apply-templates select="$title/node()"/>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="ancestor::html:div[@class='head']"><!-- nop --></xsl:when>
    <xsl:when test="$title/@class='notoc'"><!-- nop --></xsl:when>
    <xsl:when test="not($title)">
      <xsl:apply-templates select="*" mode="xep.outline"/>
    </xsl:when>
    <xsl:otherwise>
      <rx:bookmark internal-destination="{$id}">
	<rx:bookmark-label>
	  <xsl:value-of select="$label"/>
	</rx:bookmark-label>
	<xsl:apply-templates select="*" mode="xep.outline"/>
      </rx:bookmark>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
