<?xml version='1.0'?> <!-- -*- xml -*- -->

<!--
  ancestors.xsl - Ancestor report stylesheet for GRAMPS
  Copyright (C) 2003  Tim Waugh <tim@cyberelk.net>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  -->

<!-- $Id: ancestors.xsl,v 1.25 2003/06/08 18:18:41 tim Exp $ -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:date="http://exslt.org/dates-and-times"
                exclude-result-prefixes="date"
                version='1.0'>

<xsl:output method="xml" encoding="UTF-8"
            indent="yes"
            doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
            doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<!-- Set this variable to the ID of the individual for the report -->
<xsl:variable name="ancestors-of" select="'I0001'"/>

<!-- Tweakable parameters -->
<xsl:variable name="img.width" select="'100'"/>
<!-- No more tweakable parameters -->

<xsl:template match="description">
  <xsl:text>: </xsl:text>
  <xsl:value-of select="string(.)"/>
</xsl:template>

<xsl:template match="event/note">
  <xsl:text> (</xsl:text>
  <xsl:value-of select="string(.)"/>
  <xsl:text>)</xsl:text>
</xsl:template>

<xsl:template match="family/note">
  <xsl:text>  </xsl:text>
  <xsl:value-of select="string(.)"/>
</xsl:template>

<xsl:template match="place">
  <xsl:variable name="ref" select="@ref"/>
  <xsl:text> in </xsl:text>
  <xsl:value-of select="//placeobj[@id=$ref]/@title"/>
</xsl:template>

<xsl:template name="nth">
  <xsl:param name="n" select="''"/>
  <xsl:variable name="lastdig" select="substring($n,string-length($n))"/>
  <xsl:choose>
    <xsl:when test="$lastdig = '1' and $n != '11'">
      <xsl:text>st</xsl:text>
    </xsl:when>
    <xsl:when test="$lastdig = '3'">
      <xsl:text>rd</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>th</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="datestr">
  <xsl:text> </xsl:text>
  <xsl:value-of select="@val"/>
</xsl:template>

<xsl:template name="long-date">
  <xsl:param name="date" select="''"/>
  <xsl:variable name="yr" select="substring-before($date,'-')"/>
  <xsl:variable name="mndy" select="substring-after($date,concat($yr,'-'))"/>
  <xsl:variable name="dy" select="substring-after($mndy,'-')"/>
  <xsl:choose>
    <xsl:when test="not ($dy = '')">
      <xsl:text> on </xsl:text>
      <xsl:value-of select="number($dy)"/>
      <xsl:call-template name="nth">
        <xsl:with-param name="n" select="$dy"/>
      </xsl:call-template>
      <xsl:text> </xsl:text>
      <xsl:value-of select="date:month-name(translate($date,'?','2'))"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="$yr"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text> in </xsl:text>
      <xsl:choose>
        <xsl:when test="$mndy">
          <xsl:value-of select="date:month-name($date)"/>
          <xsl:text> </xsl:text>
          <xsl:value-of select="$yr"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$date"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="event[@type='Death']/dateval">
  <xsl:variable name="died" select="date:year(@val)"/>
  <xsl:variable name="born"
                select="ancestor::person/event[@type='Birth']/dateval/@val"/>

  <xsl:choose>
    <xsl:when test="$born != ''">
      <xsl:variable name="age" select="($died - date:year($born))"/>
      <xsl:choose>
        <xsl:when test="string($age) = 'NaN'">
          <xsl:call-template name="long-date">
            <xsl:with-param name="date" select="@val"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="$age &lt; 6">
          <xsl:text> an infant</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text> at </xsl:text>
          <xsl:value-of select="$age"/>
          <xsl:call-template name="long-date">
            <xsl:with-param name="date" select="@val"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="long-date">
        <xsl:with-param name="date" select="@val"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="dateval">
  <xsl:call-template name="long-date">
    <xsl:with-param name="date" select="@val"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="son-or-daughter">
  <xsl:param name="id" select="''"/>
  <xsl:choose>
    <xsl:when test="string(/database/people/person[@id=$id]/gender) = 'F'">
      <xsl:text>daughter</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>son</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="Pronoun">
  <xsl:param name="id" select="''"/>
  <xsl:choose>
    <xsl:when test="string(/database/people/person[@id=$id]/gender) = 'F'">
      <xsl:text>She</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>He</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="long-born-died">
  <xsl:param name="id" select="''"/>
  <xsl:for-each select="/database/people/person[@id=$id]">
    <xsl:if test="event[@type='Birth']">
      <xsl:text>, born</xsl:text>
      <xsl:apply-templates select="event[@type='Birth']/dateval|
                                   event[@type='Birth']/datestr"/>
      <xsl:apply-templates select="event[@type='Birth']/place"/>
      <xsl:apply-templates select="event[@type='Birth']/note"/>
    </xsl:if>

    <xsl:if test="event[@type='Death']">
      <xsl:choose>
        <xsl:when test="event[@type='Birth']">
          <xsl:text>; </xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>, </xsl:text>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>died</xsl:text>
      <xsl:apply-templates select="event[@type='Death']/dateval|
                                   event[@type='Death']/datestr"/>
      <xsl:apply-templates select="event[@type='Death']/place"/>
      <xsl:apply-templates select="event[@type='Death']/note"/>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<xsl:template name="short-occupation">
  <xsl:param name="id" select="''"/>
  <xsl:for-each select="/database/people/person[@id=$id]">
    <xsl:if test="count(event[@type='Occupation']) = 1">
      <xsl:text> (</xsl:text>
      <xsl:value-of select="event[@type='Occupation']/description"/>
      <xsl:text>)</xsl:text>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

