<?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
  -->

<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="text" indent="no"/>

<!-- User serviceable parts -->
<xsl:param name="individual" select="'I25'"/>
<xsl:param name="days-warning" select="32"/>
<xsl:param name="threshold-max" select="120"/> <!-- years old -->

<!-- No user serviceable parts below. -->
<xsl:variable name="today" select="date:day-in-year()"/>
<xsl:variable name="days-this-year">
  <xsl:choose>
    <xsl:when test="date:leap-year()">366</xsl:when>
    <xsl:otherwise>365</xsl:otherwise>
  </xsl:choose>
</xsl:variable>
<xsl:template match="person">
  <xsl:if test="event[@type='Birth']/dateval and not (event[@type='Death'])">
    <xsl:variable name="birthday" select="event[@type='Birth']/dateval/@val"/>
    <xsl:variable name="daydiff" select="date:day-in-year($birthday)-$today"/>
    <xsl:if test="($daydiff &gt; 0 and $daydiff &lt;= $days-warning) or
                  $daydiff &lt; ($days-warning - $days-this-year)">
      <xsl:variable name="age">
        <xsl:variable name="carry-year">
          <xsl:choose>
            <xsl:when test="$daydiff &lt; 0">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:variable name="base-age"
             select="date:year() - date:year($birthday)"/>
        <xsl:value-of select="$base-age + $carry-year"/>
      </xsl:variable>
      <xsl:if test="$age &lt; $threshold-max">
        <xsl:value-of select="name/first"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="name/last"/>
        <xsl:text> will be </xsl:text>
        <xsl:value-of select="$age"/>
        <xsl:text> in </xsl:text>
        <xsl:choose>
          <xsl:when test="$daydiff &lt; 0">
            <xsl:value-of select="$daydiff + $days-this-year"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$daydiff"/>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:text> days (</xsl:text>
        <xsl:value-of select="date:month-name ($birthday)"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="date:day-in-month($birthday)"/>
        <xsl:text>)&#10;</xsl:text>
      </xsl:if>
    </xsl:if>
  </xsl:if>
</xsl:template>

<xsl:template match="/">
  <xsl:variable name="person"
                select="/database/people/person[@id=$individual]"/>

  <!-- Spouse -->
  <xsl:variable name="cfamily"
                select="/database/families/family[@id=$person/parentin/@ref]"/>
  <xsl:variable name="spouse"
       select="/database/people/person[parentin/@ref = $cfamily/@id]"/>
  <xsl:apply-templates select="$spouse"/>

  <!-- Children -->
  <xsl:apply-templates
       select="/database/people/person[childof/@ref = $cfamily/@id]"/>

  <!-- Parents -->
  <xsl:variable name="pfamily"
       select="/database/families/family[@id = $person/childof/@ref or
                                         @id = $spouse/childof/@ref]"/>
  <xsl:variable name="parents"
       select="/database/people/person[parentin/@ref = $pfamily/@id]"/>
  <xsl:variable name="epfamily"
       select="/database/families/family[@id = $parents/parentin/@ref]"/>
  <xsl:apply-templates
       select="/database/people/person[parentin/@ref = $epfamily/@id]"/>

  <!-- Siblings -->
  <xsl:apply-templates
       select="/database/people/person[childof/@ref = $epfamily/@id and
                                       not (@id = $person/@id or
                                            @id = $spouse/@id)]"/>

  <!-- Grand-parents -->
  <xsl:variable name="gfamily"
       select="/database/families/family[@id = $parents/childof/@ref]"/>
  <xsl:apply-templates
       select="/database/people/person[parentin/@ref = $gfamily/@id]"/>

  <!-- Aunts and uncles -->
  <xsl:variable name="aunt-uncle"
       select="/database/people/person[childof/@ref = $gfamily/@id and
                                       not (@id = $parents/@id)]"/>
  <xsl:apply-templates select="$aunt-uncle"/>

  <!-- Cousins -->
  <xsl:variable name="gcfamily"
       select="/database/families/family[@id = $aunt-uncle/parentin/@ref]"/>
  <xsl:apply-templates
       select="/database/people/person[childof/@ref = $gcfamily/@id]"/>
</xsl:template>

</xsl:stylesheet>

