<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:gen="http://ns.cafeconleche.org/genealogy/"
  xmlns:xhtml="http://www.w3.org/1999/xhtml">

  <xsl:template match="/">
    <html>
      <head>
        <title>Family Tree</title>
      </head>
      <body>
        <xsl:apply-templates select="gen:FAMILY_TREE"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="gen:FAMILY_TREE">

    <h1>Family Tree</h1>

    <h2>Families</h2>
    <xsl:apply-templates select="gen:FAMILY"/>

    <h2>People</h2>
    <xsl:apply-templates select="gen:PERSON"/>

    <h2>Sources</h2>
    <ul>
     <xsl:apply-templates select="gen:SOURCE"/>
    </ul>

  </xsl:template>

  <xsl:template match="gen:PERSON">

    <h3>
      <xsl:element name="a">
        <xsl:attribute name="name">
          <xsl:value-of select="@ID"/>
        </xsl:attribute>
      <xsl:value-of select="gen:NAME"/>
      </xsl:element>
    </h3>

    <ul>
      <xsl:if test="gen:BIRTH">
        <li>Born: <xsl:value-of select="gen:BIRTH"/></li>
      </xsl:if>
      <xsl:if test="gen:DEATH">
        <li>Died: <xsl:value-of select="gen:DEATH"/></li>
      </xsl:if>
      <xsl:if test="gen:BAPTISM">
        <li>Baptism: <xsl:value-of select="gen:BAPTISM"/></li>
      </xsl:if>
      <xsl:if test="BURIAL">
        <li>Burial: <xsl:value-of select="gen:BURIAL"/></li>
      </xsl:if>
      <xsl:apply-templates select="gen:FATHER"/>
      <xsl:apply-templates select="gen:MOTHER"/>
    </ul>

    <xsl:apply-templates select="gen:NOTE"/>

  </xsl:template>

  <xsl:template match="gen:FATHER">
    <li>
      <xsl:element name="a">
        <xsl:attribute name="href">#<xsl:value-of 
          select="@PERSON"/></xsl:attribute>
        Father
      </xsl:element>
    </li>
  </xsl:template>

  <xsl:template match="gen:MOTHER">
    <li>
      <xsl:element name="a">
        <xsl:attribute name="href">#<xsl:value-of 
          select="@PERSON"/></xsl:attribute>
        Mother
      </xsl:element>
    </li>
  </xsl:template>

  <xsl:template match="xhtml:body">
    <div>
      <xsl:for-each select="node()|@*">
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </div>
  </xsl:template>

  <xsl:template match="gen:SOURCE">

    <li>
      <xsl:element name="a">
        <xsl:attribute name="name">
          <xsl:value-of select="@ID"/>
        </xsl:attribute>
        <xsl:value-of select="."/>
      </xsl:element>
    </li>

  </xsl:template>

  <xsl:template match="gen:FAMILY">
    <ul>
      <xsl:apply-templates select="gen:HUSBAND"/>
      <xsl:apply-templates select="gen:WIFE"/>
      <xsl:apply-templates select="gen:CHILD"/>
    </ul>
  </xsl:template>

  <xsl:template match="gen:HUSBAND">
    <li>Husband: <a href="#{@PERSON}">
      <xsl:value-of select="id(@PERSON)/gen:NAME"/>
    </a></li>
  </xsl:template>

  <xsl:template match="gen:WIFE">
    <li>Wife: <a href="#{@PERSON}">
      <xsl:value-of select="id(@PERSON)/gen:NAME"/>
    </a></li>
  </xsl:template>

  <xsl:template match="gen:CHILD">
    <li>Child: <a href="#{@PERSON}">
      <xsl:value-of select="id(@PERSON)/gen:NAME"/>
    </a></li>
  </xsl:template>

</xsl:stylesheet>