<xsl:template name="title">
  <xsl:choose>
    <xsl:when test="name/title">
      <xsl:value-of select="name/title"/>
    </xsl:when>
    <xsl:when test="gender = 'F'">
      <xsl:choose>
        <xsl:when test="name/@type = 'Married Name'">
          <xsl:text>Mrs.</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>Miss</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>Mr.</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="person-name">
  <xsl:param name="id" select="''"/>
  <xsl:for-each select="/database/people/person[@id=$id]">
    <xsl:variable name="first" select="name/first"/>
    <xsl:variable name="last" select="name/last"/>
    <xsl:choose>
      <xsl:when test="translate($first, '?', '') = ''">
        <xsl:call-template name="title" select="."/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$first"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="translate($last, '?', '') = ''">
        <xsl:if test="translate($first, '?', '') = ''">
          <xsl:text> (unknown)</xsl:text>
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text> </xsl:text>
        <xsl:value-of select="$last"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="name/@type != 'Birth Name'">
      <xsl:text> (</xsl:text>
      <xsl:value-of select="name/@type"/>
      <xsl:text>)</xsl:text>
    </xsl:if>
   </xsl:for-each>
</xsl:template>

<xsl:template name="first-name-or-nick">
  <xsl:param name="id" select="''"/>
  <xsl:variable name="person" select="/database/people/person[@id=$id]"/>

  <xsl:choose>
    <xsl:when test="$person/nick">
      <xsl:value-of select="$person/nick"/>
    </xsl:when>
    <xsl:when test="contains($person/name/first,' ')">
      <xsl:value-of select="substring-before($person/name/first,' ')"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$person/name/first"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="family">
  <xsl:param name="id" select="''"/>
  <xsl:param name="already-described" select="/nothing"/>

  <xsl:variable name="family" select="/database/families/family[@id=$id]"/>
  <xsl:variable name="father-described"
                select="$family/father[@ref = $already-described/@ref]"/>

  <xsl:variable name="children">
    <xsl:for-each select="$family/child">
      <xsl:variable name="this" select="@ref"/>
      <xsl:call-template name="person">
        <xsl:with-param name="id" select="$this"/>
        <xsl:with-param name="suppress-children" select="'yes'"/>
        <xsl:with-param name="short-form"
                        select="($this = $ancestors-of) or
                                ($this = $already-described/@ref)"/>
        <xsl:with-param name="needs-name" select="yes"/>
        <xsl:with-param name="from-family" select="$family"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:variable>

  <xsl:call-template name="person">
    <xsl:with-param name="id" select="$family/father/@ref"/>
    <xsl:with-param name="short-form" select="$father-described"/>
    <xsl:with-param name="already-described"
                    select="$already-described"/>
    <xsl:with-param name="needs-name" select="not ($family/mother)"/>
    <xsl:with-param name="from-family" select="$family"/>
  </xsl:call-template>

  <xsl:variable name="mother-described"
                select="$family/mother[@ref = $already-described/@ref]"/>
  <xsl:call-template name="person">
    <xsl:with-param name="id" select="$family/mother/@ref"/>
    <xsl:with-param name="short-form" select="$mother-described"/>
    <xsl:with-param name="already-described"
                    select="$already-described"/>
    <xsl:with-param name="needs-name" select="not ($family/father)"/>
    <xsl:with-param name="from-family" select="$family"/>
  </xsl:call-template>

  <xsl:if test="$children != ''">
    <div class="Children">
      <p class="ChildTitle">
        <xsl:text>Their children:</xsl:text>
      </p>

      <xsl:copy-of select="$children"/>
    </div>
  </xsl:if>
</xsl:template>

<xsl:template name="person">
  <xsl:param name="id" select="''"/>
  <xsl:param name="suppress-children" select="''"/>
  <xsl:param name="short-form" select="''"/>
  <xsl:param name="already-described" select="/nothing"/>
  <xsl:param name="needs-name" select="''"/>
  <xsl:param name="from-family" select="''"/>

  <xsl:variable name="name">
    <xsl:call-template name="person-name">
      <xsl:with-param name="id" select="$id"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:if test="not ($name = '')">
    <xsl:variable name="person" select="/database/people/person[@id=$id]"/>
    <xsl:variable name="photo"
                  select="$person/objref[not (preceding-sibling::objref)]"/>

    <xsl:variable name="img">
      <xsl:variable name="object"
                    select="/database/objects/object[@id=$photo/@ref]"/>
      <xsl:choose>
        <xsl:when test="substring-before ($object/@mime,'/') = 'image'">
          <img src="{$object/@src}" width="{$img.width}"
               alt="Photo"/>
        </xsl:when>
        <xsl:otherwise>
          <ul class="NoPicture"><li/></ul>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="bits">
      <xsl:call-template name="short-occupation">
        <xsl:with-param name="id" select="$id"/>
      </xsl:call-template>
      <xsl:call-template name="long-born-died">
        <xsl:with-param name="id" select="$id"/>
      </xsl:call-template>
      <xsl:choose>
        <xsl:when test="$suppress-children = ''">
          <xsl:call-template name="parents-of">
            <xsl:with-param name="id" select="$id"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>.</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:call-template name="married-whom">
        <xsl:with-param name="id" select="$id"/>
        <xsl:with-param name="suppress-children" select="$suppress-children"/>
      </xsl:call-template>
      <xsl:call-template name="inline-notes">
        <xsl:with-param name="id" select="$id"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="longnotes">
      <xsl:call-template name="long-notes">
        <xsl:with-param name="id" select="$id"/>
        <xsl:with-param name="suppress-children" select="$suppress-children"/>
        <xsl:with-param name="already-described" select="$already-described"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:if test="$bits != '.' or $longnotes != '' or
                  $img != '' or $suppress-children != '' or
                  $needs-name">

      <!-- We have something to say about this person. -->
      <table width="100%">
        <tr>
          <td width="{$img.width}">
            <xsl:copy-of select="$img"/>
          </td>
          <td>
            <p class="Entry">
              <xsl:value-of select="$name"/>
              <xsl:choose>
                <xsl:when test="$short-form">
                  <xsl:text> (mentioned above).</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="$bits"/>
                </xsl:otherwise>
              </xsl:choose>
            </p>
          </td>

          <xsl:for-each select="$person/parentin">
            <xsl:variable name="ref" select="@ref"/>
            <xsl:variable name="family"
                          select="/database/families/family[@id=$ref]"/>
            <xsl:for-each select="$family/father|$family/mother">
              <xsl:variable name="ref" select="@ref"/>
              <xsl:for-each select="/database/people/person[@id=$ref]">
                <xsl:variable name="partner" select="."/>
                <xsl:if test="@id != $id and
                              ($suppress-children != '' or
                               (@id != $from-family/father/@ref and
                                @id != $from-family/mother/@ref))">
                  <xsl:variable name="obj-partner"
                       select="$partner/objref[not (preceding-sibling::objref)
                                               and not (@ref = $photo/@ref)]
                                              /@ref"/>
                  <xsl:variable name="object"
                       select="/database/objects/object[@id=$obj-partner]"/>

                  <xsl:if test="substring-before($object/@mime,'/') = 'image'">
                    <td>
                      <img src="{$object/@src}" width="{$img.width}"
                           alt="Partner's photo"/>
                    </td>
                  </xsl:if>
                </xsl:if>
              </xsl:for-each>
            </xsl:for-each>
          </xsl:for-each>
        </tr>
      </table>

      <xsl:if test="not($short-form)">
        <xsl:copy-of select="$longnotes"/>
      </xsl:if>
    </xsl:if>
  </xsl:if>
</xsl:template>

<xsl:template name="long-details">
  <xsl:param name="notes" select="''"/>

  <p class="Details">
    <xsl:value-of select="substring-before($notes,'&#10;')"/>
  </p>

  <xsl:if test="substring-after($notes,'&#10;')">
    <xsl:call-template name="long-details">
      <xsl:with-param name="notes" select="substring-after($notes,'&#10;')"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="long-notes">
  <xsl:param name="id" select="''"/>
  <xsl:param name="suppres-children" select="''"/>
  <xsl:param name="already-described" select="/nothing"/>

  <xsl:variable name="person" select="/database/people/person[@id=$id]"/>
  <xsl:variable name="notes" select="$person/note"/>
  <xsl:if test="not ($notes = '') and contains($notes, '&#10;')">
    <xsl:call-template name="long-details">
      <xsl:with-param name="notes" select="$notes"/>
    </xsl:call-template>
  </xsl:if>

  <xsl:variable name="events"
                select="$person/event[@type='Baptism' or @type='Education' or
                                      @type='Occupation' or
                                      @type='Military Service' or
                                      @type='Burial']"/>
  <xsl:if test='$events'>
    <p class="SubEntry">
      <xsl:text>More about </xsl:text>
      <xsl:call-template name="first-name-or-nick">
        <xsl:with-param name="id" select="$person/@id"/>
      </xsl:call-template>
      <xsl:text>:</xsl:text>
    </p>

    <xsl:for-each select="$events">
      <p class="Details">
        <xsl:value-of select="@type"/>
        <xsl:apply-templates select="description"/>
        <xsl:apply-templates select="dateval"/>
        <xsl:apply-templates select="place"/>
        <xsl:apply-templates select="note"/>
      </p>
    </xsl:for-each>
  </xsl:if>
</xsl:template>

<xsl:template name="inline-notes">
  <xsl:param name="id" select="''"/>
  <xsl:variable name="notes" select="/database/people/person[@id=$id]/note"/>
  <xsl:if test="not ($notes = '' or contains($notes, '&#10;'))">
    <xsl:text>  </xsl:text>
    <xsl:value-of select="$notes"/>
  </xsl:if>
</xsl:template>

<xsl:template name="married-whom">
  <xsl:param name="id" select="''"/>
  <xsl:param name="suppress-children" select="''"/>

  <xsl:variable name="pronoun">
    <xsl:text>  </xsl:text>
    <xsl:call-template name="Pronoun">
      <xsl:with-param name="id" select="$id"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="person" select="/database/people/person[@id=$id]"/>
  <xsl:for-each select="$person/parentin">
    <xsl:variable name="ref" select="@ref"/>
    <xsl:variable name="family" select="/database/families/family[@id=$ref]"/>
    <xsl:for-each select="/database/people/person[@id=$family/mother/@ref or
                                                  @id=$family/father/@ref]">
      <xsl:if test="not(@id = $id)">
        <xsl:variable name="children">
          <xsl:if test="$suppress-children != ''">
            <xsl:variable name="child-count" select="count($family/child)"/>
            <xsl:if test="$child-count &gt; 0">
              <xsl:text>, and they had </xsl:text>
              <xsl:value-of select="$child-count"/>
              <xsl:choose>
                <xsl:when test="$child-count = 1">
                  <xsl:text> child named </xsl:text>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:text> children: </xsl:text>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:for-each select="$family/child">
                <xsl:call-template name="first-name-or-nick">
                  <xsl:with-param name="id" select="@ref"/>
                </xsl:call-template>
                <xsl:choose>
                  <xsl:when test="following-sibling::child[following-sibling::child]">
                    <xsl:text>, </xsl:text>
                  </xsl:when>
                  <xsl:when test="following-sibling::child">
                    <xsl:text> and </xsl:text>
                  </xsl:when>
                </xsl:choose>
              </xsl:for-each>
            </xsl:if>
          </xsl:if>
        </xsl:variable>

        <xsl:choose>
          <xsl:when test="$person/parentin[@ref=$family/@id and preceding-sibling::parentin]">
            <xsl:value-of select="$pronoun"/>
            <xsl:text> later married </xsl:text>
            <xsl:call-template name="person-name">
              <xsl:with-param name="id" select="@id"/>
            </xsl:call-template>
            <xsl:apply-templates
                 select="$family/event[@type='Marriage']/dateval"/>
            <xsl:apply-templates
                 select="$family/event[@type='Marriage']/datestr"/>
            <xsl:apply-templates
                 select="$family/event[@type='Marriage']/place"/>
            <xsl:apply-templates
                 select="$family/event[@type='Marriage']/note"/>
            <xsl:value-of select="$children"/>
            <xsl:text>.</xsl:text>
          </xsl:when>
          <xsl:when test="$suppress-children != '' or @id=$family/mother/@ref">
            <xsl:value-of select="$pronoun"/>
            <xsl:text> married </xsl:text>
            <xsl:call-template name="person-name">
              <xsl:with-param name="id" select="@id"/>
            </xsl:call-template>
            <xsl:apply-templates
                 select="$family/event[@type='Marriage']/dateval"/>
            <xsl:apply-templates
                 select="$family/event[@type='Marriage']/datestr"/>
            <xsl:apply-templates
                 select="$family/event[@type='Marriage']/place"/>
            <xsl:apply-templates
                 select="$family/event[@type='Marriage']/note"/>
            <xsl:value-of select="$children"/>
            <xsl:text>.</xsl:text>
          </xsl:when>
        </xsl:choose>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

<xsl:template name="parents-of">
  <xsl:param name="id" select="''"/>

  <xsl:variable name="pronoun">
    <xsl:text>.  </xsl:text>
    <xsl:call-template name="Pronoun">
      <xsl:with-param name="id" select="$id"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="child">
    <xsl:call-template name="son-or-daughter">
      <xsl:with-param name="id" select="$id"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="childof"
                select="concat($pronoun,' is the ',$child,' of ')"/>
  <xsl:for-each select="/database/people/person[@id=$id]/childof">
    <xsl:variable name="family" select="@ref"/>
    <xsl:variable name="fathername">
      <xsl:call-template name="person-name">
        <xsl:with-param name="id" select="/database/families/family[@id=$family]/father/@ref"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="mothername">
      <xsl:call-template name="person-name">
        <xsl:with-param name="id" select="/database/families/family[@id=$family]/mother/@ref"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$fathername = '' and $mothername = ''"/>
      <xsl:when test="$fathername = ''">
        <xsl:value-of select="concat($childof,$mothername)"/>
      </xsl:when>
      <xsl:when test="$mothername = ''">
        <xsl:value-of select="concat($childof,$fathername)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($childof,$fathername)"/>
        <xsl:text> and </xsl:text>
        <xsl:value-of select="$mothername"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:for-each>
  <xsl:text>.</xsl:text>
</xsl:template>

<xsl:template name="generation">
  <xsl:param name="generations" select="'10'"/>
  <xsl:param name="thisgen" select="'2'"/>
  <xsl:param name="families" select="''"/>
  <xsl:param name="already-described" select="/nothing"/>

  <xsl:if test="$generations > 0 and $families">
    <xsl:variable name="people">
      <xsl:for-each select="$families">
        <xsl:call-template name="family">
          <xsl:with-param name="id" select="@id"/>
          <xsl:with-param name="already-described"
                          select="$already-described"/>
        </xsl:call-template>
      </xsl:for-each>
    </xsl:variable>

    <xsl:if test="not ($people = '')">
      <h2>Generation <xsl:value-of select="$thisgen"/></h2>
      <xsl:copy-of select="$people"/>
    </xsl:if>

    <xsl:variable name="fathers"
                  select="/database/people/person[@id=$families/father/@ref]"/>
    <xsl:variable name="mothers"
                  select="/database/people/person[@id=$families/mother/@ref]"/>
    <xsl:variable name="parents" select="$fathers | $mothers"/>
    <xsl:call-template name="generation">
      <xsl:with-param name="generations" select="$generations - 1"/>
      <xsl:with-param name="thisgen" select="$thisgen + 1"/>
      <xsl:with-param name="families" select="/database/families/family[@id=$parents/childof/@ref]"/>
      <xsl:with-param name="already-described"
                      select="$already-described |
                              $families/father |
                              $families/mother"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="head">
  <head>
    <title>Ancestors report</title>
    <style type="text/css">
.Details {
        font-size: 10pt; color: #000000;
        text-align: left; text-indent: 0.00cm;
        margin-right: 0.00cm; margin-left: 1.00cm;
        border-top:none; border-bottom:none;
        border-left:none; border-right:none;
        font-family:"Times New Roman","Times","serif";
}
.Endnotes {
        font-size: 12pt; color: #000000;
        text-align: left; text-indent: 0.00cm;
        margin-right: 0.00cm; margin-left: 0.00cm;
        border-top:none; border-bottom:none;
        border-left:none; border-right:none;
        font-family:"Times New Roman","Times","serif";
}
.Children {
        font-size: 10pt; color: #000000;
        text-align: left; text-indent: 0.00cm;
        margin-right: 0.00cm; margin-left: 1.00cm;
        border-style:solid; border-width:thin;
	padding: 5px; margin-bottom: 0.50cm;
        font-family:"Times New Roman","Times","serif";
}
.SubEntry {
        font-size: 12pt; color: #000000;
        text-align: left; text-indent: 0.00cm;
        margin-right: 0.00cm; margin-left: 1.00cm;
        border-top:none; border-bottom:none;
        border-left:none; border-right:none;
        font-family:"Times New Roman","Times","serif";
}
.Generation {
        font-size: 14pt; color: #000000;
        text-align: center; text-indent: 0.00cm;
        margin-right: 0.00cm; margin-left: 0.00cm;
        border-top:none; border-bottom:none;
        border-left:none; border-right:none;
        font-style:italic; font-family:"Helvetica","Arial","sans-serif";
}
.Entry {
        font-size: 12pt; color: #000000;
        text-align: left; text-indent: 0.00cm;
        margin-right: 0.00cm; margin-left: 1.00cm;
        border-top:none; border-bottom:none;
        border-left:none; border-right:none;
        font-family:"Times New Roman","Times","serif";
}
.Title {
        font-size: 16pt; color: #000000;
        text-align: center; text-indent: 0.00cm;
        margin-right: 0.00cm; margin-left: 0.00cm;
        border-top:none; border-bottom:none;
        border-left:none; border-right:none;
        font-style:italic; font-weight:bold; font-family:"Helvetica","Arial","sans-serif";
}
ul.NoPicture {
        padding:50;
}
    </style>
  </head>
</xsl:template>

<xsl:template match="/">
  <xsl:variable name="id" select="$ancestors-of"/>
  <html>
    <xsl:call-template name="head"/>
    <body>
      <h2>Generation 1</h2>
      <xsl:call-template name="person">
        <xsl:with-param name="id" select="$id"/>
        <xsl:with-param name="suppress-children" select="'yes'"/>
        <xsl:with-param name="from-family" select="/nothing"/>
      </xsl:call-template>
      <xsl:variable name="person" select="/database/people/person[@id=$id]"/>
      <xsl:variable name="family" select="/database/families/family[@id=$person/childof/@ref]"/>
      <xsl:variable name="parents" select="/database/people/person[@id=$family/child/@ref]"/>
      <xsl:variable name="families" select="/database/families/family[@id = $parents/childof/@ref]"/>
      <xsl:call-template name="generation">
        <xsl:with-param name="families" select="$families"/>
      </xsl:call-template>
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>

